#include <math.h>
#include <stdio.h>
int main() {
printf("%.16lf\n",exp(1.0));
}
I've tried this program on 9 different hardware/software combinations,
using both the GNU and native compilers on AIX, HP-UX (PA-RISC),
Linux, Solaris 10 (SPARC), Open Solaris (x86). All except Sun Studio
on SPARC give a correctly rounded result of 2.7182818284590451, but
Sun Studio on SPARC gives a different answer of 2.7182818284590455.
I'd like to know what IBM's compiler on AIX gives, if anyone has a
minute and could compile it.
Dave
VAC 6, on AIX 5.1:
$ cc -o davidkirkby -lm davidkirkby.c
$ davidkirkby
2.7182818284590451
12:38 $ pwd
/home/simon/src/6-line
12:38 $ ls
doit.c
12:38 $ cat doit.c
#include <math.h>
#include <stdio.h>
int main() {
printf("%.16lf\n",exp(1.0));
}
12:38 $ xlc -qversion
IBM XL C/C++ Enterprise Edition V8.0 for AIX
Version: 08.00.0000.0000
12:38 $ CC=xlc make CFLAGS=-lm doit
xlc -lm doit.c -o doit
12:38 $ ./doit
2.7182818284590451
12:40 $ uname -vr
2 5
12:40 $
Thank you both for computing this. Solaris on SPARC gives a different
value as you can see if Sun Studio is used. In fact, it gives a
different value with gcc if a slightly more complex program is
compiled. It would appear that in this case, gcc can inline this, so
does not need to make a call to the maths library.
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char **argv) {
double x = 1.0;
if (argc>1)
x = atof(argv[1]);
printf("%.16lf\n",exp(x));
}
The above prints the a different value on SPARC to all other systems
tried.
Whether this is a bug in Sun's maths library is open to opinion. It
may be that Sun do not feel it worth spending the extra processor
cycles to get a more accurate result, when subsequent computations are
later performed on a processor using only 64 bits, where the precision
will soon be lost. On x86 the situation is different, because the
floating point processor uses 80 bits internally. I've no idea where
processors used on AIX and HP-UX fit in. Both give a more accurate
result than SPARC.
Anyway, thank you both for your help. It was much appreciated.
Dave