Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7673347
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T16:29:29+00:00 2026-05-31T16:29:29+00:00

I’m calculating pi using a long winded formula. I’m trying to get more familiar

  • 0

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;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T16:29:30+00:00Added an answer on May 31, 2026 at 4:29 pm

    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]

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have thousands of HTML files to process using Groovy/Java and I need to
I am trying to loop through a bunch of documents I have to put
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I am trying to understand how to use SyndicationItem to display feed which is
I have a jquery bug and I've been looking for hours now, I can't
Basically, what I'm trying to create is a page of div tags, each has
this is what i have right now Drawing an RSS feed into the php,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.