[Tutor] Fraction - differing interpretations for number and string - presentation
Wolfgang Maier
wolfgang.maier at biologie.uni-freiburg.de
Thu Apr 16 23:04:36 CEST 2015
On 16.04.2015 19:24, Jim Mooney wrote:
>
> Understood about the quondam inexactness of floating point bit
> representation. I was just wondering why the different implementation of
> representing it when using Fraction(float) as opposed to using
> Fraction(string(float)). In terms of user presentation, the string usage
> has smaller numbers for the ratio, so it would be more understandable and
> should, I assume, be chosen for GUI display.
>
The whole point of the discussion is that this is *not* a presentation
issue. Fraction(1.64) and Fraction("1.64") *are* two different numbers
because one gets constructed from a value that is not quite 1.64.
What sense would it make for Fraction(1.64) to represent itself inexactly ?
However, if you really want a shortened, but inexact answer: Fractions
have a limit_denominator method that you could use like so:
>>> Fraction(1.64).limit_denominator(25)
Fraction(41, 25)
More information about the Tutor
mailing list