# newbie ques - check double

Discussion in 'C++' started by Apple, Sep 28, 2004.

1. ### AppleGuest

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.

Apple, Sep 28, 2004

2. ### John HarrisonGuest

"Apple" <> wrote in message
news:0%e6d.75508\$...
> 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, Sep 28, 2004

3. ### John HarrisonGuest

#include <math.h> for the floor function.

john

John Harrison, Sep 28, 2004
4. ### Mike WahlerGuest

"John Harrison" <> wrote in message
news:...
>
> "Apple" <> wrote in message
> news:0%e6d.75508\$...
> > 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

Mike Wahler, Sep 28, 2004
5. ### David HarmonGuest

On Tue, 28 Sep 2004 17:43:34 GMT in comp.lang.c++, "Mike Wahler"
<> 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!

David Harmon, Sep 28, 2004
6. ### Zian SmithGuest

"Apple" <> wrote in message news:<0%e6d.75508\$>...
> 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;

Zian Smith, Sep 28, 2004
7. ### Mike WahlerGuest

"Zian Smith" <> wrote in message
news:...
> "Apple" <> wrote in message

news:<0%e6d.75508\$>...
> > 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, Sep 28, 2004
8. ### Mike WahlerGuest

"David Harmon" <> wrote in message
news:...
> On Tue, 28 Sep 2004 17:43:34 GMT in comp.lang.c++, "Mike Wahler"
> <> 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

Mike Wahler, Sep 28, 2004