How can I convert real number to IBM 32 Float?
I think how to do it with RealDigits[]
, IntegerDigits[]
and FromDigits[]
.
IBM32FloatSignBit =
Function[{num}, UnitStep[-num]];
IBM32FloatExpBits =
Function[{num}, IntegerDigits[RealDigits[num, 16][[-1]] + 64, 2, 7]];
IBM32FloatFractionBits =
Function[{num}, IntegerDigits[RealDigits[num, 16][[1, 1 ;; 6]], 2, 4]];
And the function that will be to convert real number:
IBM32FloatBits =
Function[{num},
FromDigits[#, 2]& /@
Partition[Flatten @ {
IBM32FloatSignBit[num],
IBM32FloatExpBits[num],
IBM32FloatFractionBits[num]
}, {8}]
]
(* for (256.0^3 - 1.0)/256.0^3 return {64, 255, 255, 255} *)
But this way not good. For the large array of numbers function works slowly. Important, that this function will be unpacking array that has been packing. How to avoid this and speed up the reverse conversion to bytes? How to do it without using RealDigits
and FromDigits
? This functions does not support in Compile
.