[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Problem with comparing and adding
From: |
Juergen Kahrs |
Subject: |
Re: Problem with comparing and adding |
Date: |
Wed, 08 Mar 2006 09:40:52 +0100 |
User-agent: |
Mozilla Thunderbird 1.0.6-1.4.1 (X11/20051026) |
Lec Maj wrote:
I ran across a problem with comparing 2 numbers while adding. I am not
sure if this is suppose to be like this or if it is a bug. Can you
please verify this. I downloaded and compiled the latest version of
gawk and I see the same problem.
Hans-Bernhard Broeker has already pointed out what
the problem really is, and he is right. These questions
come up here every few months, and most times people
use gawk when doing arithmetic on finite precision
floating point numbers. It looks like gawk is actually
quite popular for adding up numbers in ASCII files.
As you will notice, either way the answer is 0.3, but the conditional
statement acts differently. There is a work around to check each time
for <= or >=, but we do not know of all the places in the code this is
done, and we are bound to miss one. Thanks for your help.
Numbers like 0.3 cannot be represented exactly with
floating point numbers which have 2 as their base.
You could use base-10 numbers.
As an alternative, you could use libs like MPFR,
but be warned that MPFR still uses base-2 floats.
And if you want to stick with gawk, have a look
at xgawk, which will soon be released (beta) with
an MPFR extension.