Velocity Reviews > check if a double is odd or not

# check if a double is odd or not

Angelo Chen
Guest
Posts: n/a

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

rahul
Guest
Posts: n/a

 06-18-2008
On Jun 18, 12:19 pm, Angelo Chen <(E-Mail Removed)> 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 */
}

Angelo Chen
Guest
Posts: n/a

 06-18-2008
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 <(E-Mail Removed)> 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

vippstar@gmail.com
Guest
Posts: n/a

 06-18-2008
On Jun 18, 10:47 am, Angelo Chen <(E-Mail Removed)> 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 */

thomas.mertes@gmx.at
Guest
Posts: n/a

 06-18-2008
On 18 Jun., 09:47, Angelo Chen <(E-Mail Removed)> 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.

Boon
Guest
Posts: n/a

 06-18-2008
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 */

Richard Tobin
Guest
Posts: n/a

 06-18-2008
In article <(E-Mail Removed)>,
<(E-Mail Removed)> 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)

Boon
Guest
Posts: n/a

 06-18-2008
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 */

Richard Bos
Guest
Posts: n/a

 06-18-2008
http://www.velocityreviews.com/forums/(E-Mail Removed) (Richard Tobin) wrote:

> In article <(E-Mail Removed)>,
> <(E-Mail Removed)> 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 Tobin
Guest
Posts: n/a

 06-18-2008
In article <(E-Mail Removed)4all.nl>,
Richard Bos <(E-Mail Removed)> 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)