From: Frédéric M. <fre...@gb...> - 2008-03-30 13:55:27
|
On dimanche 30 mars 2008, Joe Heafner wrote: > Okay this is driving me crazy. Typing arange(0.1,0.4,0.1) should give > [0.1, 0.2, 0.3] but instead it gives [0.1, 0.2, 0.3, 0.4]. Typing > arange(0.1,0.5, 0.1) should give [0.1, 0.2, 0.3, 0.4] and it indeed > does. Another example that doesn't give the "correct" result is > arange(0.2,0.8,0.2), which gives [ 0.2, 0.4, 0.6, 0.8]. Is this a bug in > the arange() code? I've tinkered with this for two days and there seems > to be no way to predict when arange() will behave as documented. I have > my students working on a small assignment that uses arange() and I don't > want this to affect their work. Is there some detail I'm overlooking? arange([start,] stop[, step,], dtype=None) For integer arguments, just like range() except it returns an array whose type can be specified by the keyword argument dtype. If dtype is not specified, the type of the result is deduced from the type of the arguments. For floating point arguments, the length of the result is ceil((stop - start)/step). This rule may result in the last element of the result being greater than stop. I'm afraid you will have to check the result, and correct it. But I agree, this is a strange behaviour! -- Frédéric http://www.gbiloba.org |