P
pcauchon
When I try to run this code on my machine (iMac with MacOS 10.5), I
get very strange results. I am using this compiler:
i686-apple-darwin9-gcc-4.0.1 (GCC) 4.0.1 (Apple Inc. build 5465)
The first program loop gives the results I expect (the value of 2**x
when x is smaller than -1024 should be smaller than 10**-308).
However the second loop gives values that seem to be corrupted
somehow. Is it specific to iMac? Is there any way to detect when the
returned value is corrupted?
Many thanks
Philippe
---- begin code ----
#include <math.h>
#include <stdio.h>
int main(int argc, const char * argv[]) {
double x, y;
printf("first loop\n");
for (x = 2040; x < 2060; ++x) {
y = exp2(-x);
printf("%.4f %.4f\n", x, y);
}
printf("second loop\n");
for (x = 2040.1313; x < 2060; ++x) {
y = exp2(-x);
printf("%.4f %.4f\n", x, y);
}
return 0;
}
--- end code ----
Program output on my machine:
first loop
2040.0000 0.0000
2041.0000 0.0000
2042.0000 0.0000
2043.0000 0.0000
2044.0000 0.0000
2045.0000 0.0000
2046.0000 0.0000
2047.0000 0.0000
2048.0000 0.0000
2049.0000 0.0000
2050.0000 0.0000
2051.0000 0.0000
2052.0000 0.0000
2053.0000 0.0000
2054.0000 0.0000
2055.0000 0.0000
2056.0000 0.0000
2057.0000 0.0000
2058.0000 0.0000
2059.0000 0.0000
second loop
2040.1313 0.0000
2041.1313 0.0000
2042.1313 0.0000
2043.1313 0.0000
2044.1313 0.0000
2045.1313 -3.6482
2046.1313 -2.0000
2047.1313 -2.0000
2048.1313 -2.0000
2049.1313 -2.0000
2050.1313 -2.0000
2051.1313 -233.7301
2052.1313 -467.4603
2053.1313 -934.9206
2054.1313 -1869.8412
2055.1313 -3739.6823
2056.1313 -7479.3646
2057.1313 -14958.7292
2058.1313 -29917.4584
2059.1313 -59834.9169
get very strange results. I am using this compiler:
i686-apple-darwin9-gcc-4.0.1 (GCC) 4.0.1 (Apple Inc. build 5465)
The first program loop gives the results I expect (the value of 2**x
when x is smaller than -1024 should be smaller than 10**-308).
However the second loop gives values that seem to be corrupted
somehow. Is it specific to iMac? Is there any way to detect when the
returned value is corrupted?
Many thanks
Philippe
---- begin code ----
#include <math.h>
#include <stdio.h>
int main(int argc, const char * argv[]) {
double x, y;
printf("first loop\n");
for (x = 2040; x < 2060; ++x) {
y = exp2(-x);
printf("%.4f %.4f\n", x, y);
}
printf("second loop\n");
for (x = 2040.1313; x < 2060; ++x) {
y = exp2(-x);
printf("%.4f %.4f\n", x, y);
}
return 0;
}
--- end code ----
Program output on my machine:
first loop
2040.0000 0.0000
2041.0000 0.0000
2042.0000 0.0000
2043.0000 0.0000
2044.0000 0.0000
2045.0000 0.0000
2046.0000 0.0000
2047.0000 0.0000
2048.0000 0.0000
2049.0000 0.0000
2050.0000 0.0000
2051.0000 0.0000
2052.0000 0.0000
2053.0000 0.0000
2054.0000 0.0000
2055.0000 0.0000
2056.0000 0.0000
2057.0000 0.0000
2058.0000 0.0000
2059.0000 0.0000
second loop
2040.1313 0.0000
2041.1313 0.0000
2042.1313 0.0000
2043.1313 0.0000
2044.1313 0.0000
2045.1313 -3.6482
2046.1313 -2.0000
2047.1313 -2.0000
2048.1313 -2.0000
2049.1313 -2.0000
2050.1313 -2.0000
2051.1313 -233.7301
2052.1313 -467.4603
2053.1313 -934.9206
2054.1313 -1869.8412
2055.1313 -3739.6823
2056.1313 -7479.3646
2057.1313 -14958.7292
2058.1313 -29917.4584
2059.1313 -59834.9169