Hi everyone,

I am taking a C++ 100 level beginners course and we just got this assgnment that I am stuck on. Here is the assginment description:

We are going to just compute the first 15 or so digits of π using doubles.

In any case, to compute the value of π, we need a formula for it. Let’s use the standard formula,

π = 16arctan(1/5) - 4arctan(1/239)


This is all well and good, but in order to compute π, we need to compute arctan now. We could try to use a standard function from cmath, but let’s write our own function to compute arctan. Mathematicians tell us an easy formula to use is

arctan(x) = x - x^3/3 + x^5/5 - x^7/7 + x^9/9 - ...

The only parts that might need some thinking are the alternating signs, and the fact that the exponents and denominators both increment by 2.

Here is a sample output from the program:

Pi = 3.14159265358

Press any key to continue

In class, the professor told us to compare it to the programs we worked on to compute sin(x) and e^x. I have been referring to those programs I have but I still can't seem to solve it. Here is what I have so far:

#include <iostream>
using namespace std;

int main()
{
	double denom;
	double sign;
	double power;
	double old;
	double arctan;
	double x;
	double y;
	int count, i;

	arctan = 0;
	old = 1;
	count = 1;
	sign = 1;
	x = 1/5;
	y = 1/239;

	while (old != arctan)
	{
		old = arctan;

		denom = 1;
		for (i = 2; i<=count; i++)
			denom += 2;

		power = 1;
		for (i = 1; i <=count; i++)
			power += 2;
	

		arctan = arctan + sign*power/denom;
		sign = -sign;
		count += 2;


	}


	cout << "Pi = " << arctan << endl;

	return 0;
}

1) Why calculate the denom and power values from scratch each time? Just add 2 each time through the loop.

2) How does sign*power/denom; equate to x^5/5 ? Where's your x? Where's x^5?

1) Why calculate the denom and power values from scratch each time? Just add 2 each time through the loop.

2) How does sign*power/denom; equate to x^5/5 ? Where's your x? Where's x^5?

In response to 1), isn't that what I'm doing by saying denom += 2 and power +=2 on lines 32 and 37?

In response to 2), that is just what I put since I was basing it off of a program I have that calculates sin(x). I am just confused as to how to get it to work for arctan and then using that calculation to derive pi.

In response to 1), isn't that what I'm doing by saying denom += 2 and power +=2 on lines 32 and 37?

Well, technically, yes. But when you need denom to be 5 you calculate 1,3,5. Then when you need 15 you calculate 1,3,5,7,9,11,13,15. But if you need 15, wasn't the last calculation 13? So rather than calculating all those other values, why not just add 2? And since power is the identical value, why calculate it at all? You've already got the value in denom.

In response to 2), that is just what I put since I was basing it off of a program I have that calculates sin(x). I am just confused as to how to get it to work for arctan and then using that calculation to derive pi.

My question stands. You have the equation. Make your code use that equation. You can certainly base it off another program, but you still have to change it to the correct equation.

How do I make this code use that equation? What would you suggest?

How do I make this code use that equation? What would you suggest?

Since this is your very first post, you don't. You come with your own technique and write your own code, and don't hijack arguav74's thread. You don't steal someone else's code for your own homework.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.