我正在通过
将Pythonfloat
序列化为CSV文件
import csv
some_float = 3 / 5
csv.writerow([..., some_float, ...])
然后通过pandas.read_csv
将其作为np.float64
回复。是否保证我读入的浮点值将是我得到的值,或者我可能会失去一些精度?感谢。
答案 0 :(得分:2)
A comment in the Python source suggests this is safe.
https://github.com/python/cpython/blob/master/Python/pystrtod.c#L828
/* The repr() precision (17 significant decimal digits) is the
minimal number that is guaranteed to have enough precision
so that if the number is read back in the exact same binary
value is recreated. This is true for IEEE floating point
by design, and also happens to work for all other modern
hardware. */
Double check you are getting up to 17 significant digits in your csv file - otherwise you may need to force convert your floating point numbers with repr() on output to csv.