I’m trying to convert a float number from base10 to base2/8/16, then convert it to bytes (base256) of the converted values, by far I can do it mathematically like:
0.15625 (base 10) = 0.00101 (base2) = 101 x 2^-5 (base2) --> 0x00 (positive sign), 0x05 (mantissa), 0xFB (exponent)
0.15625 (base 10) = 0.12 (base8) = 12 x 8^-2 (base8) --> 0x00 (positive sign), 0x0c (mantissa), 0xFE (exponent)
-0.15625 (base 10) = -0.00101 (base2) = -101 x 2^-5 (base2) --> 0xFF (negative sign), 0x05 (mantissa), 0xFB (exponent)
-0.15625 (base 10) = -0.12 (base8) = -12 x 8^-2 (base8) --> 0xFF (negative sign), 0x0c (mantissa), 0xFE (exponent)
Is there an existing library that can convert float32
, float64
, or big.Float
from math package to the desired output or do I have to create the library on my own?
The mathematical conversion involves a lot of division in the mathematical world and I heard it can strain the CPU with too many division operations.