Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Python (http://www.velocityreviews.com/forums/f43-python.html)
-   -   Re: 1== 1 is False? (http://www.velocityreviews.com/forums/t318780-re-1-1-is-false.html)

 =?ISO-8859-1?Q?Thomas_N=FCcker?= 06-24-2003 08:34 AM

Re: 1== 1 is False?

Gerhard Häring <gh@ghaering.de> wrote in message news:<mailman.1056119630.21165.python-list@python.org>...
> delphiro wrote:
> > [...] If I print out the values I get something like 2.260212 <= 2.260212 and 2.260212 >= 2.260212 is NOT true.
> >
> > What's wrong?

>
> Your float values might not be what they look like.
>
> It makes a difference wether you use str() or repr() to convert them to
> strings, for example:
>
> #v+
> >>> 2/3.0

> 0.66666666666666663
> >>> print 2/3.0

> 0.666666666667
> #v-
>
> -- Gerhard

What do i have to do, if i need "more" exactness?
I have for example the problem with the representation of 2.3 or 2.7:
>>> 2.3

2.2999999999999998
>>> 2.7

2.7000000000000002
This is a really strange behaviour!

Thomas

 Max M 06-24-2003 08:52 AM

Re: 1== 1 is False?

Thomas Nücker wrote:

> What do i have to do, if i need "more" exactness?
> I have for example the problem with the representation of 2.3 or 2.7:
>
>>>>2.3

>
> 2.2999999999999998
>
>>>>2.7

>
> 2.7000000000000002
> This is a really strange behaviour!

No it is not. Furthermore it is well documented.

I searched Google for: "python floating point precision"

Second result has the title:

B. Floating Point Arithmetic: Issues and Limitations
http://www.python.org/doc/current/tut/node14.html

regards Max M

 Ben Finney 06-24-2003 11:02 AM

Re: 1== 1 is False?

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

 All times are GMT. The time now is 11:28 PM.