A
Angelo Chen
hi,
any quick way to check if a double is an odd number or not? thanks
any quick way to check if a double is an odd number or not? thanks
hi,
any quick way to check if a double is an odd number or not? thanks
Please do not top-post.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
}
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, ...
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
}
[/QUOTE]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'.
Boon said:I would write.
#include <math.h>
if (lrint(t) & 1) /* odd */ else /* even */
If there is no fractional part I would suggest you don't
use double at all. Use some integer type like 'long'.
[/QUOTE]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 there is no fractional part I would suggest you don't
use double at all. Use some integer type like 'long'.
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
}
pete said:if (fmod(x, 1) == 0 && fmod(x, 2) != 0) {
puts("x is odd");
}
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");
}
I would expect fmod(1.5, 1) == .5rahul 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)
Kaz Kylheku said:Note that the ISO C function difftime returns double.
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.
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.