python rounding problem.

Grant Edwards grante at visi.com
Mon May 8 16:11:52 EDT 2006


On 2006-05-08, Thomas Bartkus <thomasbartkus at comcast.net> wrote:

>> does python support true rations, which means that 1/3 is a
>> true one-third and not 0.333333333 rounded off at some
>> arbitrary precision?
>
> At risk of being boring  ;-)
>
> - Python supports both rational and irrational numbers as
>   floating point numbers the way any language on any digital
>   computer does - imprecisely.
>
> A "true" (1/3) can only be expressed as a fraction.

At the risk of being both boring and overly pedantic, that's
not true.  In base 3, the value in question is precisely
representable in floating point: 0.1

> As soon as you express it as a floating point - you are in a
> bit of trouble because that's impossible.

It's not possible in base 2 or base 10.  It's perfectly
possible in base 9 (used by the Nenets of Northern Russia) base
12 (popular on planets where everybody has twelve toes) or base
60 (used by th Sumerians).  [I don't know if any of those
peoples used floating point in those bases -- I'm just pointing
out that your prejudice towards base 10 notation is showing.]

> You can not express (1/3) as a floating point in Python any
> more than you can do it with pencil and paper.

That's true assuming base 2 in Python and base 10 on paper. The
base used by Python is pretty much etched in stone (silicon, to
be precise).  There used to be articles about people working on
base-3 logic gates, but base-3 logic never made it out of the
lab. However, you can pick any base you want when using paper
and pencil. 

> You can be precise and write "1/3" or you can surrender to
> arithmetic convenience and settle for the imprecise by writing
> "0.333333333", chopping it off at some arbitrary precision.

Or you can write  0.1
                     3

:)

-- 
Grant Edwards                   grante             Yow!  Yes, Private
                                  at               DOBERMAN!!
                               visi.com            



More information about the Python-list mailing list