Using a long double or just a double for calculati

2019-07-19 14:35发布

I'm calculating pi using a long winded formula. I'm trying to get more familiar with floating point numbers etc. I have a working program that uses doubles. The problem with my code is:

  1. If I use a double, pi is only accurate to the 7th decimal place. I can't get it to be any more accurate.
  2. If I use a long double, pi is accurate up to the 9th decimal place however the code takes much longer to run. If I check for precision for less than 0.00000001 using a long double, pi returns a value of 9.4246775. I assume that this is due to the long double.

My question is what is the most accurate variable type? How could I change my code to improve the precision of pi?

Here is my code:

#include <iomanip>
#include <cstdlib>
#include <iostream>
#include <cmath>

using namespace std;

int main()
{
 double arctan;
 double pi;
 double precision;
 double previous=0;
 int y=3;
 int loopcount=0;

   cout<<"Start\n";

   arctan=1-(pow(1,y)/y);

 do
   {
     y=y+2;
     arctan=arctan+(pow(1,y)/y);
     y=y+2;
     arctan=arctan-(pow(1,y)/y);

      pi=4*(arctan);

    //  cout<<"Pi is: ";
    //  cout<<setprecision(12)<<pi<<endl;

      precision=(pi*(pow(10,10)/10));

      loopcount++;

      if(precision-previous<0.000000001)
        break;

      previous=precision;
    }
  while(true);

  cout<<"Pi is:"<<endl;
       cout<<setprecision(11)<<pi<<endl;
  cout<<"Times looped:"<<endl;
       cout<<loopcount<<endl;

return 0;
}

3条回答
贼婆χ
2楼-- · 2019-07-19 14:40

Or, you could just hard-code the digits of PI and see what happens. ^_^

http://www.joyofpi.com/pi.html

查看更多
叛逆
3楼-- · 2019-07-19 14:46

The predefined floating-point type with the greatest precision is long double.

There are three predefined floating-point types:

  • float has at least 6 decimal digits of precision
  • double has at least 10, and at least as many as float
  • long double has at least 10, and at least as many as double

These are minimum requirements; any or all of these types could have more precision.

If you need more precision than long double can provide, you might look at GMP, which supports arbitrary precision (at considerable expense in speed and memory usage).

查看更多
欢心
4楼-- · 2019-07-19 14:58

You can get the max limits of doubles/long doubles from std::numeric_limits

#include <iostream>
#include <limits>

int main()
{
    std::cout << "     Double::digits10:  " << std::numeric_limits<double>::digits10 << "\n";
    std::cout << "Long Double::digits10:  " << std::numeric_limits<long double>::digits10 << "\n";
}

On my machine this gives:

     Double::digits10:  15
Long Double::digits10:  18

So I expect long double to be accurate to 18 digits.
The definition of this term can be found here:

http://www.cplusplus.com/reference/std/limits/numeric_limits/

Standard quote: 18.3.2 Numeric limits [limits]

Also Note: As the comment is way down in the above list:

That @sarnold is incorrect (though mysteriously he has two silly people up-voting his comment without checking) in his assertions on pow(). What he states is only applicable to C. C++ has overloads for the types because in C++ pow() is a template function. See: http://www.cplusplus.com/reference/clibrary/cmath/pow/ in the standard at 26.4.7 complex value operations [complex.value.ops]

查看更多
登录 后发表回答