Why does np.arccos(1.0) give nan if fed by np.arange?

numpy, python, trigonometry

Solution

This is normal floating point inaccuracy. Adding 0.05 many times to 0.7 does not necessarily add up to 1 exactly.

Changing `print x` to `print repr(x)` outputs 1.0000000000000002 for the last `x`.

>>> np.arccos(1.0000000000000002)
__main__:1: RuntimeWarning: invalid value encountered in arccos
nan

Problem

Can anyone replicate this? ``` import numpy as np print np.arccos(1.0) print np.arccos(1) for x in np.arange(0.7,1,0.05): print x print np.arccos(x) ``` Output: ``` 0.0 0.0 0.7 0.795398830184 0.75 0.722734247813 0.8 0.643501108793 0.85 0.55481103298 0.9 0.451026811796 0.95 0.317560429292 1.0 nan ``` Note the last value,which should be `np.arccos(1.0)`, is `nan`. When I do `np.arccos(1.0)` in the console it is `0.0` as I would expect. What is causing this behaviour? Note, I am using Python 2.7.6 Spyder IDE Win7

Original source