check if a double is odd or not

Discussion in 'C Programming' started by Angelo Chen, Jun 18, 2008.

  1. Angelo Chen

    Angelo Chen Guest

    hi,
    any quick way to check if a double is an odd number or not? thanks
    Angelo Chen, Jun 18, 2008
    #1
    1. Advertising

  2. Angelo Chen

    rahul Guest

    On Jun 18, 12:19 pm, Angelo Chen <> wrote:
    > hi,
    > any quick way to check if a double is an odd number or not? thanks


    What's with double and odd? The simples test is to calculate mod 2:
    unsigned int n = NUM;
    if ( 1 == (n % 2)) {
    /* odd */
    }

    How do you define odd for decimals? 2.1 is odd or even? By double, if
    you just mean to have larger size and not the decimal values, then you
    can cast the result to int.
    double num = NUM;
    if ( 1 == (int)(num % 2)){
    /* odd */
    }
    rahul, Jun 18, 2008
    #2
    1. Advertising

  3. Angelo Chen

    Angelo Chen Guest

    Hi,

    you are correct, I have this need that the double contains a time
    interval in seconds since jan, 1, 2001, so the double will not have a
    fractional part, here is what I use now, but not so sure if this
    applies to all situation:

    double t; // t is a time interval set somewhere

    double d = t / 2.0;
    if (floor(d)*2.0 != t) {
    // odd number
    }



    On Jun 18, 3:46 pm, Richard Heathfield <> wrote:
    > Angelo Chen said:
    >
    > > hi,
    > > any quick way to check if a double is an odd number or not? thanks

    >
    > First, please tell me whether 3.1415926 is an odd number.
    >
    > --
    > Richard Heathfield <http://www.cpax.org.uk>
    > Email: -http://www. +rjh@
    > Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
    > "Usenet is a strange place" - dmr 29 July 1999
    Angelo Chen, Jun 18, 2008
    #3
  4. Angelo Chen

    Guest

    On Jun 18, 10:47 am, Angelo Chen <> wrote:
    > Hi,
    >
    > you are correct, I have this need that the double contains a time
    > interval in seconds since jan, 1, 2001, so the double will not have a
    > fractional part, here is what I use now, but not so sure if this
    > applies to all situation:
    >
    > double t; // t is a time interval set somewhere
    >
    > double d = t / 2.0;
    > if (floor(d)*2.0 != t) {
    > // odd number
    >
    > }

    Please do not top-post.
    read <http://www.caliburn.nl/topposting.html> to learn why

    Here's how I'd do it:
    double d = 12345.6789;
    if((unsigned long)d & 1) /* odd */
    else /* even */
    , Jun 18, 2008
    #4
  5. Angelo Chen

    Guest

    On 18 Jun., 09:47, Angelo Chen <> wrote:
    > Hi,
    >
    > you are correct, I have this need that the double contains a time
    > interval in seconds since jan, 1, 2001, so the double will not have a
    > fractional part, ...


    If there is no fractional part I would suggest you don't
    use double at all. Use some integer type like 'long'.

    Greetings Thomas Mertes

    Seed7 Homepage: http://seed7.sourceforge.net
    Seed7 - The extensible programming language: User defined statements
    and operators, abstract data types, templates without special
    syntax, OO with interfaces and multiple dispatch, statically typed,
    interpreted or compiled, portable, runs under linux/unix/windows.
    , Jun 18, 2008
    #5
  6. Angelo Chen

    Boon Guest

    Angelo Chen wrote:

    > I have this need that the double contains a time
    > interval in seconds since jan, 1, 2001, so the double will not have a
    > fractional part, here is what I use now, but not so sure if this
    > applies to all situation:
    >
    > double t; // t is a time interval set somewhere
    >
    > double d = t / 2.0;
    > if (floor(d)*2.0 != t) {
    > // odd number
    > }


    I would write.

    #include <math.h>
    if (lrint(t) & 1) /* odd */ else /* even */
    Boon, Jun 18, 2008
    #6
  7. In article <>,
    <> wrote:

    >> you are correct, I have this need that the double contains a time
    >> interval in seconds since jan, 1, 2001, so the double will not have a
    >> fractional part, ...


    >If there is no fractional part I would suggest you don't
    >use double at all. Use some integer type like 'long'.


    On many systems, a double can accurately represent integers with
    larger values than any integer type. (Of course, this is less true
    now with long long, but that's still not universally available.)
    And intervals of seconds can be quite large enough for that
    to be relevant.

    -- Richard
    --
    In the selection of the two characters immediately succeeding the numeral 9,
    consideration shall be given to their replacement by the graphics 10 and 11 to
    facilitate the adoption of the code in the sterling monetary area. (X3.4-1963)
    Richard Tobin, Jun 18, 2008
    #7
  8. Angelo Chen

    Boon Guest

    Boon wrote:

    > Angelo Chen wrote:
    >
    >> I have this need that the double contains a time
    >> interval in seconds since jan, 1, 2001, so the double will not have a
    >> fractional part, here is what I use now, but not so sure if this
    >> applies to all situation:
    >>
    >> double t; // t is a time interval set somewhere
    >>
    >> double d = t / 2.0;
    >> if (floor(d)*2.0 != t) {
    >> // odd number
    >> }

    >
    > I would write.
    >
    > #include <math.h>
    > if (lrint(t) & 1) /* odd */ else /* even */


    If it's legal for t to be larger than 2^31 then I'd use llrint.

    if (llrint(t) & 1) /* odd */ else /* even */
    Boon, Jun 18, 2008
    #8
  9. Angelo Chen

    Richard Bos Guest

    (Richard Tobin) wrote:

    > In article <>,
    > <> wrote:
    >
    > >> you are correct, I have this need that the double contains a time
    > >> interval in seconds since jan, 1, 2001, so the double will not have a
    > >> fractional part, ...

    >
    > >If there is no fractional part I would suggest you don't
    > >use double at all. Use some integer type like 'long'.

    >
    > On many systems, a double can accurately represent integers with
    > larger values than any integer type.


    _Can_, yes. After a computation or three there is no longer a guarantee
    that it _does_.

    Richard
    Richard Bos, Jun 18, 2008
    #9
  10. In article <4all.nl>,
    Richard Bos <> wrote:

    >> On many systems, a double can accurately represent integers with
    >> larger values than any integer type.


    >_Can_, yes. After a computation or three there is no longer a guarantee
    >that it _does_.


    If you restrict yourself to operations that produce integer results,
    they will be correct.

    -- Richard
    --
    In the selection of the two characters immediately succeeding the numeral 9,
    consideration shall be given to their replacement by the graphics 10 and 11 to
    facilitate the adoption of the code in the sterling monetary area. (X3.4-1963)
    Richard Tobin, Jun 18, 2008
    #10
  11. Angelo Chen

    Willem Guest

    Richard Tobin wrote:
    ) On many systems, a double can accurately represent integers with
    ) larger values than any integer type. (Of course, this is less true
    ) now with long long, but that's still not universally available.)
    ) And intervals of seconds can be quite large enough for that
    ) to be relevant.

    However, when floating point values get too large, then the ones position
    starts getting inaccurate which makes the even/odd question meaningless.


    SaSW, Willem
    --
    Disclaimer: I am in no way responsible for any of the statements
    made in the above text. For all I know I might be
    drugged or something..
    No I'm not paranoid. You all think I'm paranoid, don't you !
    #EOT
    Willem, Jun 18, 2008
    #11
  12. Angelo Chen

    Kaz Kylheku Guest

    On Jun 18, 1:31 am, wrote:
    > On 18 Jun., 09:47, Angelo Chen <> wrote:
    >
    > > Hi,

    >
    > > you are correct, I have this need that the double contains a time
    > > interval in seconds since jan, 1, 2001, so the double will not have a
    > > fractional part, ...

    >
    > If there is no fractional part I would suggest you don't
    > use double at all. Use some integer type like 'long'.


    Note that the ISO C function difftime returns double.
    Kaz Kylheku, Jun 18, 2008
    #12
  13. Angelo Chen

    Kaz Kylheku Guest

    On Jun 18, 12:47 am, Angelo Chen <> wrote:
    > Hi,
    >
    > you are correct, I have this need that the double contains a time
    > interval in seconds since jan, 1, 2001, so the double will not have a
    > fractional part, here is what I use now, but not so sure if this
    > applies to all situation:
    >
    > double t; // t is a time interval set somewhere
    >
    > double d = t / 2.0;
    > if (floor(d)*2.0 != t) {
    >   // odd number
    >
    > }


    You're looking for the fmod function. If d is a multiple of 2.0, then
    fmod(d, 2.0) should yield zero.

    It won't apply in situations where the floating-point numbers have
    exponents so large that the precision of the mantissa no longer
    stretches far enough to represent consecutive integers.
    Kaz Kylheku, Jun 18, 2008
    #13
  14. Angelo Chen

    Guest

    pete <> wrote:
    >
    > if (fmod(x, 1) == 0 && fmod(x, 2) != 0) {
    > puts("x is odd");
    > }


    Why not just fmod(x, 2.0) == 1.0 ?


    -- Larry Jones

    Another casualty of applied metaphysics. -- Hobbes
    , Jun 18, 2008
    #14
  15. Angelo Chen

    user923005 Guest

    On Jun 18, 12:19 am, Angelo Chen <> wrote:
    > hi,
    > any quick way to check if a double is an odd number or not? thanks


    1. I guess that you will be happier with long long in the long long
    run
    2. Did you consider leap seconds?
    http://maia.usno.navy.mil/ser7/tai-utc.dat
    user923005, Jun 18, 2008
    #15
  16. Angelo Chen

    rahul Guest

    On Jun 18, 3:05 pm, pete <> wrote:
    > Angelo Chen wrote:
    > > hi,
    > > any quick way to check if a double is an odd number or not? thanks

    >
    > if (fmod(x, 1) == 0 && fmod(x, 2) != 0) {
    > puts("x is odd");
    >
    > }
    >
    > --
    > pete


    But isn't fmod(x,1) 0 for any number? (Other than NaN and infinity;
    but they aren't numbers in the true sense)
    rahul, Jun 19, 2008
    #16
  17. Angelo Chen

    Daniel Pitts Guest

    rahul wrote:
    > On Jun 18, 3:05 pm, pete <> wrote:
    >> Angelo Chen wrote:
    >>> hi,
    >>> any quick way to check if a double is an odd number or not? thanks

    >> if (fmod(x, 1) == 0 && fmod(x, 2) != 0) {
    >> puts("x is odd");
    >>
    >> }
    >>
    >> --
    >> pete

    >
    > But isn't fmod(x,1) 0 for any number? (Other than NaN and infinity;
    > but they aren't numbers in the true sense)

    I would expect fmod(1.5, 1) == .5

    --
    Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>
    Daniel Pitts, Jun 19, 2008
    #17
  18. Angelo Chen

    Richard Bos Guest

    Kaz Kylheku <> wrote:

    > On Jun 18, 1:31=A0am, wrote:
    > > On 18 Jun., 09:47, Angelo Chen <> wrote:
    > >
    > > > you are correct, I have this need that the double contains a time
    > > > interval in seconds since jan, 1, 2001, so the double will not have a
    > > > fractional part, ...

    > >
    > > If there is no fractional part I would suggest you don't
    > > use double at all. Use some integer type like 'long'.

    >
    > Note that the ISO C function difftime returns double.


    True, but that double is not guaranteed to have no fractional part.

    Richard
    Richard Bos, Jun 19, 2008
    #18
  19. On Jun 18, 5:49 pm, wrote:

    > Why not just fmod(x, 2.0) == 1.0 ?


    That is almost correct, except that for example -11 is an odd number,
    and fmod (-11.0, 2.0) == -1.0. So you have to check whether the result
    of fmod (x, 2.0) is either 1.0 or -1.0. Checking for even numbers is
    easier, just check fmod (x, 2.0) == 0.0.
    christian.bau, Jun 20, 2008
    #19
  20. On Fri, 20 Jun 2008 15:26:20 -0700 (PDT), "christian.bau"
    <> wrote:

    >On Jun 18, 5:49 pm, wrote:
    >
    >> Why not just fmod(x, 2.0) == 1.0 ?

    >
    >That is almost correct, except that for example -11 is an odd number,
    >and fmod (-11.0, 2.0) == -1.0. So you have to check whether the result
    >of fmod (x, 2.0) is either 1.0 or -1.0. Checking for even numbers is
    >easier, just check fmod (x, 2.0) == 0.0.


    pow(10,25)+1 is an odd value. What will your fmod() expression
    evaluate to?


    Remove del for email
    Barry Schwarz, Jun 21, 2008
    #20
    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. Sydex
    Replies:
    12
    Views:
    6,452
    Victor Bazarov
    Feb 17, 2005
  2. Michael Speer

    Odd behavior with odd code

    Michael Speer, Feb 16, 2007, in forum: C Programming
    Replies:
    33
    Views:
    1,062
    Richard Heathfield
    Feb 18, 2007
  3. Klaas Vantournhout

    the fastest way to do an odd/even check?

    Klaas Vantournhout, Feb 1, 2007, in forum: C++
    Replies:
    6
    Views:
    400
    Sylvester Hesp
    Feb 2, 2007
  4. ctime double double check

    , Mar 8, 2007, in forum: C Programming
    Replies:
    11
    Views:
    645
    Keith Thompson
    Mar 9, 2007
  5. Peter
    Replies:
    9
    Views:
    400
    David Mark
    Jul 7, 2009
Loading...

Share This Page