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
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 */
}
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
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
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 */
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.
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)
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 */
>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.
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_.
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)
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
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