does anyone have or want to gin up a numeric algorithm for converting an integer into the number you'd get by reinterpreting its bits as a floating point number?

enthusiast enthusiast
offsite reposts: @nex3-reposts
clips & pics: @nex3-moments
Cohost utilities:
header photo by me
does anyone have or want to gin up a numeric algorithm for converting an integer into the number you'd get by reinterpreting its bits as a floating point number?
it's not a numerical algorithm anyway - you're dealing with interpreting the same binary string in two separate systems that coincide on that particular keyvalue.
or, the cast is just as valid - and probably outperforms - any "numerical algorithm" you'd come up with. same ins, same outs.
but there are computing environments in which I can't feasibly do a bitwise cast between two different numeric types
how much do you care about different machines giving different answers with the same inputs? i feel like this has the potential for endianness issues
anyhow: geeks for geeks has this on float32's bit mapping;
https://www.geeksforgeeks.org/introduction-of-floating-point-representation/
i'm not 100% on how that translates to float64 but if you can dump the int to a binary string (how do you feel about writing 32 binary and statements? 64 of them?) you can reinterpret it as a float
i am struggling with cohost's markdown. here's the pastebin version of the python version, where the whitespace is preserved and it doesn't look like ass: https://pastebin.com/G7z42JCP
it is expecting to use 64b ints and floats.
what you would be looking for to run this is
binstring_to_float64(int_to_binstring( input ))
some caveats -
• this runs in linear time; that's probably fine for like. one off runs but if you make a web utitlity for this you should probably rewrite it to do better than that
• I opted for readability rather than concisity or minimal variable allocation / garbage collection since you probably aren't using python and you'll need to translate it.
• This might not use the exact bitstring format the computer you're using uses because of endianness. :shrug; this will be consistent between systems though.
• hopefully you are using a language with a binary and; otherwise it's something like this for the first function: https://pastebin.com/ncqmuXcz
• hopefully you are using a language with strings/stringlikes as primitives; I can't help you work around that one
and then one last note: most human readable positive numbers are just going to produce very small decimals this way. like you have 64 bits and until one of them goes over 2^53 ( aka 9007199254740992 ), they're all being lowered to the 2^-1022th power
so you wouldn't want to just do something like C++20's std::bit_cast either, or a bitwise operation, or multiplication/division as replacement for a bitwise operation, making it numerical even though it's just bit math? I as an unsigned int32: ( I % 2^31 ) / 2^23 = exponent, I / 2^31 for the sign, I % 2^23 for the remainder? (A bunch of bitshifts as integer math)
Not a numeric answer, but the way you do this in C# is incredibly weird. You have to explicitly layout struct memory so it reads a float and an int from the same spot in memory. You are, for some reason, allowed to do this is a safe context.
Hrmm, putting some thought into this, masking the sign bit, the exponent bits, and then the mantissa bits is fairly easy. Then take the exponent value, divide it by whatever ~2^23 value to recover it, and then just some addition/subtraction to get the actual exponent value. For the mantissa I think you just... take the masked bits and repeatedly divide by 2 until you're below 1, then add 1. Then don't forget the sign bit. There's probably some subnormal nonsense you have to deal with and also if you're doing this math itself in floating point numbers you're definitely going to accumulate enough error that you won't get a perfect reconstruction... so you might need to do something even more bit by bit to unpack the exponent and mantissa. It's definitely not a fuuuun time.
EDIT: Oh and endianness, if you are trying to move values between systems. No consistency there.
For the mantissa you don’t have to do any repeated division, you just need to divide by ~2^23 and add 1 unless the number is subnormal (which is a conditional on the exponent)