[Tutor] greater precision?
John Collins
john at netcore.com.au
Mon Oct 29 12:19:24 CET 2012
Hi Peter,
Thanks.
> [On modern hardware] Python uses IEEE 754 double-precision
> <http://en.wikipedia.org/wiki/Double_precision> internally which gives 15 to
> 17 digits. But of course errors may accumulate.
As in my previous, this script seems to output precisely 12 significant
not 15 and not 17. ??? 15 and I'd be happier right now, as it matches
Excel's precision there, 'as it is'.
> 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?
> It is a toolbox rather than an engine.
Ah ha! *Now* I'm getting it!
> math is basically a wrapper around the C library, and these functions all
> use C's double and [most of the time] your hardware's floating point unit.
Right!
> Yes, Python compiles the source to bytecode before it executes it, and for
> all modules except the main script this bytecode is cached in a .pyc file.
That nails it (as I was wondering, just now, why the primary scripts
remained intact).
> 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.
> Of more practical relevance may be something like gmpy
> <http://code.google.com/p/gmpy/>
I've just had a peek. Looks like what I need. Haven't a clue if I'll
understand how to 'patch it in' to the scripts. Doco read time!
John.
More information about the Tutor
mailing list