off topic but please forgive me me and answer

Mensanator mensanator at aol.com
Fri Apr 2 15:35:55 EDT 2010


On Apr 1, 9:44 pm, Steven D'Aprano <st... at REMOVE-THIS-
cybersource.com.au> wrote:
> 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)

Where do you get that from?

>
> --
> Steven- Hide quoted text -
>
> - Show quoted text -




More information about the Python-list mailing list