From: "Nelson H. F. Beebe" <beebe@math.utah.edu>
To: Kamaraju S Kusumanchi <kamaraju@bluebottle.com>
Cc: guile-devel@gnu.org, beebe@math.utah.edu
Subject: Re: asserting the equality of double values
Date: Sat, 15 Sep 2007 13:20:22 -0600 (MDT) [thread overview]
Message-ID: <CMM.0.95.0.1189884022.beebe@psi.math.utah.edu> (raw)
In-Reply-To: <fch632$kbe$1@sea.gmane.org>
Kamaraju S Kusumanchi asks today:
>> ...
>> x = ldexp (1.0, DBL_MANT_DIG) - 1.0;
>> assert (x == floor (x)); /* should be an integer already */
>>
>> here ldexp and floor both return double values. Is it guaranteed that
>> asserting the equality of two double values will always work? When learning
>> C, I remember reading somewhere not to rely on such comparisons.
>> ...
floor() returns a double that contains a whole number representing the
integer part of its argument. Since a double is a 64-bit quantity on
most modern machines, and in the IEEE 754 floating-point system, the
64-bit format contains a 53-bit significand, the integer part of a
double is too big to store as a 32-bit int type in C.
The comparision x == floor(x) is perfectly okay. Floating-point
comparisons are NOT fuzzy: they are exact. If x contained a whole
number on entry, then floor(x) is bit-for-bit identical to it.
Regrettably, there is a lot of misinformation about floating-point
arithmetic promulgated by book authors and programs who lack
sufficient understanding of the subject.
If you do something like
x = 1.0;
y = 3.0;
if (3.0 * (x/y) == 1.0) ...
you cannot expect the equality to be true everywhere, because 1/3 is
not exactly representable in a finite number of bits in a binary
arithmetic system. This particular example evaluates to true (1) with
IEEE 754 arithmetic in default rounding, but not with other rounding
modes.
However, the point in the sample code at the start of this message is
quite different: it uses ldexp(u,v) to construct a number u * 2**v,
and since u = 1.0 here, the result is 2**v, an EXACTLY-REPRESENTABLE
number. Since DBL_MANT_DIG = 53, the result is the number 2**53 =
9_007_199_254_740_992. Subtracting one produces a significant of all
1-bits: 2**53 - 1 = +0x1.fffffffffffffp+52, and that is the
next-to-largest exactly representable whole number in this arithmetic
system. Its floor is identical, so "x == floor()" is true.
On some historical floating-point designs, this code might not work
like it does in IEEE 754 arithmetic, because of different rounding
behavior.
-------------------------------------------------------------------------------
- Nelson H. F. Beebe Tel: +1 801 581 5254 -
- University of Utah FAX: +1 801 581 4148 -
- Department of Mathematics, 110 LCB Internet e-mail: beebe@math.utah.edu -
- 155 S 1400 E RM 233 beebe@acm.org beebe@computer.org -
- Salt Lake City, UT 84112-0090, USA URL: http://www.math.utah.edu/~beebe/ -
-------------------------------------------------------------------------------
_______________________________________________
Guile-devel mailing list
Guile-devel@gnu.org
http://lists.gnu.org/mailman/listinfo/guile-devel
next prev parent reply other threads:[~2007-09-15 19:20 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2007-09-15 17:53 asserting the equality of double values Kamaraju S Kusumanchi
2007-09-15 19:20 ` Nelson H. F. Beebe [this message]
2007-09-18 0:13 ` Kevin Ryde
2007-09-18 2:44 ` Kamaraju S Kusumanchi
2007-09-17 17:45 ` Stephen Compall
2007-09-18 2:45 ` Kamaraju S Kusumanchi
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
List information: https://www.gnu.org/software/guile/
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=CMM.0.95.0.1189884022.beebe@psi.math.utah.edu \
--to=beebe@math.utah.edu \
--cc=guile-devel@gnu.org \
--cc=kamaraju@bluebottle.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).