Double multiplication differs between compile time and runtime in 32 bit platform

前端 未结 3 1976
太阳男子
太阳男子 2021-02-12 18:00

I\'m compiling and running the following program in 32 and 64 bit platforms:

int main()
{
  double y = 8.34214e08;
  double z = 1.25823e45;

  return y * z == 8.         


        
3条回答
  •  执念已碎
    2021-02-12 18:53

    Floating point calculations done at compile time often occur at a higher precision than double uses at run time. Also C may perform run-time intermediate double calculations at the higher long double precision. Either explain your inequality. See FLT_EVAL_METHOD for details.

      volatile double y = 8.34214e08;
      volatile double z = 1.25823e45;
      volatile double yz = 8.34214e08 * 1.25823e45;
      printf("%.20e\n", y);
      printf("%.20e\n", z);
      printf("%.20e\n", yz);
      printf("%.20Le\n", (long double) y*z);
      printf("%.20Le\n", (long double) 8.34214e08 * 1.25823e45);
    
    8.34214000000000000000e+08
    1.25822999999999992531e+45
    // 3 different products!
    1.04963308121999993395e+54
    1.04963308121999993769e+54
    1.04963308122000000000e+54
    

    Your results may slightly differ.

提交回复
热议问题