Float compression [Re: floating point math results question]
Jason Orendorff
jason at jorendorff.com
Sun Jan 27 13:00:55 EST 2002
PEP 239 - Adding a Rational Type to Python
http://python.sourceforge.net/peps/pep-0239.html
The PEP itself leaves too many things undiscussed;
but I think the idea is:
type(150/181) is rational #not int or float
str(150/181) == "150/181"
Johnathan Hogg wrote:
> A more interesting problem is an efficient algorithm for refactoring an
> arbitrary rational number into a sequence of the form:
>
> ... + 100.k[2] + 10.k[1] + k[0] + k[-1]/10 + k[-2]/100 + ...
>
> where 0 <= k[i] < 10, which you would want for printing out the
> representation as a normal floating point number. [Of course noting that
> many rationals are non-terminating as floats.]
For the ordinary case where the desired precision is small,
you can just: str(float(num)/float(denom))
## Jason Orendorff http://www.jorendorff.com/
More information about the Python-list
mailing list