C
C#2w ago
prxxss

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
Angius2w ago
Not 100% sure what you want to achieve here
prxxss
prxxssOP2w ago
i want 2.2 to be represented as 0x4066 that is the IEEE754 representation of a 16 bit floating point number
MODiX
MODiX2w 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
Angius2w ago
Huh, not quite the expected result
prxxss
prxxssOP2w ago
😭
Angius
Angius2w ago
That said... not sure how can you represent a float as an ushort One has decimal points... another doesn't
prxxss
prxxssOP2w ago
maybe i can use a span :p
Angius
Angius2w 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
prxxss
prxxssOP2w ago
well the goal is float -> binary string but a 16 bit binary string
Angius
Angius2w ago
Floats are 32-bit
prxxss
prxxssOP2w ago
are there 16 bit floats then? i mean like a "half" type floats are singles
Angius
Angius2w 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
prxxss
prxxssOP2w ago
now i need to figure out a way to convert a float to a half?
Angius
Angius2w ago
You will lose precision doing so
prxxss
prxxssOP2w 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
Angius2w 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
prxxss
prxxssOP2w 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
prxxss
prxxssOP2w ago
yeah that works, its the fact that i cant convert it to a ushort
blueberriesiftheywerecats
Its 16 bit now, why convert it
prxxss
prxxssOP2w 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
Angius2w ago
Do that, then Where does the ushort come in?
prxxss
prxxssOP2w ago
because i cant directly convert a half to a binary string
blueberriesiftheywerecats
Convert.ToString(half, 2);
prxxss
prxxssOP2w ago
uh huh
No description
Angius
Angius2w ago
theHalf.ToString("X2")
prxxss
prxxssOP2w ago
thats hex right? bool is the first overload for ToString, it means there isnt an overload supporting Half
Angius
Angius2w ago
You wanted a hexadecimal string, no?
blueberriesiftheywerecats
Half implements IBinaryNumber Maybe casting it to that will help you
prxxss
prxxssOP2w 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