Re: Can a double always represent an int exactly?
"dandelion" <dandelion@mead ow.net> writes:[color=blue]
> "Fred Ma" <fma@doe.carlet on.ca> wrote in message[color=green]
>> dandelion wrote:[color=darkred]
>> >
>> > M_PI/M_PI seldomly equals 1.000000.[/color]
>>
>> I imagine that would depend on how division is implemented.[/color]
>
> 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.[/color]
I just tried this on a wide variety of systems; M_PI/M_PI compares
equal to 1.0 on all but one of them. (The exception was a Cray SV1.)
Here's the program I used:
#include <stdio.h>
#include <math.h>
int main(void)
{
double var_M_PI = M_PI;
double ratio = M_PI / M_PI;
double var_ratio = var_M_PI / var_M_PI;
printf("M_PI = %g\n", M_PI);
printf("var_M_P I = %g\n", var_M_PI);
printf("ratio = %g\n", ratio);
printf("ratio %s 1.0\n", ratio == 1.0 ? "==" : "!=");
printf("var_rat io = %g\n", var_ratio);
printf("var_rat io %s 1.0\n", var_ratio == 1.0 ? "==" : "!=");
return 0;
}
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.
--
Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
"dandelion" <dandelion@mead ow.net> writes:[color=blue]
> "Fred Ma" <fma@doe.carlet on.ca> wrote in message[color=green]
>> dandelion wrote:[color=darkred]
>> >
>> > M_PI/M_PI seldomly equals 1.000000.[/color]
>>
>> I imagine that would depend on how division is implemented.[/color]
>
> 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.[/color]
I just tried this on a wide variety of systems; M_PI/M_PI compares
equal to 1.0 on all but one of them. (The exception was a Cray SV1.)
Here's the program I used:
#include <stdio.h>
#include <math.h>
int main(void)
{
double var_M_PI = M_PI;
double ratio = M_PI / M_PI;
double var_ratio = var_M_PI / var_M_PI;
printf("M_PI = %g\n", M_PI);
printf("var_M_P I = %g\n", var_M_PI);
printf("ratio = %g\n", ratio);
printf("ratio %s 1.0\n", ratio == 1.0 ? "==" : "!=");
printf("var_rat io = %g\n", var_ratio);
printf("var_rat io %s 1.0\n", var_ratio == 1.0 ? "==" : "!=");
return 0;
}
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.
--
Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Comment