Topic: Test equality of floating points: why is it part of C++?
Author: Niels Dekker <ndekker@REMOVETHISnki.nl>
Date: 2000/07/11 Raw View
So given the following function:
bool IsZero(const double d)
{
return d == 0.0;
}
May I conclude that C++ doesn't even guarantee that the following three
calls to IsZero return true?
IsZero(0.0);
const double dZeroConst = 0.0;
IsZero(dZeroConst);
double dZeroVar = 0.0;
IsZero(dZeroVar);
If so, in which cases is the use of the operator == for floating point
numbers guaranteed safe?
If any non-trivial use of == for floating points is unsafe, why is it still
part of the language???
James Kuyper wrote:
>
> Niels Dekker wrote:
> >
> > Given the following MAGIC_NUMBER macro and IsMagic function:
> >
> > #define MAGIC_NUMBER 1.23456789E30
> >
> > bool IsMagic(const double d)
> > {
> > return d == MAGIC_NUMBER;
> > }
> >
> > Does C++ guarantee that the following function call returns true?
> > IsMagic(MAGIC_NUMBER);
[...]
> As a practical matter, you can safely compare for equality integers
> smaller than the smallest integer that can't be represented exactly, but
> the C++ standard doesn't even say that.
Niels Dekker
ndekker "at" nki "dot" nl
---
[ 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 ]