[Tutor] Fraction - differing interpretations for number and string - presentation

Dave Angel davea at davea.name
Fri Apr 17 00:31:50 CEST 2015


On 04/16/2015 01:24 PM, Jim Mooney wrote:
>>
>> Is this "inaccurate"? Well, in the sense that it is not the exact true
>> mathematical result, yes it is, but that term can be misleading if you
>> think of it as "a mistake". In another sense, it's not inaccurate, it is
>> as accurate as possible (given the limitation of only having a certain
>> fixed number of bits).
>> --
>> Steve
>>
>
> ---
> 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)).

You didn't use str(float), you used a simple str.  So there was no 
quantization error since it was never converted to binary floating 
point.  If you have a number that happens to be an exact decimal number, 
don't convert it via float().  Either convert it via Decimal() or 
convert it directly to fraction.


   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.
>

Presumably you didn't read my message.  When you use a literal 1.64 in 
your code, you're telling the compiler to call the float() function on 
the token.  You've already forced the quantization error.  Nothing to do 
with the fraction class.

-- 
DaveA


More information about the Tutor mailing list