Re: double cast to int reliable?
- From: jameskuyper <jameskuyper@xxxxxxxxxxx>
- Date: Tue, 13 Jan 2009 16:32:16 -0800 (PST)
jacob navia wrote:
spasmous wrote:
In the following code:
int i,j;
double d;
i = 1; /* or any integer */
d = (double)i;
j = (int)d;
Is there any chance that "i" will not equal "j" due to the double
being stored inexactly?
If int is 32 bits no.
if int is more than 53 bits, yes definitely.
A IEEE double can hold 53 bits of precision
(without the exponent), so 32 bits will easily fit,
but not 64 or 55
Unless an implementation chooses to pre-define the macro
__STDC_IEC_559__, the C standard imposes no obligation that double be
an IEEE type. The standard allows DBL_EPSILON to be as high as 1e-9.
Assuming FLT_RADIX==2, this allows for as few as 31 bits in the
significand, meaning that numbers as low as 2^32+1 might not be
exactly representable in double. int32_t is safe, but nothing larger
is.
And before you ask - yes, there are real-world programs that have to
deal with non-IEEE floating point formats. For instance, a program I
had to write recently forced me to become familiar with MIL-STD-1750A,
which specifies 32-bit and 48-bit floating point formats that don't
match IEEE requirements, but do meet the requirements of the C
standard. See <http://en.wikipedia.org/wiki/MIL-STD-1750A> for more
details. A link from that page leads to <http://www.xgc.com/products/
gcc-1750/gcc-1750.html>, a C compiler that supports those formats.
.
- References:
- double cast to int reliable?
- From: spasmous
- Re: double cast to int reliable?
- From: jacob navia
- double cast to int reliable?
- Prev by Date: Re: Are ++ and -- operators really more efficient
- Next by Date: Re: C vs. C++
- Previous by thread: Re: double cast to int reliable?
- Next by thread: Re: double cast to int reliable?
- Index(es):
Relevant Pages
|