2

My school give me an assignment to calculate pi.

The result should be :

Question 4
Accuracy set at : 1000

term               pi
1                  4
100                3.13159
200                3.13659
300                3.13826
400                ...
...                ...

The result in my program :

term               pi
1                  4
100                3
200                3
300                3
400                ...
...                ...

I guess that when I do (4 / denominator), the result will lose the decimal number although I have changed some declarations of data type from int to double. (Some websites tell me to do this.) Maybe I do it wrongly.

How can I deal with this problem?

The following is my program.

#include <iostream>
using namespace std;


class Four
{
private:
    int inputedAccuracy;
    double pi;
    int denominator;
    int doneTermCounter;
    double oneTerm;
    int negativeController;

public:
    double question4()
    {
        cout << "Accuracy set at : " ;
        cin >> inputedAccuracy;
        cout << endl;
        pi = 0.0;
        denominator = 1.0;
        doneTermCounter = 0;

        negativeController = 1;
        cout << "Term" << "            " << "pi" << endl;
        cout << "1  " << "             " << "4" << endl;


        for (inputedAccuracy; inputedAccuracy > 0; inputedAccuracy -= 100)
        {
            for (int doneTerm = 0; doneTerm < 100; doneTerm++)
            {
                pi = pi + (negativeController * 4 / denominator);
                negativeController *= -1;
                denominator += 2;
                doneTermCounter++;
            }
            if (doneTermCounter >= 10000)
                cout << doneTermCounter << "             " << pi << endl;
            else
                if (doneTermCounter >= 1000)
                    cout << doneTermCounter << "            " << pi << endl;
                else
                    cout << doneTermCounter << "             " << pi << endl;
        }
        return 0.0;
    }
};

Thank you for your attention!

2
  • The least intrusive fix is: Change the 4 in 'pi = pi + (negativeController * 4 / denominator);' to 4.0
    – user2249683
    Sep 29, 2013 at 11:52
  • You mean pi isn't equal to 3??? What about the Bible?
    – john
    Sep 29, 2013 at 12:00

4 Answers 4

4
pi = pi + (negativeController * 4 / denominator);

The (negativeController * 4 / denominator) expression results in an int because both negativeController and denominator are int. In other words, you're doing an integer division here which explains why you don't get the expected result.

Declare either (or both) of them as double to force a floating-point division.

1
  • The problem have been solved. Thank you for your attention. XD
    – Casper
    Sep 29, 2013 at 11:52
2

You should change :-

int denominator; to

double denominator;

See here

1
  • The problem have been solved. Thank you for your attention. XD
    – Casper
    Sep 29, 2013 at 17:00
2
pi = pi + (negativeController * 4 / denominator);

In this line, you have an integer division (because both operands of / are of type int), meaning that the fractional part of the division's result is discarded.

To use floating point division, at least one operand/side needs to be of type float or (long) double. The easiest way to achieve this would in this case be a change of 4 (a literal of type int) to 4.0 (a literal of type double):

Then, when calculating the result of *, negativeController will also be converted to double (usual arithmetic conversions), yielding a double as the left-hand side operand of / which in turn causes denominator (the rhs) to also be converted into a double and so on.

1
  • The problem have been solved. Thank you for your attention. XD
    – Casper
    Sep 29, 2013 at 17:13
1

I think changing negativeController and denominator to int would do the trick as the sub-expression is being evaluated on integers thus loosing precision.

3
  • The problem have been solved. Thank you for your attention. XD
    – Casper
    Sep 29, 2013 at 16:29
  • @KinLi I thought I would be rewarded with an upvote.
    – Saksham
    Sep 29, 2013 at 16:53
  • I really want to give you a voteup but "Vote Up requires 15 reputation."
    – Casper
    Sep 29, 2013 at 17:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.