# check if a double is odd or not

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

1. ### Angelo ChenGuest

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

Angelo Chen, Jun 18, 2008

2. ### rahulGuest

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

3. ### Angelo ChenGuest

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@
> "Usenet is a strange place" - dmr 29 July 1999

Angelo Chen, Jun 18, 2008
4. ### 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
>
> }

Here's how I'd do it:
double d = 12345.6789;
if((unsigned long)d & 1) /* odd */
else /* even */

, Jun 18, 2008
5. ### 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
6. ### BoonGuest

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
7. ### Richard TobinGuest

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
8. ### BoonGuest

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
9. ### Richard BosGuest

(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
10. ### Richard TobinGuest

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
11. ### WillemGuest

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
12. ### Kaz KylhekuGuest

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
13. ### Kaz KylhekuGuest

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
14. ### 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
15. ### user923005Guest

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
16. ### rahulGuest

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
17. ### Daniel PittsGuest

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
18. ### Richard BosGuest

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
19. ### christian.bauGuest

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
20. ### Barry SchwarzGuest

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