public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
From: John Love-Jensen <eljay@adobe.com>
To: Robin Ericsson <robin.ericsson@noname4us.com>, <gcc-help@gcc.gnu.org>
Subject: Re: strange double compare problem
Date: Thu, 20 Feb 2003 13:03:00 -0000	[thread overview]
Message-ID: <BA7A2C9C.767B%eljay@adobe.com> (raw)
In-Reply-To: <1045734015.23558.14.camel@pulver.localhost.nu>

Hi Robin,

You are mistaken.

The value 55.8000 is not equal to 52.8500 + 2.9500 using floating point
math.

The reason that it is not equal is because floating point representation is
binary, and the numbers you are using do not have an EXACT binary
representation.  So they are approximated.

The binary approximation of 52.8500 plus the binary approximation of 2.9500
does not equal the binary approximation of 55.8000.  Which is an unexpected
surprise for many people.

In you equality check, you need to do a bounds test.  The bounds test needs
to allow a fudge factor, and within that fudge factor you'd call it
(more-or-less) "equal".

Or you need to use integers with an implied fixed point (and you'll have to
compensate for that fixed point during multiplication or division).

Or you need to use BCD math, with fixed point.

Or you need to link against a robust math library, such as calc:
http://www.isthe.com/chongo/tech/comp/calc/

Sincerely,
--Eljay

      reply	other threads:[~2003-02-20 13:03 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2003-02-20  9:58 Robin Ericsson
2003-02-20 13:03 ` John Love-Jensen [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=BA7A2C9C.767B%eljay@adobe.com \
    --to=eljay@adobe.com \
    --cc=gcc-help@gcc.gnu.org \
    --cc=robin.ericsson@noname4us.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).