Zero division error!
Chris Barker
chrishbarker at home.net
Thu Aug 2 15:36:16 EDT 2001
Prem Rachakonda wrote:
> I am writing a script for 'cubic spline interpolation' and when there is
> a float value = 0.004, it is rounded off to zero and I am getting a Zero
> Division error. Is it a python bug?
> x=array([2.056, 2.06, 2.064, 2.068, 2.072, 2.076, 2.08, 2.084, 2.088,
> 2.092, 2.096, 2.1, 2.104])
> ..
> ..
> ..
> d[0]=x[1]-x[0]
> c[1]=(y[1]-y[0])/d[0]
A couple of points:
You are either using the array module, or the Numeric module. In either
case, your problem is probably that you defined d as being an integer
array, so you should define it as a float.
If you are using the array module, you really should be using Numeric:
check it out at:
http://sourceforge.net/projects/numpy
Once you are using NumPy, you might want your code to look something
like:
from Numeric import *
x= array([2.056, 2.06, 2.064, 2.068, 2.072, 2.076, 2.08, 2.084, 2.088,
2.092, 2.096, 2.1, 2.104],Float)
# the "Float" would be assumed here, but it doesn't hurt to make it
explicit
y = ones(x.shape,Float) # just some useless data for testing
#d[0]=x[1]-x[0]
#let's make this an array computation instead:
indices = arange(len(x)-1)
d = take(x,indices+1) - take(x,indices) # boy do I wish I could pass a
sequence in as an index!
c =(take(y,indices+1) - take(y,indices)) / d
-Chris
--
Christopher Barker,
Ph.D.
ChrisHBarker at home.net --- --- ---
http://members.home.net/barkerlohmann ---@@ -----@@ -----@@
------@@@ ------@@@ ------@@@
Oil Spill Modeling ------ @ ------ @ ------ @
Water Resources Engineering ------- --------- --------
Coastal and Fluvial Hydrodynamics --------------------------------------
------------------------------------------------------------------------
More information about the Python-list
mailing list