Re: Generating equally-spaced floats with least rounding error

2011-09-25 Thread Terry Reedy
On 9/25/2011 3:36 AM, Arnaud Delobelle wrote: On 24 September 2011 23:10, Terry Reedy wrote: The best you can do for this example is ['%20.18f' % (i/10 ) for i in range(0, 22, 3)] ['0.00', '0.299989', '0.599978', '0.900022', '1.199

Re: Generating equally-spaced floats with least rounding error

2011-09-25 Thread Steven D'Aprano
Terry Reedy wrote: > I do hope you did not stop with my lead-in sentence, and read to the > end, where I gave you most of the answer you were looking for, without > using the fractions module. Yes, I read your entire post, thank you, and for my purposes I'm happy with the fractions module. --

Re: Generating equally-spaced floats with least rounding error

2011-09-25 Thread Arnaud Delobelle
On 24 September 2011 23:10, Terry Reedy wrote: > On 9/24/2011 10:18 AM, Arnaud Delobelle wrote: >>      ((n-i)*a + i*b)/n for i in range(n+1) > ['%20.18f' % x for x in [((7-i)*0.0 + i*2.1)/7 for i in range(8)]] > ['0.00', '0.299989', '0.599978', > '0.9

Re: Generating equally-spaced floats with least rounding error

2011-09-24 Thread Chris Angelico
On Sun, Sep 25, 2011 at 3:31 PM, Steven D'Aprano wrote: 2.1 == F(21, 10) > False > Ahh, but that may just mean that Fraction doesn't implement an == that compares with floats. >>> 2.1==float(F(21,10)) True This may be because one oughtn't to use == with floats anyway. >>> F(2.1)==F(21,10)

Re: Generating equally-spaced floats with least rounding error

2011-09-24 Thread Terry Reedy
On 9/25/2011 1:21 AM, Steven D'Aprano wrote: Terry Reedy wrote: On 9/24/2011 9:53 AM, Steven D'Aprano wrote: [...] [0.0 + i*width for i in range(8)] [0.0, 0.3, 0.6, 0.8999, 1.2, 1.5, 1.7998, 2.1] The 4th and 7th values have rounding errors, the rest are exact No th

Re: Generating equally-spaced floats with least rounding error

2011-09-24 Thread Chris Angelico
On Sun, Sep 25, 2011 at 3:31 PM, Steven D'Aprano wrote:>> If you look again at the code I used, I did. I just did it inside the list> comp. You did, but you created a Fraction from a float; my version used Fraction(21,10) instead of (effectively) Fraction(2.1). My description was a little sloppy,

Re: Generating equally-spaced floats with least rounding error

2011-09-24 Thread John Ladasky
On Sep 24, 6:53 am, Steven D'Aprano wrote: > I'm trying to generate a sequence of equally-spaced numbers between a lower > and upper limit. Given arbitrary limits, what is the best way to generate a > list of equally spaced floats with the least rounding error for each point? > > For example, supp

Re: Generating equally-spaced floats with least rounding error

2011-09-24 Thread Steven D'Aprano
Chris Angelico wrote: > On Sun, Sep 25, 2011 at 3:01 AM, Steven D'Aprano > wrote: >> Mark Dickinson wrote: >> >>> Using Fraction for intermediate calculations actually works perfectly >>> for this, since conversions from float to Fraction are exact, while >>> conversions from Fraction to float ar

Re: Generating equally-spaced floats with least rounding error

2011-09-24 Thread Steven D'Aprano
Terry Reedy wrote: > On 9/24/2011 9:53 AM, Steven D'Aprano wrote: [...] > [0.0 + i*width for i in range(8)] >> [0.0, 0.3, 0.6, 0.8999, 1.2, 1.5, 1.7998, 2.1] >> >> The 4th and 7th values have rounding errors, the rest are exact > > No they are not. Their errors are jus

Re: Generating equally-spaced floats with least rounding error

2011-09-24 Thread Steven D'Aprano
Arnaud Delobelle wrote: > On these examples, using fractions is no better than what I suggested > in my previous post. I'm sorry, I can't see your previous post. Perhaps it isn't available on my news provider? -- Steven -- http://mail.python.org/mailman/listinfo/python-list

Re: Generating equally-spaced floats with least rounding error

2011-09-24 Thread Terry Reedy
On 9/24/2011 9:53 AM, Steven D'Aprano wrote: I'm trying to generate a sequence of equally-spaced numbers between a lower and upper limit. Given arbitrary limits, what is the best way to generate a list of equally spaced floats with the least rounding error for each point? For example, suppose I

Re: Generating equally-spaced floats with least rounding error

2011-09-24 Thread f . derainville
>>> import numpy >>> numpy.arange(0, 3, 0.3) array([ 0. , 0.3, 0.6, 0.9, 1.2, 1.5, 1.8, 2.1, 2.4, 2.7]) -- http://mail.python.org/mailman/listinfo/python-list

Re: Generating equally-spaced floats with least rounding error

2011-09-24 Thread Stefan Krah
Arnaud Delobelle wrote: > start, stop, n = -1, 1.1, 7 > [float(F(start) + i*(F(stop)-F(start))/n) for i in range(n+1)] > > [-1.0, -0.7, -0.39997, -0.09996, > > 0.20004, 0.5001, 0.8, 1.1] > > >>> start, stop, n = -1, 1.1, 7 > >>> [((n-i)*st

Re: Generating equally-spaced floats with least rounding error

2011-09-24 Thread Arnaud Delobelle
On 24 September 2011 18:01, Steven D'Aprano wrote: > Mark Dickinson wrote: > >> Using Fraction for intermediate calculations actually works perfectly >> for this, since conversions from float to Fraction are exact, while >> conversions from Fraction to float are correctly rounded.  So if >> you're

Re: Generating equally-spaced floats with least rounding error

2011-09-24 Thread Mark Dickinson
On Sep 24, 6:01 pm, Steven D'Aprano wrote: > Ah, I knew it was too easy! > > >>> from fractions import Fraction as F > >>> start, stop, n = 1, 3.1, 7 > >>> [float(F(start) + i*(F(stop)-F(start))/n) for i in range(n+1)] > > [1.0, 1.3, 1.6, 1.9001, 2.2, 2.5, 2.8003, 3.1] I b

Re: Generating equally-spaced floats with least rounding error

2011-09-24 Thread Chris Angelico
On Sun, Sep 25, 2011 at 3:01 AM, Steven D'Aprano wrote: > Mark Dickinson wrote: > >> Using Fraction for intermediate calculations actually works perfectly >> for this, since conversions from float to Fraction are exact, while >> conversions from Fraction to float are correctly rounded.  So if >> y

Re: Generating equally-spaced floats with least rounding error

2011-09-24 Thread Steven D'Aprano
Mark Dickinson wrote: > Using Fraction for intermediate calculations actually works perfectly > for this, since conversions from float to Fraction are exact, while > conversions from Fraction to float are correctly rounded. So if > you're using Python, you're not too bothered about efficiency, an

Re: Generating equally-spaced floats with least rounding error

2011-09-24 Thread Chris Angelico
On Sun, Sep 25, 2011 at 12:26 AM, Mel wrote: > low_limit + (total_width * i) / intervals > Definite improvement, if the range is comparatively small. Multiply first, then divide. ChrisA -- http://mail.python.org/mailman/listinfo/python-list

Re: Generating equally-spaced floats with least rounding error

2011-09-24 Thread Mark Dickinson
On Sep 24, 5:46 pm, Steven D'Aprano wrote: > Speed is important, but secondary to correctness. To be honest, I never even > thought of using fractions as an intermediate result -- I was thinking of > generating lists of Fractions. I think I can use this approach. Speed could be improved greatly b

Re: Generating equally-spaced floats with least rounding error

2011-09-24 Thread Steven D'Aprano
Mark Dickinson wrote: > On Sep 24, 2:53 pm, Steven D'Aprano +comp.lang.pyt...@pearwood.info> wrote: >> I'm trying to generate a sequence of equally-spaced numbers between a >> lower and upper limit. Given arbitrary limits, what is the best way to >> generate a list of equally spaced floats with t

Re: Generating equally-spaced floats with least rounding error

2011-09-24 Thread Mark Dickinson
On Sep 24, 2:53 pm, Steven D'Aprano wrote: > I'm trying to generate a sequence of equally-spaced numbers between a lower > and upper limit. Given arbitrary limits, what is the best way to generate a > list of equally spaced floats with the least rounding error for each point? > > For example, supp

Re: Generating equally-spaced floats with least rounding error

2011-09-24 Thread Mel
Steven D'Aprano wrote: > I'm trying to generate a sequence of equally-spaced numbers between a > lower and upper limit. Given arbitrary limits, what is the best way to > generate a list of equally spaced floats with the least rounding error for > each point? > > For example, suppose I want to div

Re: Generating equally-spaced floats with least rounding error

2011-09-24 Thread Arnaud Delobelle
On Sep 24, 2011 2:59 PM, "Chris Angelico" wrote: > > On Sat, Sep 24, 2011 at 11:53 PM, Steven D'Aprano > wrote: > > #1 add multiples of the width to the starting value, 0. > > > > #2 subtract multiples of width from the ending value, 2.1. > > #3, go half and half - generate the lower half by addi

Re: Generating equally-spaced floats with least rounding error

2011-09-24 Thread Vlastimil Brom
2011/9/24 Chris Angelico : > On Sat, Sep 24, 2011 at 11:53 PM, Steven D'Aprano > wrote: >> #1 add multiples of the width to the starting value, 0. >> >> #2 subtract multiples of width from the ending value, 2.1. > > #3, go half and half - generate the lower half by adding to the lower > bound, and

Re: Generating equally-spaced floats with least rounding error

2011-09-24 Thread Chris Angelico
On Sat, Sep 24, 2011 at 11:53 PM, Steven D'Aprano wrote: > #1 add multiples of the width to the starting value, 0. > > #2 subtract multiples of width from the ending value, 2.1. #3, go half and half - generate the lower half by adding to the lower bound, and the upper half by subtracting from the

Generating equally-spaced floats with least rounding error

2011-09-24 Thread Steven D'Aprano
I'm trying to generate a sequence of equally-spaced numbers between a lower and upper limit. Given arbitrary limits, what is the best way to generate a list of equally spaced floats with the least rounding error for each point? For example, suppose I want to divide the range 0 to 2.1 into 7 equal