how set numpy floating point accuracy?

Do you care about the actual precision of the result, or about getting the exact same digits back from your two calculations?

If you just want the same digits, you could use np.around() to round the results to some appropriate number of decimal places. However, by doing this you’ll only reduce the precision of the result.

If you actually want to compute the result more precisely, you could try using the np.longdouble type for your input array, which, depending on your architecture and compiler, might give you an 80- or 128-bit floating point representation, rather than the standard 64-bit np.double*.

You can compare the approximate number of decimal places of precision using np.finfo:

print np.finfo(np.double).precision
# 15

print np.finfo(np.longdouble).precision
# 18

Note that not all numpy functions will support long double – some will down-cast it to double.


*However, some compilers (such as Microsoft Visual C++) will always treat long double as synonymous with double, in which case there would be no difference in precision between np.longdouble and np.double.

Leave a Comment