Velocity Reviews - Computer Hardware Reviews

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

Reply
Thread Tools

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.


 
Reply With Quote
 
 
 
 
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


 
Reply With Quote
 
 
 
 
John Harrison
Guest
Posts: n/a
 
      09-28-2004
#include <math.h> for the floor function.

john


 
Reply With Quote
 
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


 
Reply With Quote
 
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!

 
Reply With Quote
 
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;
 
Reply With Quote
 
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


 
Reply With Quote
 
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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
ctime double double check aisling.cronin@gmail.com C Programming 11 03-09-2007 10:39 PM
newbie ques Madhu Ramachandran Perl Misc 17 12-21-2005 04:34 AM
Newbie Ques: ((void(*) (void *) 0) Desmond Foley C Programming 6 06-07-2005 03:00 PM
cannot convert parameter from 'double (double)' to 'double (__cdecl *)(double)' error Sydex C++ 12 02-17-2005 06:30 PM
Newbie Ques??: Camcorder to disk Joe Boater DVD Video 1 10-09-2003 05:19 PM



Advertisments