Re: test whether a double is even?

From: Niki Estner (niki.estner_at_cube.net)
Date: 11/25/03


Date: Tue, 25 Nov 2003 22:56:57 +0100


> On my machine an int is 32 bits, so I would lose precision by converting
> a 39 bit number to an int, would I not? (An unsigned long is also 32
bits.)

Err, yes and no: Your original question was about testing a number if it's
even - exactly the last bit will decide whether this is the case or not. Of
course you may loose precision casting a big number to int, short, or char,
but the result will still be even if the original number was (and vice
versa).

Niki

PS: if you need the precision for other purposes, and assuming you're using
in C/C++: there are 'long double' (80 bit double on a PC) and 'long long'
(64 bit integer on a PC) types.
And, if you want an even/odd test on a number you probably shouldn't use
floating point types - as the name says, the least significant bits may
"float" away - making every number big enough even...

"John Smith" <JSmith@mail.com> schrieb im Newsbeitrag
news:3FC3B418.7010703@mail.com...
> Corey Murtagh wrote:
>
> > If the stored value is always an integer, even though the storage type
> > is floating, just convert it to an int and check. Faster that way than
> > using fmod.
> >
>
> The numbers are pretty big. 224401164137 is typical, which is 39 bits.
> On my machine an int is 32 bits, so I would lose precision by converting
> a 39 bit number to an int, would I not? (An unsigned long is also 32
bits.)
>
> Which brings me to another question. Again, on my machine, a double is
> 64 bits. How many of those bits are available to represent a whole number?
>
>
>



Relevant Pages

  • Re: int Versus decimal
    ... INT only uses 4 bytes because it doesn't have the precision that DECIMAL ... How are you hoping to maintain the same precision when converting back ... > I have to keep time series on large amounts of numeric data (millions ...
    (microsoft.public.sqlserver.datawarehouse)
  • Re: Linux oddity
    ... > When converting a double to an int, ... > I'd expect on Linux: ... cases and 64 bit precision in the later case. ...
    (comp.lang.cpp)
  • Re: 12 hour clock and offset problem
    ... not violate precision in time based on precise longitude location. ... even more complex if adjustment is made including seconds. ... is lessened by use of the int() function within perl core. ...
    (comp.lang.perl.misc)
  • Re: double to int conversion yields strange results
    ... giving me two different values for the int. ... the question raised is not about the precision of the ... is done in extended precision. ... It is the conversion to 'double' before the conversion to 'int' that ...
    (comp.lang.c)
  • Re: [C] [newbie] problem reading from file
    ... why did you declare two variables in one statement here? ... note that it returns an int, ... actually I would consider converting all this inner loop to a for-loop ... that places each input character into number as long as there is still ...
    (alt.comp.lang.learn.c-cpp)