From: pmontgom@cwi.nl (Peter L. Montgomery)
Subject: Re: floating point comparison
Date: 1997/08/02
Date: 1997-08-02T00:00:00+00:00 [thread overview]
Message-ID: <EE9HBx.H9G@cwi.nl> (raw)
In-Reply-To: 33E261F7.2EFB@mailgw.sanders.lockheed.com
In article <33E261F7.2EFB@mailgw.sanders.lockheed.com>
user <user@mailgw.sanders.lockheed.com> writes:
>Bob Binder wrote:
>> I haven't followed the IEEE fp standard, but I can't imagine how
>> (without resorting to some kind of BCD representation) any floating
>> point scheme using a non-decimal radix gets around the fundamental fact
>> that there are non-terminal fractions in this radix which are not
>> non-terminal in a decimal radix.
>> Try this:
>> x, y float;
>> x = 1.0;
>> y = (x/10.0)*10.0;
>> if x == y {
>> // not likely }
>> else {
>> // more likely
>> };
>Although I am reading this in comp.lang.ada, the suggestion to try a
>simple example like this in C piqued my interest.
>
>On a Sun SPARC-5, with the built in compiler supplied by Sun, this
>code (fixed for syntax) found (x == y) to be true. Using 3.0 and
>7.0 (neither of which have any chance of yielding an exact
>representation
>after the division) also found (x == y) to be true.
You lucked out due to IEEE rounding to nearest.
Each nonzero floating point value is approximated by +- 1 times
an integer mantissa M in [2^52, 2^53) times a power 2^e,
where e is an integer.
When approximating 1/10 = 2^(-56) * (2^56/10),
we want M to be the integer nearest to 2^56/10.
This is (2^56 + 4)/10. The computed value of 1.0/10.0
becomes ((2^56 + 4)/10) * 2^(-56).
Now multiply back by 10.0. The exact product
would be (2^56 + 4)*2^(-56), but the mantissa
is required to be in [2^52, 2^53). The result is scaled
to (2^52 + 0.25)*2^(-52). Now a rounding gives
2^52 * 2^(-52), or 1.0. The final result happens to
match the original x, even though intermediate results were inexact.
On the other hand, try (1.0/49.0)*49.0.
The quotient 1/49 is approximated by ((2^58 - 23)/49) * 2^(-58).
Now multiply back by 49. The normalized mantissa
2^53 - 23/32 rounds to 2^53 - 1, and the product
is approximated by (2^53 - 1)*2^(-53).
[Yes, I ignored denormals and other special operands above.]
--
Peter L. Montgomery pmontgom@cwi.nl San Rafael, California
A mathematician whose age has doubled since he last drove an automobile.
next prev parent reply other threads:[~1997-08-02 0:00 UTC|newest]
Thread overview: 105+ messages / expand[flat|nested] mbox.gz Atom feed top
1997-07-29 0:00 floating point comparison Matthew Heaney
1997-07-30 0:00 ` Robert Dewar
1997-07-30 0:00 ` Matthew Heaney
1997-07-31 0:00 ` Jim Carr
1997-07-30 0:00 ` Matthew Heaney
1997-07-31 0:00 ` Samuel Mize
1997-07-31 0:00 ` Bob Binder (remove .mapson to email)
1997-07-31 0:00 ` Robert Dewar
1997-08-01 0:00 ` Dale Stanbrough
1997-08-04 0:00 ` Paul Eggert
1997-08-06 0:00 ` Robert Dewar
1997-08-14 0:00 ` Paul Eggert
1997-08-01 0:00 ` user
1997-08-02 0:00 ` Peter L. Montgomery [this message]
1997-08-04 0:00 ` W. Wesley Groleau x4923
1997-08-05 0:00 ` Bob Binder (remove .mapson to email)
1997-08-02 0:00 ` Lynn Killingbeck
1997-08-03 0:00 ` Bob Binder (remove .mapson to email)
1997-08-03 0:00 ` Charles R. Lyttle
1997-08-03 0:00 ` Robert Dewar
1997-07-31 0:00 ` Martin Tom Brown
1997-07-31 0:00 ` Robert Dewar
1997-08-02 0:00 ` Lynn Killingbeck
1997-07-31 0:00 ` Gerald Kasner
1997-07-31 0:00 ` Robert Dewar
1997-07-30 0:00 ` Jan Galkowski
1997-07-31 0:00 ` Don Taylor
1997-07-31 0:00 ` Russ Lyttle
1997-08-01 0:00 ` W. Wesley Groleau x4923
1997-08-02 0:00 ` Robert Dewar
1997-08-02 0:00 ` Matthew Heaney
1997-08-03 0:00 ` Robert Dewar
1997-08-04 0:00 ` W. Wesley Groleau x4923
1997-08-05 0:00 ` Jan-Christoph Puchta
1997-08-05 0:00 ` W. Wesley Groleau x4923
1997-08-05 0:00 ` Samuel Mize
1997-08-06 0:00 ` Robert Dewar
1997-08-07 0:00 ` Shmuel (Seymour J.) Metz
1997-08-08 0:00 ` Peter Shenkin
1997-08-09 0:00 ` Albert Y.C. Lai
1997-08-06 0:00 ` Chris L. Kuszmaul
1997-08-07 0:00 ` Dave Sparks
1997-08-08 0:00 ` Robert Dewar
1997-08-08 0:00 ` Jan-Christoph Puchta
1997-08-09 0:00 ` Robert Dewar
1997-08-10 0:00 ` Lynn Killingbeck
1997-08-08 0:00 ` Mark Eichin
[not found] ` <5sbb90$qsc@redtail.cruzio.com>
[not found] ` <5scugs$jdc$1@cnn.nas.nasa.gov>
1997-08-07 0:00 ` Robert Dewar
1997-08-08 0:00 ` Gerhard Heinzel
1997-08-08 0:00 ` schlafly
1997-08-08 0:00 ` Daniel Villeneuve
1997-08-09 0:00 ` Robert Dewar
1997-08-09 0:00 ` David Ullrich
1997-08-10 0:00 ` Robert Dewar
1997-08-16 0:00 ` Andrew V. Nesterov
1997-08-18 0:00 ` Robert Dewar
1997-08-19 0:00 ` Hans Olsson
1997-08-19 0:00 ` Jim Carr
1997-08-21 0:00 ` Christian Bau
1997-08-21 0:00 ` Jim Carr
1997-08-21 0:00 ` Robert Dewar
1997-08-22 0:00 ` Jim Carr
1997-08-22 0:00 ` Robert Dewar
1997-08-23 0:00 ` Jim Carr
1997-08-24 0:00 ` Robert Dewar
[not found] ` <5u4eq6$30b$1@news.lth.se>
1997-08-29 0:00 ` Robert Dewar
1997-09-01 0:00 ` Chris RL Morgan
1997-08-29 0:00 ` Andrew V. Nesterov
1997-08-29 0:00 ` Robert Dewar
[not found] ` <340DF1DD.2736@iop.com>
1997-09-07 0:00 ` Get_Immediate Robert Dewar
1997-09-07 0:00 ` Get_Immediate Robert Dewar
1997-09-08 0:00 ` Get_Immediate J Giffen
1997-09-01 0:00 ` floating point comparison Jim Carr
[not found] ` <checkerEFx6xI.FCM@netcom.com>
1997-09-03 0:00 ` Chris L. Kuszmaul
1997-09-05 0:00 ` Malome Khomo
1997-09-07 0:00 ` Robert Dewar
1997-09-05 0:00 ` Robert Dewar
1997-09-10 0:00 ` Jim Carr
1997-09-12 0:00 ` Robert Dewar
1997-09-15 0:00 ` James Pauley
1997-09-16 0:00 ` Robert Dewar
1997-08-23 0:00 ` W. Wesley Groleau x4923
1997-08-23 0:00 ` Robert Dewar
1997-08-30 0:00 ` Paul Eggert
1997-08-06 0:00 ` Robert Dewar
[not found] ` <33E8E3E1.17EA@pseserv3.fw.hac.com>
[not found] ` <5sbgpk$q0n$1@goanna.cs.rmit.edu.au>
1997-08-07 0:00 ` Robert Dewar
[not found] ` <33FE4603.1B6B@pseserv3.fw.hac.com>
1997-08-23 0:00 ` Robert Dewar
1997-08-08 0:00 ` W. Wesley Groleau x4923
1997-08-06 0:00 ` Robert Dewar
1997-08-07 0:00 ` Dr. Rex A. Dwyer
[not found] ` <33E8DFF6.6F44@pseserv3.fw.hac.com>
1997-08-07 0:00 ` Robert Dewar
[not found] ` <33EA1251.3466@link.com>
[not found] ` <33EA46CC.226@pseserv3.fw.hac.com>
1997-08-08 0:00 ` Christian Bau
1997-08-12 0:00 ` Martin Tom Brown
1997-08-23 0:00 ` W. Wesley Groleau x4923
1997-08-23 0:00 ` Robert Dewar
1997-09-05 0:00 ` Robert I. Eachus
1997-09-06 0:00 ` schlafly
1997-09-09 0:00 ` Robert Dewar
1997-09-07 0:00 ` M. J. Saltzman
1997-09-11 0:00 ` Robin Rosenberg
1997-08-07 0:00 ` Do-While Jones
1997-08-03 0:00 ` Brian Rogoff
1997-08-03 0:00 ` Robert Dewar
1997-08-02 0:00 ` Michael Sierchio
1997-08-08 0:00 ` floating point conversions Mark Lusti
replies disabled
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox