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
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.
--
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
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)
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
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,
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
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
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
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
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
>>> 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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
26 matches
Mail list logo