off topic but please forgive me me and answer
Steven D'Aprano
steve at REMOVE-THIS-cybersource.com.au
Fri Apr 2 19:07:53 EDT 2010
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
In older Pythons, there was a demo module Demo/classes/Rat.py but it may
not be installed on your system. See http://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.
--
Steven
More information about the Python-list
mailing list