simple problem with arange / roundoff

Hi, I discovered a bug in one of my program probably due to a round-off problem in a "arange" statement. I use something like: step = (end - start) / (npix - 1.) gridX = num.arange(start-step/2., end+step/2., step) where I wish to get a simple 1D array with npix+1 numbers going from (start-step/2.) to (end+step/2.). But then, "arange" often gets me an array only going from "start-step/2." to "end - step/2." instead, due very probably to round-off problems (I guess it does not reach the last value because <<(start-step/2.) + npix * step >> is found to be larger than (end+step/2.). Here is an example: start = -30. end = 30. npix = 31 step = (end - start) / (npix - 1.) gridX = num.arange(start-step/2., end+step/2., step) array([-31., -29., -27., -25., -23., -21., -19., -17., -15., -13., -11., -9., -7., -5., -3., -1., 1., 3., 5., 7., 9., 11., 13., 15., 17., 19., 21., 23., 25., 27., 29.]) As you can see, it does not go up to 31., but only to 29, although step is = 2.0 Is there is a way out of this ? (except by doing the silly: gridX = num.arange(start-step/2., end+1.001*step/2., step) ) Thanks for any input there (and sorry for the silly question) Eric

On 7/31/07, Eric Emsellem <emsellem@obs.univ-lyon1.fr> wrote:
Here is an example:
start = -30. end = 30. npix = 31 step = (end - start) / (npix - 1.) gridX = num.arange(start-step/2., end+step/2., step) array([-31., -29., -27., -25., -23., -21., -19., -17., -15., -13., -11., -9., -7., -5., -3., -1., 1., 3., 5., 7., 9., 11., 13., 15., 17., 19., 21., 23., 25., 27., 29.])
As you can see, it does not go up to 31., but only to 29, although step is = 2.0
import numpy.matlib as M M.linspace(-30,30,31)
array([-30., -28., -26., -24., -22., -20., -18., -16., -14., -12., -10., -8., -6., -4., -2., 0., 2., 4., 6., 8., 10., 12., 14., 16., 18., 20., 22., 24., 26., 28., 30.])

Keith Goodman wrote:
import numpy.matlib as M M.linspace(-30,30,31)
Tim mentioned it, but just to be clear: linspace() is so useful that it now lives in the numpy namespace:
numpy.linspace is numpy.matlib.linspace True
numpy.linspace(1,10,10) array([ 1., 2., 3., 4., 5., 6., 7., 8., 9., 10.])
-- Christopher Barker, Ph.D. Oceanographer Emergency Response Division NOAA/NOS/OR&R (206) 526-6959 voice 7600 Sand Point Way NE (206) 526-6329 fax Seattle, WA 98115 (206) 526-6317 main reception Chris.Barker@noaa.gov

On 7/31/07, Eric Emsellem <emsellem@obs.univ-lyon1.fr> wrote:
Hi,
I discovered a bug in one of my program probably due to a round-off problem in a "arange" statement. I use something like:
step = (end - start) / (npix - 1.) gridX = num.arange(start-step/2., end+step/2., step)
where I wish to get a simple 1D array with npix+1 numbers going from (start-step/2.) to (end+step/2.).
But then, "arange" often gets me an array only going from "start-step/2." to "end - step/2." instead, due very probably to round-off problems (I guess it does not reach the last value because <<(start-step/2.) + npix * step >> is found to be larger than (end+step/2.).
Here is an example:
start = -30. end = 30. npix = 31 step = (end - start) / (npix - 1.) gridX = num.arange(start-step/2., end+step/2., step) array([-31., -29., -27., -25., -23., -21., -19., -17., -15., -13., -11., -9., -7., -5., -3., -1., 1., 3., 5., 7., 9., 11., 13., 15., 17., 19., 21., 23., 25., 27., 29.])
As you can see, it does not go up to 31., but only to 29, although step is = 2.0
Is there is a way out of this ? (except by doing the silly: gridX = num.arange(start-step/2., end+1.001*step/2., step) )
Yes. Don't use arange with floating point numbers steps. Either write this as something equivalent to: gridX = num.arange(npix) * step + start or use linspace. gridX = num.linspace(start, stop, npix) Thanks for any input there (and sorry for the silly question)
Eric _______________________________________________ Numpy-discussion mailing list Numpy-discussion@scipy.org http://projects.scipy.org/mailman/listinfo/numpy-discussion
-- . __ . |-\ . . tim.hochberg@ieee.org
participants (4)
-
Christopher Barker
-
Eric Emsellem
-
Keith Goodman
-
Timothy Hochberg