Does anyone know why the below doesn't equal 0?
import numpy as np
np.sin(np.radians(180))
or:
np.sin(np.pi)
When I enter it into python it gives me 1.22e-16.
Does anyone know why the below doesn't equal 0?
import numpy as np
np.sin(np.radians(180))
or:
np.sin(np.pi)
When I enter it into python it gives me 1.22e-16.
The problem it is not a rounding error of pi. Note that the problem do not appear for cosine:
The problem is much more ... complicated. It is related with the precision of pi inside the processor. This was discovered and explained here: https://randomascii.wordpress.com/2014/10/09/intel-underestimates-error-bounds-by-1-3-quintillion/
The number
π
cannot be represented exactly as a floating-point number. So,np.radians(180)
doesn't give youπ
, it gives you3.1415926535897931
.And
sin(3.1415926535897931)
is in fact something like1.22e-16
.So, how do you deal with this?
You have to work out, or at least guess at, appropriate absolute and/or relative error bounds, and then instead of
x == y
, you write:(This also means that you have to organize your computation so that the relative error is larger relative to
y
than tox
. In your case, becausey
is the constant0
, that's trivial—just do it backward.)Numpy provides a function that does this for you across a whole array,
allclose
:(This actually checks
abs(y - x) < abs_ bounds + rel_bounds * y)
, but that's almost always sufficient, and you can easily reorganize your code when it's not.)In your case:
So, how do you know what the right bounds are? There's no way to teach you enough error analysis in an SO answer. Propagation of uncertainty at Wikipedia gives a high-level overview. If you really have no clue, you can use the defaults, which are
1e-5
relative and1e-8
absolute.