From: Patrick Maupin on
On Apr 1, 7:49 pm, Tim Chase <python.l...(a)tim.thechases.com> wrote:
> David Robinow wrote:
> > $ python -c "print 1/2 * 1/2"
> > 0
>
> >  But that's not what I learned in grade school.
> > (Maybe I should upgrade to 3.1?)
>
> That's because you need to promote one of them to a float so you
> get a floating-point result:
>
>    >>> 1/2 * 1/2
>    0
>    >>> 1/2 * 1/2.0
>    0.0
>
> Oh...wait ;-)
>
> -tkc

Hmmm, I think I'm starting to see why we need math.fsum() to take care
of those rounding errors...
From: Steven D'Aprano on
On Thu, 01 Apr 2010 19:55:27 -0400, David Robinow wrote:

>>> superpollo wrote:
>>> > how much is one half times one half?
[...]
> Well, my python says:
>
> $ python -c "print 1/2 * 1/2"
> 0
>
> But that's not what I learned in grade school.
> (Maybe I should upgrade to 3.1?)

Python 2.x defaults to integer division, a design error which has been
rectified in 3.x.

One can do any of these:

[steve(a)sylar ~]$ python3.1 -c "print(1/2 * 1/2)"
0.25
[steve(a)sylar ~]$ python2.6 -c "from __future__ import division; print 1/2
* 1/2"
0.25
[steve(a)sylar ~]$ python2.6 -Q new -c "print 1/2 * 1/2"
0.25
[steve(a)sylar ~]$ python2.6 -c "print 0.5 * 0.5"
0.25


and probably many others as well.



--
Steven
From: Steven D'Aprano on
On Thu, 01 Apr 2010 19:49:43 -0500, Tim Chase wrote:

> David Robinow wrote:
>> $ python -c "print 1/2 * 1/2"
>> 0
>>
>> But that's not what I learned in grade school.
>> (Maybe I should upgrade to 3.1?)
>
> That's because you need to promote one of them to a float so you get a
> floating-point result:
>
> >>> 1/2 * 1/2
> 0
> >>> 1/2 * 1/2.0
> 0.0
>
> Oh...wait ;-)

Tim, I'm sure you know the answer to this, but for the benefit of the
Original Poster, the problem is that you need to promote *both* divisions
to floating point. Otherwise one of them will give int 0, which gives 0.0
when multiplied by 0.5.

>>> 1.0/2 * 1/2.0
0.25


If you want an exact result when multiplying arbitrary fractions, you
need to avoid floats and decimals and use Fractions:

>>> Fraction(1, 2)**2
Fraction(1, 4)



--
Steven
From: Lie Ryan on
On 04/02/10 13:01, Patrick Maupin wrote:
> On Apr 1, 7:49 pm, Tim Chase <python.l...(a)tim.thechases.com> wrote:
>> David Robinow wrote:
>>> $ python -c "print 1/2 * 1/2"
>>> 0
>>
>>> But that's not what I learned in grade school.
>>> (Maybe I should upgrade to 3.1?)
>>
>> That's because you need to promote one of them to a float so you
>> get a floating-point result:
>>
>> >>> 1/2 * 1/2
>> 0
>> >>> 1/2 * 1/2.0
>> 0.0
>>
>> Oh...wait ;-)
>>
>> -tkc
>
> Hmmm, I think I'm starting to see why we need math.fsum() to take care
> of those rounding errors...

hmm?

>>> import math
>>> math.fsum([1/2, 1/2])
0.0

it doesn't appear to take care of those rounding errors, not in this
case at least.
From: Patrick Maupin on
On Apr 1, 9:50 pm, Lie Ryan <lie.1...(a)gmail.com> wrote:
> On 04/02/10 13:01, Patrick Maupin wrote:
>
>
>
> > On Apr 1, 7:49 pm, Tim Chase <python.l...(a)tim.thechases.com> wrote:
> >> David Robinow wrote:
> >>> $ python -c "print 1/2 * 1/2"
> >>> 0
>
> >>>  But that's not what I learned in grade school.
> >>> (Maybe I should upgrade to 3.1?)
>
> >> That's because you need to promote one of them to a float so you
> >> get a floating-point result:
>
> >>    >>> 1/2 * 1/2
> >>    0
> >>    >>> 1/2 * 1/2.0
> >>    0.0
>
> >> Oh...wait ;-)
>
> >> -tkc
>
> > Hmmm, I think I'm starting to see why we need math.fsum() to take care
> > of those rounding errors...
>
> hmm?
>
> >>> import math
> >>> math.fsum([1/2, 1/2])
>
> 0.0
>
> it doesn't appear to take care of those rounding errors, not in this
> case at least.

you're right! I mis-read the problem. What we REALLY need is a good
math.fmul() ;-)
First  |  Prev  |  Next  |  Last
Pages: 1 2 3 4 5 6 7 8 9 10 11 12
Prev: free proxy & fast
Next: C-style static variables in Python?