Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: reading float80 values

I have an array of 10-bytes (80-bits) Little Endian float values (or float80). How can i read this values in python 3?

The package struct does not support float80 (may be I read the docs carelessly).

The package array as same as package "struct" does not support float80.

The package numpy supports float128 or float96 types. It's very good, but appending \x00 in a tail of float80 to extend it to float96 or float128 is ugly, importing of this package takes a lot of time.

The package ctypes supports c_longdouble. It's many times faster then numpy, but sizeof(c_longdouble) is machine-dependent and can be less then 80 bits, appending \x00 in a tail of float80 to extend it to c_longdouble is ugly too.

UPDATE 1: test code at my gist.github. The function decode_str64 is ugly, but it works. Now I'm looking for right way

like image 491
kai3341 Avatar asked Nov 16 '25 09:11

kai3341


1 Answers

Let me rewrite my answer in a more logical way:

ctypes c_longdouble is machine dependent because the longdouble float type is not set in stone by the C standard and is dependent on the compiler :( but it is still your best you can have right now for high precision floats...

If you plan to use numpy, numpy.longdouble is what your are looking for, numpy.float96 or numpy.float128 are highly misleading names. They do not indicate a 96- or 128-bit IEEE floating point format. Instead, they indicate the number of bits of alignment used by the underlying long double type. So e.g. on x86-32, long double is 80 bits, but gets padded up to 96 bits to maintain 32-bit alignment, and numpy calls this float96. On x86-64, long double is again the identical 80 bit type, but now it gets padded up to 128 bits to maintain 64-bit alignment, and numpy calls this float128. There's no extra precision, just extra padding.

Appending \x00 at the end of a float80 to make a Float96 is ugly, but in the end it is just that as float96 is just a padded float80 and numpy.longdouble is a float96 or float128 depending of the architecture of the machine you use.

What is the internal precision of numpy.float128?

like image 196
Cabu Avatar answered Nov 17 '25 21:11

Cabu



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!