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

  • SEARCH
  • Home
  • 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 6100711
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T13:25:02+00:00 2026-05-23T13:25:02+00:00

I have just made a program which calculates pi. However, even with 10 million

  • 0

I have just made a program which calculates pi. However, even with 10 million iterations my result is kinda off. I get 3.1415927535897831, whereas already early on is it wrong. It is supposed to be 3.141592653589793238…

So my question is: What is the required amount of iterations to get at least an accurate answer all the way up to 10^-16

Here is my code if anyone is interested:

#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
long double pi = 4.0;
long double tempPi;
for (int i = 1, j = 3; i <= 10000000; i++, j+=2)
{
    tempPi = static_cast<double>(4)/j;
    if (i%2 != 0)
    {
            pi -= tempPi;
    }
    else if (i%2 == 0)
    {
         pi += tempPi;
    }
}        
cout << "Pi has the value of: " << setprecision(16) << fixed << pi << endl;
system("pause");
return 0;

}

Any performance related tips would also be appreciated.

  • 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-23T13:25:02+00:00Added an answer on May 23, 2026 at 1:25 pm

    You are using the Leibniz series, which is very, very slow to converge. In an alternating series such as the one you are using, the first omitted term provides a good estimate of the error in the estimate. Your first omitted term is 4/2000005, so you should expect less than six significant digits of precision here.

    Note well: Rounding errors, use of double precision numbers has nothing to do with the lack of precision here. The sole factor is that you are using a crappy algorithm.

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

Sidebar

Related Questions

I have made a new windows service which works fine using barebone code (just
I have made a program in Java which is used to collect medical data
I have made a program in Java that calculates powers of two, but it
I have just ported several of our home-made Outlook COM-addins from Delphi 2007 to
I have a package that I just made and I have an old-mode that
I've just create a new MVC project, and have made no changes at all,
Have just started using Visual Studio Professional's built-in unit testing features, which as I
I have just inherited a server application, however it seems that the only copy
I'm a beginner in programming. I've just made a program called Guessing Game. And
I have a program which automatically launches a phone call when program is started.

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.