Velocity Reviews > C++ > newbie ques - check double

# newbie ques - check double

Apple
Guest
Posts: n/a

 09-28-2004
is there any simple way to check if the value of a double is an integer?
like,

double d;
d = 15.5 => NOT an integer
d = 15.0 => IS an integer

if there's no standard function to do that, can anyone give me a hint on how
to write a right one?

thanks.

John Harrison
Guest
Posts: n/a

 09-28-2004

"Apple" <(E-Mail Removed)> wrote in message
news:0%e6d.75508\$(E-Mail Removed). ..
> is there any simple way to check if the value of a double is an integer?
> like,
>
> double d;
> d = 15.5 => NOT an integer
> d = 15.0 => IS an integer
>
> if there's no standard function to do that, can anyone give me a hint on

how
> to write a right one?
>

if (d == floor(d))
// d is an integer

john

John Harrison
Guest
Posts: n/a

 09-28-2004
#include <math.h> for the floor function.

john

Mike Wahler
Guest
Posts: n/a

 09-28-2004

"John Harrison" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>
> "Apple" <(E-Mail Removed)> wrote in message
> news:0%e6d.75508\$(E-Mail Removed). ..
> > is there any simple way to check if the value of a double is an integer?
> > like,
> >
> > double d;
> > d = 15.5 => NOT an integer
> > d = 15.0 => IS an integer
> >
> > if there's no standard function to do that, can anyone give me a hint on

> how
> > to write a right one?
> >

>
> if (d == floor(d))
> // d is an integer

Another alternative is to use 'modf()'.

-Mike

David Harmon
Guest
Posts: n/a

 09-28-2004
On Tue, 28 Sep 2004 17:43:34 GMT in comp.lang.c++, "Mike Wahler"
<(E-Mail Removed)> wrote,
>> if (d == floor(d))
>> // d is an integer

>
>Another alternative is to use 'modf()'.

And there are others. But testing any floating point result for any
exact value typically indicates wrong thinking in the design anyway.
Resist the temptation and save having to fix it in the end!

Zian Smith
Guest
Posts: n/a

 09-28-2004
"Apple" <(E-Mail Removed)> wrote in message news:<0%e6d.75508\$(E-Mail Removed) >...
> is there any simple way to check if the value of a double is an integer?
> like,
>
> double d;
> d = 15.5 => NOT an integer
> d = 15.0 => IS an integer
>
> if there's no standard function to do that, can anyone give me a hint on how
> to write a right one?
>
> thanks.

Try this:

double d;
bool isInt;

isInt = (int(d)-d) ? false : true;

Mike Wahler
Guest
Posts: n/a

 09-28-2004

"Zian Smith" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) om...
> "Apple" <(E-Mail Removed)> wrote in message

news:<0%e6d.75508\$(E-Mail Removed) >...
> > is there any simple way to check if the value of a double is an integer?
> > like,
> >
> > double d;
> > d = 15.5 => NOT an integer
> > d = 15.0 => IS an integer
> >
> > if there's no standard function to do that, can anyone give me a hint on

how
> > to write a right one?
> >
> > thanks.

>
> Try this:
>
> double d;
> bool isInt;
>
> isInt = (int(d)-d) ? false : true;

This can lead to possible undefined behavior (e.g. if
the value of 'd' is greater than 'INT_MAX' or less than
'INT_MIN'. Use the library functions, that's what they're
there for.

-Mike

Mike Wahler
Guest
Posts: n/a

 09-28-2004

"David Harmon" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> On Tue, 28 Sep 2004 17:43:34 GMT in comp.lang.c++, "Mike Wahler"
> <(E-Mail Removed)> wrote,
> >> if (d == floor(d))
> >> // d is an integer

> >
> >Another alternative is to use 'modf()'.

>
> And there are others. But testing any floating point result for any
> exact value

OP is not looking for an 'exact value', but simply whether
a real number has a fractional part. Also note that types
'float' and 'double' can indeed represent integers exactly.

> typically indicates wrong thinking in the design anyway.

I don't think it's 'wrong thinking' to try to determine
if a real number has a fractional part (unless of course
it's not really needed by the application being written).

-Mike