[Edu-sig] Math weirdness?

Kirby Urner urnerk@qwest.net
Wed, 03 Oct 2001 12:15:15 -0700


At 12:03 PM 10/3/2001 -0400, Robert Rickenbrode II wrote:
>Hey folks, can someone explain these to me:
>
> >>> import math
> >>> math.pi
>3.1415926535897931
> >>> round (math.pi, 3)
>3.1419999999999999
> >>>
>
>
>and this:
>
> >>> 10./3
>3.3333333333333335

It's the usual floating point thing.  Internally, 10./3 is
stored in base 2, and it doesn't have the same repeating
pattern as in decimal.  When it chops at the end (it's gotta
chop somewhere), it loses the significant bits that'd enable
it to translate back to a decimal ending in 3, as we would
get in base 10.

Some similar explanation relates to the PI examples.

You'll find floating points have a lot of weird properties.
They don't behave "just like good little decimals".  But
this behavior isn't unique to Python.  Floating point
specs are spelled out independently of the language,
with a focus on word length and stuff.

Kirby