check if a double is odd or not

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Angelo Chen

    check if a double is odd or not

    hi,
    any quick way to check if a double is an odd number or not? thanks
  • Richard Heathfield

    #2
    Re: check if a double is odd or not

    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

    Comment

    • rahul

      #3
      Re: check if a double is odd or not

      On Jun 18, 12:19 pm, Angelo Chen <angelochen...@ gmail.comwrote:
      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 */
      }

      Comment

      • Angelo Chen

        #4
        Re: check if a double is odd or not

        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 <r...@see.sig.i nvalidwrote:
        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

        Comment

        • Richard Heathfield

          #5
          Re: check if a double is odd or not

          Angelo Chen said:
          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,
          int is_double_intpa rt_odd(double d)
          {
          unsigned long n = d;
          return n & 1;
          }

          <snip>

          --
          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

          Comment

          • vippstar@gmail.com

            #6
            Re: check if a double is odd or not

            On Jun 18, 10:47 am, Angelo Chen <angelochen...@ gmail.comwrote:
            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 */

            Comment

            • thomas.mertes@gmx.at

              #7
              Re: check if a double is odd or not

              On 18 Jun., 09:47, Angelo Chen <angelochen...@ gmail.comwrote:
              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.

              Comment

              • Richard Tobin

                #8
                Re: check if a double is odd or not

                In article <298ae571-b936-4244-b122-da6274fb3e00@m4 4g2000hsc.googl egroups.com>,
                <thomas.mertes@ gmx.atwrote:
                >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)

                Comment

                • Boon

                  #9
                  Re: check if a double is odd or not

                  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 */

                  Comment

                  • Boon

                    #10
                    Re: check if a double is odd or not

                    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 */

                    Comment

                    • pete

                      #11
                      Re: check if a double is odd or not

                      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

                      Comment

                      • Richard Bos

                        #12
                        Re: check if a double is odd or not

                        richard@cogsci. ed.ac.uk (Richard Tobin) wrote:
                        In article <298ae571-b936-4244-b122-da6274fb3e00@m4 4g2000hsc.googl egroups.com>,
                        <thomas.mertes@ gmx.atwrote:
                        >
                        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

                        Comment

                        • Richard Tobin

                          #13
                          Re: check if a double is odd or not

                          In article <4858fcf3.44418 4856@news.xs4al l.nl>,
                          Richard Bos <rlb@hoekstra-uitgeverij.nlwr ote:
                          >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)

                          Comment

                          • Willem

                            #14
                            Re: check if a double is odd or not

                            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

                            Comment

                            • Kaz Kylheku

                              #15
                              Re: check if a double is odd or not

                              On Jun 18, 1:31 am, thomas.mer...@g mx.at wrote:
                              On 18 Jun., 09:47, Angelo Chen <angelochen...@ gmail.comwrote:
                              >
                              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.

                              Comment

                              Working...