Velocity Reviews > Math errors in python

# Math errors in python

Guest
Posts: n/a

 09-18-2004
In python 2.3 (IDLE 1.0.3) running under windows 95, I get the
following types of errors whenever I do simple arithmetic:

1st example:
>>> 12.10 + 8.30

20.399999999999999
>>> 1.1 - 0.2

0.90000000000000013

2nd example(no errors here):
>>> bool(130.0 - 129.0 == 1.0)

True

3rd example:
>>> a = 0.013
>>> b = 0.0129
>>> c = 0.0001
>>> [a, b, c]

[0.012999999999999999, 0.0129, 0.0001]
>>> bool((a - b) == c)

False

This sort of error is no big deal in most cases, but I'm sure it could
become a problem under certain conditions, particularly the 3rd
example, where I'm using truth testing. The same results occur in all
cases whether I define variables a, b, and c, or enter the values
directly into the bool statement. Also, it doesn't make a difference
whether "a = 0.013" or "a = 0.0130".

I haven't checked this under windows 2000 or XP, but I expect the same
thing would happen. Any suggestions for a way to fix this sort of
error?

DogWalker
Guest
Posts: n/a

 09-18-2004
Have a look at the FAQ (before the response to your message builds).

----- Original Message -----
Newsgroups: comp.lang.python
To: http://www.velocityreviews.com/forums/(E-Mail Removed)
Sent: Saturday, September 18, 2004 9:50 AM
Subject: Math errors in python

In python 2.3 (IDLE 1.0.3) running under windows 95, I get the
following types of errors whenever I do simple arithmetic:

1st example:
>>> 12.10 + 8.30

20.399999999999999
>>> 1.1 - 0.2

0.90000000000000013

2nd example(no errors here):
>>> bool(130.0 - 129.0 == 1.0)

True

3rd example:
>>> a = 0.013
>>> b = 0.0129
>>> c = 0.0001
>>> [a, b, c]

[0.012999999999999999, 0.0129, 0.0001]
>>> bool((a - b) == c)

False

This sort of error is no big deal in most cases, but I'm sure it could
become a problem under certain conditions, particularly the 3rd
example, where I'm using truth testing. The same results occur in all
cases whether I define variables a, b, and c, or enter the values
directly into the bool statement. Also, it doesn't make a difference
whether "a = 0.013" or "a = 0.0130".

I haven't checked this under windows 2000 or XP, but I expect the same
thing would happen. Any suggestions for a way to fix this sort of
error?
--
http://mail.python.org/mailman/listinfo/python-list

Tim Peters
Guest
Posts: n/a

 09-18-2004
> In python 2.3 (IDLE 1.0.3) running under windows 95, I get the
> following types of errors whenever I do simple arithmetic:
>
> 1st example:
> >>> 12.10 + 8.30

> 20.399999999999999

....

http://docs.python.org/tut/node15.html

Gary Herron
Guest
Posts: n/a

 09-18-2004
On Saturday 18 September 2004 09:50 am, Radioactive Man wrote:
> In python 2.3 (IDLE 1.0.3) running under windows 95, I get the
> following types of errors whenever I do simple arithmetic:
>
> 1st example:
> >>> 12.10 + 8.30

>
> 20.399999999999999

It's not a bug, it's a feature of binary arithmetic on ALL coumputers
in ALL languages. (But perhaps Python is the first time it has not
been hidden from you.)

See the Python FAQ entry 1.4.2:

http://www.python.org/doc/faq/genera...-so-inaccurate

Gary Herron

Jeremy Bowers
Guest
Posts: n/a

 09-18-2004
On Sat, 18 Sep 2004 16:50:16 +0000, Radioactive Man wrote:

> In python 2.3 (IDLE 1.0.3) running under windows 95, I get the
> following types of errors whenever I do simple arithmetic:

http://www.python.org/doc/faq/genera...-so-inaccurate

Peter Otten
Guest
Posts: n/a

 09-18-2004

> thing would happen. Any suggestions for a way to fix this sort of
> error?

Starting with Python 2.4 there will be the 'decimal' module supporting
"arithmetic the way you know it":

>>> from decimal import *
>>> Decimal("12.10") + Decimal("8.30")

Decimal("20.40")
>>> Decimal("1.1") - Decimal("0.2")

Decimal("0.9")
>>> Decimal("130.0")-Decimal("129.0") == Decimal("1.0")

True
>>> a, b, c = map(Decimal, "0.013 0.0129 0.0001".split())
>>> a, b, c

(Decimal("0.013"), Decimal("0.0129"), Decimal("0.0001"))
>>> (a-b) == c

True

Peter

=?iso-8859-1?Q?Michel_Claveau_-_abstraction_m=E9ta-galactique_non_trivial?==?iso-8859-1?Q?e_en_fuite
Guest
Posts: n/a

 09-18-2004
Hi !

Many languages, yes.
All languages, no.

Few languages can use the DCB possibilities of the processors.

@-salutations
--
Michel Claveau

Dennis Lee Bieber
Guest
Posts: n/a

 09-18-2004
On Sat, 18 Sep 2004 20:54:42 +0200, Michel Claveau - abstraction
méta-galactique non triviale en fuite perpétuelle.
<(E-Mail Removed) m> declaimed the
following in comp.lang.python:

>
> Few languages can use the DCB possibilities of the processors.
>

Uhmm... Data Control Block?

Did you mean BCD -- binary coded decimal. Which is a form of
decimal arithmetic, and not native binary floating point.

Considering how often the question about strange floating point
output comes up, I come to the conclusion that CS courses no longer
Manual regarding the differences between float and fixed, neither of
which imply decimal.)

Or find a copy of "Real Computing Made Real"
http://www.amazon.com/exec/obidos/tg...95157?v=glance

--
> ================================================== ============ <
> (E-Mail Removed) | Wulfraed Dennis Lee Bieber KD6MOG <
> (E-Mail Removed) | Bestiaria Support Staff <
> ================================================== ============ <
> Overflow Page: <http://wlfraed.home.netcom.com/> <

=?iso-8859-1?Q?Michel_Claveau_-_abstraction_m=E9ta-galactique_non_trivial?==?iso-8859-1?Q?e_en_fuite
Guest
Posts: n/a

 09-18-2004
Hi !

BCD (english) ==> DCB (french)

And Alcyane-basic run, in 1976, with floating-based-DCB-arithmétique, on the
8080, before than IEEE normalize the floating-arithmétique-pow(256)-based

And DCB is native in all Intel processors.

And before, when i use Fortran on mini-computer, there was no this problem.
But, then it's very much easy to work with pow(256), And the anscendant
compatibility become... Hélas.

Michel Claveau

Dennis Lee Bieber
Guest
Posts: n/a

 09-18-2004
On Sat, 18 Sep 2004 22:58:13 +0200, Michel Claveau - abstraction
méta-galactique non triviale en fuite perpétuelle.
<(E-Mail Removed) m> declaimed the
following in comp.lang.python:

>
> And before, when i use Fortran on mini-computer, there was no this problem.

It may have been there, but you never saw it in print <G>

When I took classes in the 70s, we were taught never to rely
upon comparing two floating point numbers for equality. Instead, we were
told to compare for the difference between the two numbers being less
than some epsilon, with the epsilon defined as the smallest difference
that could be considered equal for the numbers being compared (if the
input is only good for 5 significant figures, the epsilon should not be
down into 7 significant)

a = 5.234
b = 5.235
epsilon = 0.0005
if abs(a - b) < epsilon then
equal

--
> ================================================== ============ <
> (E-Mail Removed) | Wulfraed Dennis Lee Bieber KD6MOG <
> (E-Mail Removed) | Bestiaria Support Staff <
> ================================================== ============ <
> Overflow Page: <http://wlfraed.home.netcom.com/> <