Le 21/02/2018 à 18:23, bartc a écrit :
On 21/02/2018 15:54, ast wrote:
Le 21/02/2018 à 15:02, bartc a écrit :
On 21/02/2018 13:27, ast wrote:

Time efficient or space efficient?

space efficient

If the latter, how many floats are we talking about?


Although it might be better to convert to proper 32-bit float format in this case. This will halve space and probably time requirements.)

Yes, storing 32 bits only floats is a good idea.
and the good news is that struct.pack() does the job.

from struct import *

>>> pack('>ff', 3.1234, 5.3e-7)
b'@G\xe5\xc95\x0eES'  # 2*4 bytes

>>> unpack('>ff', b'@G\xe5\xc95\x0eES')
(3.1233999729156494, 5.300000225361146e-07)

What happens if we input some out of range floats ?

>>> pack('>dd', 3.1e-500, 5.3e400)
>>> unpack('>dd', b'\x00\x00\x00\x00\x00\x00\x00\x00\x7f\xf0\x00\x00\x00\x00\x00\x00')
(0.0, inf)

So underflow/overflow are well handled

I saw there is a half precision float too, on 16 bits, but it is
really unaccurate


Reply via email to