[Tutor] greater precision?

Dave Angel d at davea.name
Mon Oct 29 12:40:39 CET 2012


On 10/29/2012 07:19 AM, John Collins wrote:
> <snip>
>> Equality.
> Right, in a 1,0 sense.
>> Because of rounding errors this is a dangerous operation for floating
>> point
>> numbers:
>>>>> print 1.0 == .1 + .1 + .1 + .1 + .1 + .1 + .1 + .1 + .1 + .1
>> False
> I take it that is because binary for .1 is a repeating sequence, yes?

Yes.  Since it cannot be represented exactly, you get a quantization error.

> <snip>
>> As you correctly observed crospoints() uses only +-*/ and ==, so you can
>> feed it every type that supports these operations (this is called "duck
>> typing"). For example:
>
>>>>> from fractions import Fraction
>>>>> from crosspoint import crosspoint
>>>>> crosspoint(Fraction(0, 1), Fraction(1, 3), Fraction(1, 1),
>>>>> Fraction(1,
>> 3), Fraction(5, 7), Fraction(0, 1), Fraction(5, 7), Fraction(1, 1))
>> (Fraction(5, 7), Fraction(1, 3))
>>>>> p = crosspoint(Fraction(0, 1), Fraction(1, 3), Fraction(1, 1),
>> Fraction(1, 3), Fraction(5, 7), Fraction(0, 1), Fraction(5, 7),
>> Fraction(1,
>> 1))
>>>>> print "%s, %s" % p
>> 5/7, 1/3
>>
>> Yay, infinite precision ;)
> You just lost me.

If you don't use any transcendental functions, then a fraction has no
quantization error.  It's totally precise.



-- 

DaveA



More information about the Tutor mailing list