最近读书《深入理解计算机系统》里第二章中的“Intel IA32 浮点运算”,发现其中给出的测试程序有些问题:
浮点数寄存器使用的是80位的扩展精度格式
float 类型使用的是32位精度格式
double 类型使用的是64位精度格式
书中给出的例子是
#include<stdio.h> double recip(int denom){ return 1.0/(double) denom; } void do_nothing(){} void test1(int denom){ double r1, r2; int t1, t2; r1 = recip(denom); r2 = recip(denom); t1 = r1 == r2; do_nothing(); t2 = r1 == r2; printf("test1 t1: r1 %f %c= r2 %f\n", r1, t1 ? '=' : '!', r2); printf("test1 t2: r1 %f %c= r2 %f\n", r1, t2 ? '=' : '!', r2); } main(){ test1(10); }
我的系统是ubuntu9.04
gcc 版本 4.3.3 (Ubuntu 4.3.3-5ubuntu4)
第一步:不带优化的编译
coffee@coffee-laptop:~$ gcc -o test test.c coffee@coffee-laptop:~$ ./test test1 t1: r1 0.100000 == r2 0.100000 test1 t2: