erronneous typecasting

N

nair SL

code to check whether double value is really a floating point
decimal / an integer?



I am checking for valid placeholders for some double variable as
below.

if( (int)dimnValue == dimnValue)

The problem is there is a creepy bug in this LOC as it does not comply
in certain cases.
Can the experts here give there views on why the check is
insufficient.

What is an alternative to check whether the double value is really a
floating point decimal / an integer?
 
J

Juha Nieminen

nair said:
code to check whether double value is really a floating point
decimal / an integer?



I am checking for valid placeholders for some double variable as
below.

if( (int)dimnValue == dimnValue)

The problem is there is a creepy bug in this LOC as it does not comply
in certain cases.
Can the experts here give there views on why the check is
insufficient.

What is an alternative to check whether the double value is really a
floating point decimal / an integer?

A double is *always* a floating point decimal. Maybe you wanted to ask
what is the best check to see if it's equal to an integer, ie. its
decimal part is all zeros?

Can you give a concrete example where "if( (int)dimnValue ==
dimnValue)" does not work? If dimnValue is the result of a calculation
rather than a literal directly assigned to the variable, you may have a
rounding issue.
 
J

James Kanze

A double is *always* a floating point decimal.

I'm not sure what he's asking, so it's hard to say. But on my
machines, floating points are binary. Of course, since 10 is a
multiple of 2, every possible floating point value has an exact
decimal representation. Some with 50 or more characters after
the decimal, however. (But I don't think that that's what he
was really asking.)
Maybe you wanted to ask what is the best check to see if it's
equal to an integer, ie. its decimal part is all zeros?
Can you give a concrete example where "if( (int)dimnValue ==
dimnValue)" does not work?

dimnValue == 1e300
If dimnValue is the result of a calculation rather than a
literal directly assigned to the variable, you may have a
rounding issue.

In which case, his floating point value isn't an integer:).
 
J

Jonathan Lee

I am checking for valid placeholders for some double variable as
below.

if( (int)dimnValue == dimnValue)

Yeah I'm not sure what you want exactly either.. BUT probably you
want to look up modf() in <cmath>. To check if your value is an
integer you could try

double remainder;
modf(dimnvalue, &remainder);
if (fabs(remainder) < eps)
// if (1.0 + fabs(remainder) == 1.0)
// if (remainder == 0.0)

The choice of if condition really depends on how much you want
to account for floating point error. You can choose a small eps
so that something "really close" to an integer is considered an
integer. Alternatively, the "add 1.0" version will force very
small floating point values to be wiped out. I don't really
recommend testing against 0.0, as in the third case, because
a floating point error could make this false when you expect it
to be true. Check out the Parashift FAQ for more info.

--Jonathan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,159
Messages
2,570,879
Members
47,414
Latest member
GayleWedel

Latest Threads

Top