Re: off topic but please forgive me me and answer
- From: Mensanator <mensanator@xxxxxxx>
- Date: Fri, 2 Apr 2010 16:48:12 -0700 (PDT)
On Apr 2, 6:07 pm, Steven D'Aprano <st...@REMOVE-THIS-
cybersource.com.au> wrote:
On Fri, 02 Apr 2010 12:35:55 -0700, Mensanator wrote:
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?
Where do I get what from? Fraction? Oops, sorry about that.
In Python2.6:
from fractions import Fraction
Ok, thanks. I've been using gmpy to do rational arithmetic:
mpq(1,4)import gmpy
gmpy.mpq(1,2)**2
But I don't have a lot of call for it.
In older Pythons, there was a demo module Demo/classes/Rat.py but it may
not be installed on your system. Seehttp://bugs.python.org/issue1682
If you meant, where did I get the statement about exact results from,
both float and Decimal are fixed precision numbers. float precision is
fixed by the operating system and/or hardware; Decimal precision can be
arbitrarily chosen by the caller, but having made that choice,
calculations are rounded to that precision. Only Fraction gives exact
results for any arbitrary rational number.
Yes, rationals are handy sometimes.
--
Steven
.
- References:
- off topic but please forgive me me and answer
- From: superpollo
- Re: off topic but please forgive me me and answer
- From: Tim Chase
- Re: off topic but please forgive me me and answer
- From: Patrick Maupin
- Re: off topic but please forgive me me and answer
- From: Tim Chase
- Re: off topic but please forgive me me and answer
- From: Steven D'Aprano
- Re: off topic but please forgive me me and answer
- From: Mensanator
- Re: off topic but please forgive me me and answer
- From: Steven D'Aprano
- off topic but please forgive me me and answer
- Prev by Date: Re: Generating text from a regular expression
- Next by Date: Re: off topic but please forgive me me and answer
- Previous by thread: Re: off topic but please forgive me me and answer
- Next by thread: Re: off topic but please forgive me me and answer
- Index(es):
Relevant Pages
|