Can a double always represent an int exactly?

?

=?ISO-8859-1?Q?=22Nils_O=2E_Sel=E5sdal=22?=

dandelion said:
----- Original Message -----
From: "Fred Ma" <[email protected]>
Newsgroups: comp.lang.c
Sent: Friday, October 22, 2004 2:03 PM
Subject: Re: Can a double always represent an int exactly?





Of course, that's why I wrote "seldomly". And which implementation would
return 1.000000, exactly? I'm curious. Try a few CPU's/FPU's and check the
results. I'll buy you a beer if
you find one.
This one does it for me.. is it the printf fscking up, or ..

#include <math.h>
#include <stdio.h>

float divide_me(float f){
return f/M_PI;
}

int main()
{

printf("%.26f\n",divide_me(M_PI));

return 0;
}
 
D

dandelion

Caveats: A moderately clever compiler could compute the value at
compilation time (I didn't check this, but I didn't use any
optimization options). And of course M_PI is non-standard.


Your beer is waiting... Nice and cold. You earned it, I picked a silly
example.
 
F

Fred Ma

Dik T. Winter said:
It is not straightforward to implement. Nevertheless, whenever the FPU
conforms to the IEEE standard the division *must* deliver the exact
answer if the quotient is representable. So on all systems using such
FPU's (and that is the majority at this moment) should deliver 1.0 when
confronted with a/a, in whatever way it is disguised. To get division
right is not straigthforward, but it is not so very difficult either.

That Keith Thompson found that it was not the case on a Cray SV1 is
entirely because that system has not an IEEE conforming floating point
system. (That machine does not have a divide instruction. It
calculates an approximation of the inverse of the denominator and
multiplies with the numerator, and one Newton iteration is performed.
Due to some quirks it may give an inexact result. If I remember
right, the smallest integral division that is inexact is 17.0/17.0.


Thanks for that. It's useful to know.

Fred
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top