newbie ques - check double

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

  1. Apple

    Apple Guest

    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
    #1
    1. Advertising

  2. "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
    #2
    1. Advertising

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

    john
     
    John Harrison, Sep 28, 2004
    #3
  4. Apple

    Mike Wahler Guest

    "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
    #4
  5. Apple

    David Harmon Guest

    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
    #5
  6. Apple

    Zian Smith Guest

    "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
    #6
  7. Apple

    Mike Wahler Guest

    "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
    #7
  8. Apple

    Mike Wahler Guest

    "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
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ted
    Replies:
    6
    Views:
    772
  2. Sydex
    Replies:
    12
    Views:
    6,503
    Victor Bazarov
    Feb 17, 2005
  3. Desmond Foley

    Newbie Ques: ((void(*) (void *) 0)

    Desmond Foley, Jun 7, 2005, in forum: C Programming
    Replies:
    6
    Views:
    1,036
    Richard Bos
    Jun 7, 2005
  4. ctime double double check

    , Mar 8, 2007, in forum: C Programming
    Replies:
    11
    Views:
    665
    Keith Thompson
    Mar 9, 2007
  5. Madhu Ramachandran

    newbie ques

    Madhu Ramachandran, Dec 15, 2005, in forum: Perl Misc
    Replies:
    17
    Views:
    342
    Eric J. Roode
    Dec 21, 2005
Loading...

Share This Page