C
C#2mo ago
anthony63

Get 16 bit floating point precision as a ushort

I saw on SO that using Convert.ToUInt16(2.2) would give me the 16 bit representation of the float, but instead it just gives me 2, what is the proper way to do what i want?
31 Replies
Angius
Angius2mo ago
Not 100% sure what you want to achieve here
anthony63
anthony63OP2mo ago
i want 2.2 to be represented as 0x4066 that is the IEEE754 representation of a 16 bit floating point number
MODiX
MODiX2mo ago
Angius
REPL Result: Success
var bytes = BitConverter.GetBytes(2.2f);
var s = "";

foreach (var b in bytes)
s += b.ToString("X2");

s
var bytes = BitConverter.GetBytes(2.2f);
var s = "";

foreach (var b in bytes)
s += b.ToString("X2");

s
Result: string
CDCC0C40
CDCC0C40
Compile: 495.085ms | Execution: 40.475ms | React with ❌ to remove this embed.
Angius
Angius2mo ago
Huh, not quite the expected result
anthony63
anthony63OP2mo ago
😭
Angius
Angius2mo ago
That said... not sure how can you represent a float as an ushort One has decimal points... another doesn't
anthony63
anthony63OP2mo ago
maybe i can use a span :p
Angius
Angius2mo ago
Unless you do float -> binary -> ushort, to get an ushort value of the binary representation of the float Which would be... largely pointless, but possible
anthony63
anthony63OP2mo ago
well the goal is float -> binary string but a 16 bit binary string
Angius
Angius2mo ago
Floats are 32-bit
anthony63
anthony63OP2mo ago
are there 16 bit floats then? i mean like a "half" type floats are singles
Angius
Angius2mo ago
Not in C#, no float is 32 bits, double is 64, decimal is 128 Ah, wait https://devblogs.microsoft.com/dotnet/introducing-the-half-type/ https://learn.microsoft.com/en-us/dotnet/api/system.half?view=net-8.0 Available since .NET 5
anthony63
anthony63OP2mo ago
now i need to figure out a way to convert a float to a half?
Angius
Angius2mo ago
You will lose precision doing so
anthony63
anthony63OP2mo ago
yeah im fine with that this doesnt seem to work
Convert.ToUInt16((Half)2.2)
Convert.ToUInt16((Half)2.2)
Unhandled exception. System.InvalidCastException: Unable to cast object of type 'System.Half' to type 'System.IConvertible'.
at System.Convert.ToUInt16(Object value)
at MapEncoder.EncodePosDword(Single x, Single y) in C:\Users\antho\Documents\Code\CSHARP\BitWriter\MapEncoder.cs:line 18
at MapEncoder.EncodeNotes(List`1 notes) in C:\Users\antho\Documents\Code\CSHARP\BitWriter\MapEncoder.cs:line 59
at MapEncoder.Encode(Map map) in C:\Users\antho\Documents\Code\CSHARP\BitWriter\MapEncoder.cs:line 90
at Program.<Main>$(String[] args) in C:\Users\antho\Documents\Code\CSHARP\BitWriter\Program.cs:line 30
Unhandled exception. System.InvalidCastException: Unable to cast object of type 'System.Half' to type 'System.IConvertible'.
at System.Convert.ToUInt16(Object value)
at MapEncoder.EncodePosDword(Single x, Single y) in C:\Users\antho\Documents\Code\CSHARP\BitWriter\MapEncoder.cs:line 18
at MapEncoder.EncodeNotes(List`1 notes) in C:\Users\antho\Documents\Code\CSHARP\BitWriter\MapEncoder.cs:line 59
at MapEncoder.Encode(Map map) in C:\Users\antho\Documents\Code\CSHARP\BitWriter\MapEncoder.cs:line 90
at Program.<Main>$(String[] args) in C:\Users\antho\Documents\Code\CSHARP\BitWriter\Program.cs:line 30
Angius
Angius2mo ago
Well then, look into the Create methods You can't represent a floating point number as an integer Best case scenario it gets rounded, worst case it gets truncated
anthony63
anthony63OP2mo ago
oh wait, i think the issue is that ToUInt16 takes in a generic of IConvertable but half doesnt support it
blueberriesiftheywerecats
just do Half h = (Half)2.2f
anthony63
anthony63OP2mo ago
yeah that works, its the fact that i cant convert it to a ushort
blueberriesiftheywerecats
Its 16 bit now, why convert it
anthony63
anthony63OP2mo ago
i need to write the binary version of it to a file well not to a file to a string i need to convert a half to a binary string
Angius
Angius2mo ago
Do that, then Where does the ushort come in?
anthony63
anthony63OP2mo ago
because i cant directly convert a half to a binary string
blueberriesiftheywerecats
Convert.ToString(half, 2);
anthony63
anthony63OP2mo ago
uh huh
No description
Angius
Angius2mo ago
theHalf.ToString("X2")
anthony63
anthony63OP2mo ago
thats hex right? bool is the first overload for ToString, it means there isnt an overload supporting Half
Angius
Angius2mo ago
You wanted a hexadecimal string, no?
blueberriesiftheywerecats
Half implements IBinaryNumber Maybe casting it to that will help you
anthony63
anthony63OP2mo ago
huh?
Unhandled exception. System.FormatException: Format specifier was invalid.
Unhandled exception. System.FormatException: Format specifier was invalid.
string rep = v.ToString("X2").PadLeft(size, '0');
string rep = v.ToString("X2").PadLeft(size, '0');
i dont think x2 can be used on a non-integer type i found
BitConverter.HalfToUInt16Bits
BitConverter.HalfToUInt16Bits
it works, thanks guys for the brainstorming
Want results from more Discord servers?
Add your server