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?

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".

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
teach the basics of representation... (Try reading the Ada Reference
Manual regarding the differences between float and fixed, neither of
which imply decimal.)

On Sat, 18 Sep 2004 22:58:13 +0200, Michel Claveau - abstraction
méta-galactique non triviale en fuite perpétuelle.
<> 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)

Gary Herron wrote:
> 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/general.html#why-are-floating-point-calculations-so-inaccurate

That's nonsense. My 7-year old TI-83 performs that calculation just
fine, and you're telling me, in this day and age, that Python running on
a modern 32-bit processor can't even compute simple decimals accurately?
Don't defend bad code.

Peter Otten wrote:
> Radioactive Man wrote:
>
>
>>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":
>
>

Great, why fix what's broken when we can introduce a new module with an
inconvenient API.

Perhaps there's a simple explanation for this, but why do we go to the
trouble of computing fractions when our hardware can't handle the
result? If the decimal value of 1/3 is can't be represented in binary,
then don't. We should use an internal representation that stores the
numerator and denominator as separate integers.

Chris S. wrote:
>> Starting with Python 2.4 there will be the 'decimal' module supporting
>> "arithmetic the way you know it":
> Great, why fix what's broken when we can introduce a new module with an
> inconvenient API.

1. It ain't broken.
2. What fraction of the numbers in your programs are constants?

That's called rational arithmetic, and I'm sure you can find a package
that implements it for you. However what would you propose for
irrational numbers like sqrt(2) and transcendental numbers like PI?

While I'd love to compute with all those numbers in infinite
precision, we're all stuck with FINITE sized computers, and hence with
the inaccuracies of finite representations of numbers.

Gary Herron wrote:
> That's called rational arithmetic, and I'm sure you can find a package
> that implements it for you. However what would you propose for
> irrational numbers like sqrt(2) and transcendental numbers like PI?

Sqrt is a fair criticism, but Pi equals 22/7, exactly the form this
arithmetic is meant for. Any decimal can be represented by a fraction,
yet not all fractions can be represented by decimals. My point is that
such simple accuracy should be supported out of the box.
> While I'd love to compute with all those numbers in infinite
> precision, we're all stuck with FINITE sized computers, and hence with
> the inaccuracies of finite representations of numbers.

So are our brains, yet we somehow manage to compute 12.10 + 8.30
correctly using nothing more than simple skills developed in
grade-school. You could theoretically compute an infinitely long
equation by simply operating on single digits, yet Python, with all of
its resources, can't overcome this hurtle?

However, I understand Python's limitation in this regard. This
inaccuracy stems from the traditional C mindset, which typically
dismisses any approach not directly supported in hardware. As the FAQ
states, this problem is due to the "underlying C platform". I just find
it funny how a $20 calculator can be more accurate than Python running
on a $1000 Intel machine.

On Sun, 19 Sep 2004 08:00:03 GMT, Chris S. wrote:
>
> Sqrt is a fair criticism, but Pi equals 22/7, exactly the form this
> arithmetic is meant for. Any decimal can be represented by a fraction,
> yet not all fractions can be represented by decimals. My point is that
> such simple accuracy should be supported out of the box.
>

Share This Page

Welcome to The Coding Forums!

Welcome to the Coding Forums, the place to chat about anything related to programming and coding languages.

Please join our friendly community by clicking the button below - it only takes a few seconds and is totally free. You'll be able to ask questions about coding or chat with the community and help others.
Sign up now!