[SciPy-user] floating point pedagogical problem

Ryan Krauss ryanlists@gmail....
Mon Jun 25 18:52:02 CDT 2007


I have  a floating point issue that is forcing me to teach my students
more python/computer science than I want to:

In [36]: temp
Out[36]: NumPy array, format: long
[-1.         -0.83045267 -0.64773663 -0.45185185 -0.24279835 -0.02057613
  0.21481481  0.46337449  0.72510288  1.        ]

In [37]: arccos(temp)
Out[37]: NumPy array, format: long
[                  nan            2.55071609            2.27540616
            2.03963643            1.81604581            1.59137391
            1.35429411            1.08899693            0.75961255
                   nan]


We are doing a robotics problem involving inverse kinematics where
they need to take the arccos and arcsin of some vectors.  The problem
is that
In [38]: eps=1e-15

In [39]: -1-eps < temp[0] < -1+eps
Out[39]: True

So, my current solution is to check for theta +/- 1 +/- eps problems like this:

tempout = []
for item in temp:
	if -1-eps<item<-1+eps:
		tempout.append(-1.0)
	elif 1-eps<item<1+eps:
		tempout.append(1.0)
	else:
		tempout.append(item)
tempout = array(tempout)

Is there a better way?  Is making arccos and arcsin check for +/-1 +/-
eps reasonable?  Or should I give a lecture on floating point evils?

Ryan


More information about the SciPy-user mailing list