C++ calculating more precise than double or long double

后端 未结 2 646
有刺的猬
有刺的猬 2020-12-01 18:47

I\'m teaching myself C++ and on this practice question it asks to write code that can calculate PI to >30 digits. I learned that double / long double are both 16 digits prec

2条回答
  •  孤城傲影
    2020-12-01 19:15

    You can use quad math, builtin type __float128 and q/Q suffixes in GCC/clang.

    #include 
    
    #include 
    
    int main ()
    {
      __float128 x = strtoflt128("1234567891234567891234567891234566", nullptr);
      auto y = 1.0q;
      printf("%.Qf", x + y); // there is quadmath_snprintf, but this also works fine
      return 0;
    }
    

提交回复
热议问题