Re: double is integer?
- From: "GT" <ContactGT_remove_@xxxxxxxxxxx>
- Date: Mon, 5 Feb 2007 16:55:23 -0000
"Joseph M. Newcomer" <newcomer@xxxxxxxxxxxx> wrote in message
news:0dcas2lrmirbjg293b14fd9odq39ide0au@xxxxxxxxxx
This is problematic at best. While it might work, in general it is not a
safe thing to do
because what if your value is 2.0000000000001 or 1.99999999999999? After
doing a lot of
floating point computations, you may have a value close to but not
precisely an integer.
For example
double d = 10.0;
double q = d / 3.0;
double t = q * 3.0;
you might assume that d == t, which will not be true, and isInteger(d) may
be true but
isInteger(t) will not be.
On the whole, this is a very risky thing to consider doing. You should
never assume that
any computation involving a double will ever produce an integer value.
For that matter,
if the integer value is very large, the double might lose low-order
precision anyway.
There is no reliable way to actually compare doubles to integers and know
that the double
is an integer value. Typically, you might consider
if( (d - ceil(d)) < fuzzfactor)
but that is about as good as it gets.
joe
How about:
bool isInteger(const double& d)
{
long int i = (long int) d;
if ((double) i == d)
{
// we have an integer
return true;
}
else
return false;
}
.
- Follow-Ups:
- Re: double is integer?
- From: Joseph M . Newcomer
- Re: double is integer?
- From: Doug Harrison [MVP]
- Re: double is integer?
- References:
- double is integer?
- From: Guido Franzke
- Re: double is integer?
- From: Joseph M . Newcomer
- double is integer?
- Prev by Date: Re: sharing variables between dialog boxes
- Next by Date: Re: BringWindowToFront/SetForegroundWindow problem
- Previous by thread: Re: double is integer?
- Next by thread: Re: double is integer?
- Index(es):
Relevant Pages
|