Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > check if a double is odd or not

Reply
Thread Tools

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
 
Reply With Quote
 
 
 
 
rahul
Guest
Posts: n/a
 
      06-18-2008
On Jun 18, 12:19 pm, Angelo Chen <angelochen...@gmail.com> 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 */
}
 
Reply With Quote
 
 
 
 
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 <r...@see.sig.invalid> 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@
> Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
> "Usenet is a strange place" - dmr 29 July 1999


 
Reply With Quote
 
vippstar@gmail.com
Guest
Posts: n/a
 
      06-18-2008
On Jun 18, 10:47 am, Angelo Chen <angelochen...@gmail.com> 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
>
> }

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 */
 
Reply With Quote
 
thomas.mertes@gmx.at
Guest
Posts: n/a
 
      06-18-2008
On 18 Jun., 09:47, Angelo Chen <angelochen...@gmail.com> 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.
 
Reply With Quote
 
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 */
 
Reply With Quote
 
Richard Tobin
Guest
Posts: n/a
 
      06-18-2008
In article <298ae571-b936-4244-b122->,
<> 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)
 
Reply With Quote
 
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 */
 
Reply With Quote
 
Richard Bos
Guest
Posts: n/a
 
      06-18-2008
(Richard Tobin) wrote:

> In article <298ae571-b936-4244-b122->,
> <> 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
 
Reply With Quote
 
Richard Tobin
Guest
Posts: n/a
 
      06-18-2008
In article <>,
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)
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
odd output for an epson 3000 nozzle check cruzer Digital Photography 7 09-22-2007 02:43 AM
ctime double double check aisling.cronin@gmail.com C Programming 11 03-09-2007 10:39 PM
Odd behavior with odd code Michael Speer C Programming 33 02-18-2007 07:31 AM
the fastest way to do an odd/even check? Klaas Vantournhout C++ 6 02-02-2007 12:08 AM
cannot convert parameter from 'double (double)' to 'double (__cdecl *)(double)' error Sydex C++ 12 02-17-2005 06:30 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57