On 18 dec, 14:09, Peter Otten <__pete...@web.de> wrote: > Jean Dubois wrote: > > I have trouble with the code beneath to make an array with equally > > spaced values > > When I enter 100e-6 as start value, 700e-6 as end value and 100e-6 I > > get the following result: > > [ 0.0001 0.00022 0.00034 0.00046 0.00058 0.0007 ] > > But I was hoping for: > > [ 0.0001 0.0002 0.0003 0.0004 0.0005 0.0006 0.0007] > > It works correctly for other values like 1,7,1 but not for 0.1,0.7,0.1 > > then again for 0.01,0.07,0.01 > > > What I find strange is that for the 1st example "1+abs(float(endvalue)- > > float(startvalue))/float(incr)" gives 7.0 but int() of this value > > gives 6 > > can someone provide help with this issue? > > thanks > > jean > > > #!/usr/bin/python > > import math > > import numpy as np > > print "Enter start value as a float (e.g. 0.001) or in scientific > > notation (e.g. 1e-3): ", > > startvalue = raw_input() > > print "Enter end value: ", > > endvalue = raw_input() > > print "Enter step: ", > > incr = raw_input() > > #nom = number of measurements > > nom=int(1+abs(float(endvalue)-float(startvalue))/float(incr)) > > array=np.linspace(float(startvalue), float(endvalue), float(nom)) > > print "Array with current values: ",array > > If you repeat the calculation of the number of intervals in the interpreter > you get > > >>> 1 + abs(0.0007-0.0001)/0.0001 > > 6.999999999999999 > > Many numbers cannot be represented exactly as float (that's the price you > have to pay for covering a wide range with just a few (8) bytes), and you > have introduced such a small error. The subsequent int() call will round > that float to the integer below it: > > >>> int(_) > > 6 > > While applying round() would work here > > >>> int(round(1 + abs(0.0007-0.0001)/0.0001)) > > 7 > > there is no once-and-for-all solution to the underlying problem. E. g. > > >>> x = 2.**53 > >>> x == x + 1 > > True
thanks jean -- http://mail.python.org/mailman/listinfo/python-list