M
mathog
This problem is probably related to this thread:
https://groups.google.com/forum/#!topic/comp.lang.c/K10prwCtRV4
This program:
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#define MY_PI 3.14159265358979323846
int main(void){
double dang = MY_PI /2.0;
printf("dang:%le\n",dang);
double dresult = cos(dang);
printf("dresult:%le\n",dresult);
exit(EXIT_SUCCESS);
}
compiled with:
gcc -Wall -std=c99 -o problem problem.c -lm
on a linux gcc 4.7.2 system emits:
(A)
dang:1.570796e+00
dresult:6.123234e-17
compiled the same way on a gcc 4.6.3 system emits:
(B)
dang:1.570796e+00
dresult:6.123032e-17
but compiled with -O2 on the same system emits (A), like the first
system. Thinking it might be an "excess precision" problem I put
"volatile" in front of "double dang" and tested again with -O0 and -O2
on the 4.6.3 system. That made the result consistent on the 4.6.3
system, but it was always (B)!
It would be a big help if this calculation was always the _same_ value,
at least on such closely related systems, because when that is not true
"diff" cannot be used to easily detect changes on the much, much larger
binary file this example was extracted from.
This particular issue could be papered over with something like:
double dresult = cos(dang);
if(dresult < 1.0e-15 && dresult > -1.0e-15)dresult=0.0;
but presumably there are similar issues at 1.0e-14 too.
The two test systems are both generating 32 bit x86 binaries and are two
different linux distros running on AMD chips (an Opteron and an Athlon X2).
Ideas?
Thanks,
David Mathog
https://groups.google.com/forum/#!topic/comp.lang.c/K10prwCtRV4
This program:
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#define MY_PI 3.14159265358979323846
int main(void){
double dang = MY_PI /2.0;
printf("dang:%le\n",dang);
double dresult = cos(dang);
printf("dresult:%le\n",dresult);
exit(EXIT_SUCCESS);
}
compiled with:
gcc -Wall -std=c99 -o problem problem.c -lm
on a linux gcc 4.7.2 system emits:
(A)
dang:1.570796e+00
dresult:6.123234e-17
compiled the same way on a gcc 4.6.3 system emits:
(B)
dang:1.570796e+00
dresult:6.123032e-17
but compiled with -O2 on the same system emits (A), like the first
system. Thinking it might be an "excess precision" problem I put
"volatile" in front of "double dang" and tested again with -O0 and -O2
on the 4.6.3 system. That made the result consistent on the 4.6.3
system, but it was always (B)!
It would be a big help if this calculation was always the _same_ value,
at least on such closely related systems, because when that is not true
"diff" cannot be used to easily detect changes on the much, much larger
binary file this example was extracted from.
This particular issue could be papered over with something like:
double dresult = cos(dang);
if(dresult < 1.0e-15 && dresult > -1.0e-15)dresult=0.0;
but presumably there are similar issues at 1.0e-14 too.
The two test systems are both generating 32 bit x86 binaries and are two
different linux distros running on AMD chips (an Opteron and an Athlon X2).
Ideas?
Thanks,
David Mathog