check if a double is odd or not

R

rahul

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

Angelo Chen

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

vippstar

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

thomas.mertes

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

Boon

Angelo said:
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 */
 
R

Richard Tobin

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, ...
[/QUOTE]
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
 
B

Boon

Boon said:
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 */
 
R

Richard Bos

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.[/QUOTE]

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

Richard
 
R

Richard Tobin

On many systems, a double can accurately represent integers with
larger values than any integer type.
[/QUOTE]
_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
 
W

Willem

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
 
K

Kaz Kylheku

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

Kaz Kylheku

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

lawrence.jones

pete said:
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
 
R

rahul

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

}

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

Daniel Pitts

rahul said:
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
 
C

christian.bau

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

Barry Schwarz

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,012
Latest member
RoxanneDzm

Latest Threads

Top