Topic: Question of DBL_Epsilon...about floating point


Author: "Ron" <nospam@this-address.org>
Date: 1999/12/29
Raw View
"jz" <jz@acer.net> wrote in message news:82bc6i$20u$1@news.ntust.edu.tw...
>
> hi, Dear all:
>        I bought a book which is Applications Programming in C++.
>        IN p.49 writen, Constant DBL_EPSILON is Smallest positive
>         double x such that 1.0+x <> 1.0
>
>         below it has a example 1.9.2:
>
>         double e1= (1.0/3.0) * 2.5, e2 = 5.0/ 6.0;
>
>         we use below sentence to chech equality.
>         abs( e1- e2) <= DBL_EPSILON

This comparison is incorrect. Whatever definition of DBL_EPSILON you use (I
prefer "smallest double x such that 1.0+x != x), you have to scale
DBL_EPSILON to the magnitude of e1 and e2 before making the comparison, or
you'll get spurious results. For example:

    // Borland C++ Builder 4.0 on x86 processor:
    //
    //    DBL_EPSILON == 2.22...E-16

    double e1 = 1.1e-22;
    double e2 = 1.0e-22;

    // Gives true. However, 1.1e-22 and 1.0e-22 are certainly
    // distinguishable...

    if (std::fabs (e1 - e2) < DBL_EPSILON) {...}

    // With proper scaling (to the largest of e1 and e2), you get the right
    // answer, which is that e1 and e2 are distinguishable.

     double a1 = std::fabs (e1);
     double a2 = std::fabs (e2);
     double biggest = std::max (a1, a2);
     double smallest = std::min (a1, a2);

     double adjustedEps = DBL_EPSILON * biggest;

     if ((biggest - smallest) < adjustedEps) {...}

 By the way, use std::numeric_limits <double>::epsilon () rather than
DBL_EPSILON.

 -- Ron



[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: James Kuyper <kuyper@wizard.net>
Date: 1999/12/22
Raw View
David R Tribble wrote:
>
> Gabriel Dos_Reis wrote:
> >
> > Francis Glassborow <francis@robinton.demon.co.uk> writes:
....
> > | And therein lies another problem.  The C++ Standard explicitly
> > | references the 1990 C Standard.  That document is about to be
> > | superseded.  What are the rules for referencing no-longer existing
> > | standards?
> >
> > self immolation :-)
>
> There's no problem.  The ISO C 1989 standard still exists (as do the
> the 1990, 1984, and 1985 variants); it didn't simply vanish because
> a later revision came into being.
>
> That said, it must also be said that ISO C++ (1998) is based on
> the ISO C 1990 standard, and will continue to be based on that
> standard.  The question at hand is, what version of C will the
> next revision of ISO C++ be based on?  Currently, the answer would
> appear to be ISO C 1999.
>
> This will probably pose some compatibility problems for compiler
> vendors who attempt to provide both a C 1999 compiler and a
> C++ 1998 compiler that generate interoperable code.  Perhaps C
> compiler vendors will provide a compiler switch to enable
> C-1990-compliant mode (much like many today provide a pre-ISO/K&R
> compliant mode).
>
> (See my almost-complete draft of the incompatibilities between
> C-1999 and C++-1998 at <http://david.tribble.com/text/cdiffs.htm>
> for further details.)

Also, remember that Section 1.2 p1 of C++98 says "All standards are
subject to revision, and parties agreements based on this International
Standard are encouraged to investigate the possibility of applying the
most recent editions of the standards indicated below." It's entirely in
the spirit of that encouragement, to try to make an implementation as
compatible with C99 as possible.
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: David R Tribble <david@tribble.com>
Date: 1999/12/20
Raw View
Gabriel Dos_Reis wrote:
>
> Francis Glassborow <francis@robinton.demon.co.uk> writes:
>
> | James Kuyper Jr. <kuyper@wizard.net> writes
> |> I don't know what the old version of the C standard said, but the
> |> newly approved one says in section 5.2.4.2.2 p10:
> |> ...
> |
> | And therein lies another problem.  The C++ Standard explicitly
> | references the 1990 C Standard.  That document is about to be
> | superseded.  What are the rules for referencing no-longer existing
> | standards?
>
> self immolation :-)

There's no problem.  The ISO C 1989 standard still exists (as do the
the 1990, 1984, and 1985 variants); it didn't simply vanish because
a later revision came into being.

That said, it must also be said that ISO C++ (1998) is based on
the ISO C 1990 standard, and will continue to be based on that
standard.  The question at hand is, what version of C will the
next revision of ISO C++ be based on?  Currently, the answer would
appear to be ISO C 1999.

This will probably pose some compatibility problems for compiler
vendors who attempt to provide both a C 1999 compiler and a
C++ 1998 compiler that generate interoperable code.  Perhaps C
compiler vendors will provide a compiler switch to enable
C-1990-compliant mode (much like many today provide a pre-ISO/K&R
compliant mode).

(See my almost-complete draft of the incompatibilities between
C-1999 and C++-1998 at <http://david.tribble.com/text/cdiffs.htm>
for further details.)

-- David R. Tribble, david@tribble.com, http://david.tribble.com --
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: Pete Becker <petebecker@acm.org>
Date: 1999/12/08
Raw View
"James Kuyper Jr." wrote:
>
> Floating point numbers cannot, in general, be represented exactly. A 64
> bit floating point representation could represent at most 2^64 different
> floating point numbers exactly;

There's an important distinction here that is often overlooked. Floating
point numbers have exact representations, and well-defined mathematical
operations. Most real numbers do not have exact representations in
floating point, and that is the source of a great deal of confusion.

--
Pete Becker
Dinkumware, Ltd.
http://www.dinkumware.com
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: Gabriel Dos_Reis <gdosreis@korrigan.inria.fr>
Date: 1999/12/08
Raw View
Francis Glassborow <francis@robinton.demon.co.uk> writes:

| In article <384B143F.BF28CB59@wizard.net>, James Kuyper Jr.
| <kuyper@wizard.net> writes
| >I
| >don't know what the old version of the C standard said, but the newly
| >approved one says in section 5.2.4.2.2 p10:
|
| And therein lies another problem.  The C++ Standard explicitly
| references the 1990 C Standard.  That document is about to be
| superseded.  What are the rules for referencing no-longer existing
| standards?

self immolation :-)

--
Gabriel Dos Reis, dosreis@cmla.ens-cachan.fr
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: Francis Glassborow <francis@robinton.demon.co.uk>
Date: 1999/12/08
Raw View
In article <xaj7liqaayh.fsf@korrigan.inria.fr>, Gabriel Dos_Reis
<gdosreis@korrigan.inria.fr> writes
>| And therein lies another problem.  The C++ Standard explicitly
>| references the 1990 C Standard.  That document is about to be
>| superseded.  What are the rules for referencing no-longer existing
>| standards?
>
>self immolation :-)

How silly of me, every experienced programmer knows the results of using
a hanging reference:)


Francis Glassborow      Journal Editor, Association of C & C++ Users
64 Southfield Rd
Oxford OX4 1PA          +44(0)1865 246490
All opinions are mine and do not represent those of any organisation


[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: Steve Clamage <stephen.clamage@sun.com>
Date: 1999/12/05
Raw View
jz wrote:
>
>        I bought a book which is Applications Programming in C++.
>        IN p.49 writen, Constant DBL_EPSILON is Smallest positive
>         double x such that 1.0+x <> 1.0

Actually, I think that should be the "largest positive double smaller
than 1.0", or the "largest value in the range (0.0, 1.0)".

Floating-point numbers are represented with a finite number of bits.
Usually the range of numbers that can be represented is wider than
the number of bits of precision used to represent the number.

For example, in a typical implementation of type double, there are
53 bits of precision (about 16 decimal digits), but numbers in the
approximate range 1E-308 to 1E+308 can be represented. Clearly, if
you add 1E-300 to 1E+300, you need about 600 decimal digits to
represent the value, but you have only about 16.

The epsilon value is the largest fraction that is too small to fit
in the number of precision bits available when repesenting the
value 1.0. With about 16 decimal digits available, the value is in
the neighborhood of 1E-16. On my system, the value is
2.2204460492503131E-16.

If you add epsilon to 1.0, the result is 1.0, since the extra
amount requires more bits to represent than are available.

--
Steve Clamage, stephen.clamage@sun.com


[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: Pete Becker <petebecker@acm.org>
Date: 1999/12/06
Raw View
Steve Clamage wrote:
>
> jz wrote:
> >
> >        I bought a book which is Applications Programming in C++.
> >        IN p.49 writen, Constant DBL_EPSILON is Smallest positive
> >         double x such that 1.0+x <> 1.0
>
> Actually, I think that should be the "largest positive double smaller
> than 1.0", or the "largest value in the range (0.0, 1.0)".
>

The definition of the DBL_xxx macros in ISO C is

 the difference between 1.0 and the least value greater than 1.0 that is
 representable in the given floating point type, b^(1-p).

For those who find floating point confusing, jz's formulation isn't
quite the same. It may be possible to add a value smaller than
DBL_EPSILON to 1.0 and get a result of 1.0 + DBL_EPSILON. The reason for
this non-intuitive result is that floating point math isn't cancellable.
That is, if you know that a + b == a + c, you cannot conclude that b ==
c. It sounds like the author of the book that js cites doesn't
understand this.

--
Pete Becker
Dinkumware, Ltd.
http://www.dinkumware.com


[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: Ross Smith <ross.s@ihug.co.nz>
Date: 1999/12/06
Raw View
Steve Clamage wrote:
>
> jz wrote:
> >
> >        I bought a book which is Applications Programming in C++.
> >        IN p.49 writen, Constant DBL_EPSILON is Smallest positive
> >         double x such that 1.0+x <> 1.0
>
> The epsilon value is the largest fraction that is too small to fit
> in the number of precision bits available when repesenting the
> value 1.0. With about 16 decimal digits available, the value is in
> the neighborhood of 1E-16. On my system, the value is
> 2.2204460492503131E-16.
>
> If you add epsilon to 1.0, the result is 1.0, since the extra
> amount requires more bits to represent than are available.

No, I think JZ's definition is right, apart from the odd usage of <>
instead of !=. The C, C++, IEC559, and LIA standards all agree that
epsilon is the smallest value such that 1+epsilon is not equal to 1.
Your description isn't very clear, but you seem to be claiming that it's
the largest value such that 1+epsilon is equal to 1, which is definitely
wrong.

--
Ross Smith <ross.s@ihug.co.nz> The Internet Group, Auckland, New Zealand
========================================================================
    "Be careful about using the following code -- I've only proven
    that it works, I haven't tested it."           -- Donald Knuth


[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: "James Kuyper Jr." <kuyper@wizard.net>
Date: 1999/12/06
Raw View
Steve Clamage wrote:
>
> jz wrote:
> >
> >        I bought a book which is Applications Programming in C++.
> >        IN p.49 writen, Constant DBL_EPSILON is Smallest positive
> >         double x such that 1.0+x <> 1.0
>
> Actually, I think that should be the "largest positive double smaller
> than 1.0", or the "largest value in the range (0.0, 1.0)".

largest? That would be a number just inside the upper limit, wouldn't
it? That doesn't sound to me like a good definition for DBL_EPSILON. The
C++ standard doesn't define it, except by reference to the C standard. I
don't know what the old version of the C standard said, but the newly
approved one says in section 5.2.4.2.2 p10:

"The values given in the following list shall be replaced by
implementation-defined
constant expressions with (positive) values that are less than or equal
to those shown:

    the difference between 1 and the least value greater than 1 that is
representable in the
given floating point type, b^(1-p)"

I inserted the ^,(, and ) in the last expression, to represent the
superscripting in the original text. The variables it's referring to are
defined in 5.2.4.2.2 p1:

 b base or radix of exponent representation (an integer > 1)
 p precision (the number of base-b digits in the significand)

In other words, it's not quite the same as the definition jz gave, but
close; and quite different from yours.


[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: Gabriel Dos_Reis <gdosreis@korrigan.inria.fr>
Date: 1999/12/06
Raw View
Pete Becker <petebecker@acm.org> writes:

[...]

| For those who find floating point confusing, jz's formulation isn't
| quite the same. It may be possible to add a value smaller than
| DBL_EPSILON to 1.0 and get a result of 1.0 + DBL_EPSILON. The reason for
| this non-intuitive result is that floating point math isn't cancellable.
| That is, if you know that a + b == a + c, you cannot conclude that b ==
| c. It sounds like the author of the book that js cites doesn't
| understand this.

Or is using an old fashioned definition (as I indicated in another
posting).

--
Gabriel Dos Reis, dosreis@cmla.ens-cachan.fr


[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: "James Kuyper Jr." <kuyper@wizard.net>
Date: 1999/12/07
Raw View
jz wrote:
....
>         What's meaning of the below sentence that book writen:
>         Constant DBL_EPSILON is Smallest positive
>         double x such that " 1.0+x <> 1.0 "
                              ^^^^^^^^^^^^^^

Apparently the book was written by someone more familiar with Basic, or
some similar language, than with C or C++. "<>" is used in several
languages to mean the same thing indicated in C and C++ by "!=".

Floating point numbers cannot, in general, be represented exactly. A 64
bit floating point representation could represent at most 2^64 different
floating point numbers exactly; real floating point representations
don't even use all of those possibilities, using some of them to
represent NaNs (Not a Number), +infinity, -infinity, and possibly other
special values. All of the other numbers must be represented by the
nearest exact representation. This means that when you add a
sufficiently small number to 1.0, you've described a number that is
still closer to 1.0 than to any other representable number. Therefore,
typical floating point implementations will satisfy 1.0+x == 1.0, for
sufficiently small x. DBL_EPSILON is the smallest value such that
1.0+DBL_EPSILON > 1.0. It's basically a measure of the precision of
floating point numbers.
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: Gabriel Dos_Reis <gdosreis@korrigan.inria.fr>
Date: 1999/12/07
Raw View
| jz wrote:
| >
| >        I bought a book which is Applications Programming in C++.
| >        IN p.49 writen, Constant DBL_EPSILON is Smallest positive
| >         double x such that 1.0+x <> 1.0

LIA-1, A.5.2.1:

    ... An alternate definition of epsilon currently in use is the
    smalllest floating point number such that the expression
    "1+epsilon" yields a value greater than 1.  This definition is
    flawed because it depends on the characteritics of the rounding
    ^^^^^^
    function.  For example, on an IEEE implementation with
    round-to-positive-infinity, epsilon would be fminD.

Steve Clamage <stephen.clamage@sun.com> writes:

| Actually, I think that should be the "largest positive double smaller
| than 1.0", or the "largest value in the range (0.0, 1.0)".

According to LIA-I it should be 'radix' to the power '1 - precision'.

--
Gabriel Dos Reis, dosreis@cmla.ens-cachan.fr
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: Gabriel Dos_Reis <gdosreis@korrigan.inria.fr>
Date: 1999/12/07
Raw View
Ross Smith <ross.s@ihug.co.nz> writes:

| Steve Clamage wrote:
| >
| > jz wrote:
| > >
| > >        I bought a book which is Applications Programming in C++.
| > >        IN p.49 writen, Constant DBL_EPSILON is Smallest positive
| > >         double x such that 1.0+x <> 1.0
| >
| > The epsilon value is the largest fraction that is too small to fit
| > in the number of precision bits available when repesenting the
| > value 1.0. With about 16 decimal digits available, the value is in
| > the neighborhood of 1E-16. On my system, the value is
| > 2.2204460492503131E-16.
| >
| > If you add epsilon to 1.0, the result is 1.0, since the extra
| > amount requires more bits to represent than are available.
|
| No, I think JZ's definition is right, apart from the odd usage of <>
| instead of !=. The C, C++, IEC559, and LIA standards all agree that
| epsilon is the smallest value such that 1+epsilon is not equal to 1.

I disagree.  LIA-I doesn't say that. More precisely LIA-I explicitely
states that that definition is flawed (cf my other posting).

--
Gabriel Dos Reis, dosreis@cmla.ens-cachan.fr
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: Francis Glassborow <francis@robinton.demon.co.uk>
Date: 1999/12/07
Raw View
In article <384B143F.BF28CB59@wizard.net>, James Kuyper Jr.
<kuyper@wizard.net> writes
>I
>don't know what the old version of the C standard said, but the newly
>approved one says in section 5.2.4.2.2 p10:

And therein lies another problem.  The C++ Standard explicitly
references the 1990 C Standard.  That document is about to be
superseded.  What are the rules for referencing no-longer existing
standards?


Francis Glassborow      Journal Editor, Association of C & C++ Users
64 Southfield Rd
Oxford OX4 1PA          +44(0)1865 246490
All opinions are mine and do not represent those of any organisation
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: Pete Becker <petebecker@acm.org>
Date: 1999/12/07
Raw View
Ross Smith wrote:
>
> No, I think JZ's definition is right, apart from the odd usage of <>
> instead of !=. The C, C++, IEC559, and LIA standards all agree that
> epsilon is the smallest value such that 1+epsilon is not equal to 1.

The C and C++ language definitions say something different. They say
that epsilon is the difference between the smallest representable value
that is greater than 1.0 and 1.0. See my other message.

--
Pete Becker
Dinkumware, Ltd.
http://www.dinkumware.com
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: Lisa Lippincott <lisa_lippincott@bigfix.com>
Date: 1999/12/07
Raw View
James Kuyper Jr. <kuyper@wizard.net> wrote:
> Apparently the book was written by someone more familiar with Basic, or
> some similar language, than with C or C++. "<>" is used in several
> languages to mean the same thing indicated in C and C++ by "!=".

Some of the more careful users of floating point math use "<>" to
mean "less than or greater than," making the distinction between that
condition and "not equal to."

But it seems clear that the author in question is not one of the
more careful users of floating point math.

                                                --Lisa Lippincott


[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: "jz" <jz@acer.net>
Date: 1999/12/04
Raw View
hi, Dear all:
       I bought a book which is Applications Programming in C++.
       IN p.49 writen, Constant DBL_EPSILON is Smallest positive
        double x such that 1.0+x <> 1.0

        below it has a example 1.9.2:

        double e1= (1.0/3.0) * 2.5, e2 = 5.0/ 6.0;

        we use below sentence to chech equality.
        abs( e1- e2) <= DBL_EPSILON
My question is the DBL_EPSILON value = 2,.22045e-16

^^^^^^^^^^^^^^^^^
                                              using cout <<  DBL_EPSILON
        but if e1 == e2 then that value almost equal to 0

        What's meaning of the below sentence that book writen:
        Constant DBL_EPSILON is Smallest positive
        double x such that " 1.0+x <> 1.0 "
                                      ^^^^^^^^^^^^^^
                    thank you!!
                            Have a nice day!!
                                                                    JZ
        Email:  jz@acer.net






[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]