"greubel" wrote:
Yes, I'm generating a real float, with random values, exponent and sign. Then sending it through the float2byte and then the byte2float and comparing the input value to the result.
No, i meant testing only the two functions against each other won't suffice (consider implementer bias, big vs little endianness). Real, external data like so:
>>> from struct import pack
>>> for f in 0, 1, -1, 123, 0.1, -4.2e9, 3e-42: print pack('f', f).encode('hex'), f
...
00000000 0
0000803f 1
000080bf -1
0000f642 123
cdcccc3d 0.1
ea567acf -4200000000.0
5d080000 3e-42
One thing, need to start the exp initial value closer to the actual by using the log() function. I'm old school, need efficient code. The way it is now, we could do the whiles a hundred or more times.
Hm, i did not think about that. Yep, could go a good hundred times. You know what, feels trivial - to just take what you were doing as first approximation,
exp = int(log(X) / log(2)) : X = X / 2^exp - and then do the whiles. Ha! I bet they will loop only once at most - but even if log/log was off by more, they will get the right result. Brilliant.