1== 1 is False?

Ben Finney bignose-hates-spam at and-zip-does-too.com.au
Tue Jun 24 07:02:54 EDT 2003


On 24 Jun 2003 01:34:12 -0700, Thomas Nücker wrote:
> What do i have to do, if i need "more" exactness? 
>
>>>> 2.7
> 2.7000000000000002
> This is a really strange behaviour!

No, it's perfectly normal; you're converting numbers between decimal,
and a fixed-precision binary representation (in the CPU).  Most decimal
fractions can't be represented exactly in binary, so must be stored
approximately.

What's "strange" is asking computers to store decimal fractions in
binary representation, and expecting the representation to be exact.

It cannot be; computers think in binary, not decimal.  Some tradeoff is
inevitable.  If you don't like the tradeoff presented by your CPU's
storage of floating-point numbers, then:

  - Use a different CPU architecture.  You'll still have the problem
    (they all store numbers in binary), but at a different level of
    precision.

  - Use integer arithmetic.  It's faster than *any* floating-point
    representation, and it's surprising how often it's simply not
    necessary to store and calculate fractional values.  Reformat the
    values only on input and output to look like fractions.

  - Use an arbitrary-precision maths library.  It takes a little
    learning, but you can have any precision you like, at a slight
    performance hit.

    <http://www.python.org/doc/current/lib/module-mpz.html>
    <http://www.egenix.com/files/python/mxNumber.html>
    <http://gmpy.sourceforge.net/>

-- 
 \      "My roommate got a pet elephant. Then it got lost. It's in the |
  `\                           apartment somewhere."  -- Steven Wright |
_o__)                                                                  |
http://bignose.squidly.org/ 9CFE12B0 791A4267 887F520C B7AC2E51 BD41714B




More information about the Python-list mailing list