How to set floating point precision?

I just figured out the same number in two ways, but in numpy it makes an error

[[ 0.910221324013388510820732335560023784637451171875]] [[-0.9102213240133882887761274105287156999111175537109375]] 

this number coincides with e ^ (- 15), but then it differs. How can I handle this error?

Is there a way to limit floating point precision?

Since I am calculating exponential with these numbers, even slight differences lead to frustrating errors ...

+8
floating-point numpy
source share
3 answers

Do you care about the actual accuracy of the result or how to return the same numbers from your two calculations?

If you only need the same numbers, you can use np.around() to round the results to some suitable number of decimal places. However, by doing this, you will reduce the accuracy of the result.

If you really want to more accurately calculate the result, you can try using the np.longdouble type for your input array, which , depending on your architecture and compiler , can give you a floating point representation of 80 or 128 bits, rather than the standard 64-bit np.double *.

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

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

Note that not all numpy functions will support the long double part - some will double it.


* However, some compilers (such as Microsoft Visual C ++) will always consider long double as synonyms for double , in which case there will be no difference between the accuracy of np.longdouble and np.double .

+9
source share

In normal use, numpy numbers double. This means that the accuracy will be less than 16 digits. Here is a resolved topic that contains the same problem ...

If you need to increase accuracy, you can use symbolic calculation .... The mpmath library ... is quiet good. The advantage is that you can use unlimited accuracy. However, computing is slower than numpy can do.

Here is an example:

 # The library mpmath is a good solution >>> import sympy as smp >>> import mpmath as mp >>> mp.mp.dps = 50 # Computation precision is 50 digits # Notice that the difference between x and y is in the digit before last (47th) >>> x = smp.mpmath.mpf("0.910221324013388510820732335560023784637451171875") >>> y = smp.mpmath.mpf("0.910221324013388510820732335560023784637451171865") >>> x - y # Must be equal to 1e-47 as the difference is on the 47th digit mpf('1.000014916280995001003481719184726944958705912691304e-47') 

You can't do better with numpy. You can count the exhibitors with better accuracy.

 smp.exp(x).evalf(20) = 2.4848724344693696167 
+3
source share

You can use math.ceil .

For example, you have:

 a = 8.869705968794857 import math print(math.ceil(a*1e10)/1e10) 

This returns 8.8697059688 , i.e. a value of up to ten decimal places. Change 1e10 to 1e15 or any other value accordingly

In your case, it will be:

 a = 0.910221324013388510820732335560023784637451171875 a = math.ceil((a*1e15)/1e15) 

This will give you a = 0.910221324013389

+1
source share

All Articles