Writing some floats in a file in an efficient way
ast
none at gmail.com
Thu Feb 22 04:28:02 EST 2018
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?
>>
>> 10^9
>
>
> 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)
b'\x00\x00\x00\x00\x00\x00\x00\x00\x7f\xf0\x00\x00\x00\x00\x00\x00'
>>> 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
More information about the Python-list
mailing list