Sign in with
Sign up | Sign in
Your question

{JAVA} Calculating PI without using class.math

Last response: in Applications
August 17, 2012 3:15:45 PM

Is it possible to calculate the Number PI without using the java class math?

I found this formula to calculate PI.

I think i need some int arrays to save the numbers but actually i'm stumped.

Has anyone a hint?
a b L Programming
August 17, 2012 5:11:08 PM

That's not formula to calculate PI.. That's a formula for arcsine.

Shouldn't be that hard
  1. double Pi = 4;
  2. bool plus = 0;
  3. for (int i = 3; i < 10000000; i += 2)
  4. {
  5. if (plus)
  6. {
  7. Pi += 4.0 / i;
  8. }
  9. else
  10. {
  11. Pi -= 4.0 / i;
  12. }
  13. plus = !plus;
  14. }
August 18, 2012 11:35:32 AM

Is that C?
a b L Programming
August 18, 2012 12:39:14 PM

Does it matter? It would work on many languages. I tested it using Visual C++ compiler. I'm sure you can convert it to Java without any problems.
August 18, 2012 1:08:38 PM

I don't know what
"bool plus = 0;" means "


"plus = !plus;"
a b L Programming
August 18, 2012 1:23:09 PM

It's a flag which determines whether or not to do an add operation or a subtract operation, and it is flipped* each time either is performed so that the two alternate. For Java you'd have to initialise the boolean to false, not 0.

*The ! inverts the boolean result of whatever follows it. So if "plus" is true, it will return false and vice versa.
August 18, 2012 1:44:29 PM

Thanks. Should "i" be also a variable?
Because in Java you can't divide i if it is not declared as a variable.
a b L Programming
August 18, 2012 1:55:22 PM

It is already declared and initialised at the start of the for loop, and its scope is restricted to the loop as well.

I'm guessing this task is for some homework. I can't think of any common reason to want to manually calculate pi except because you are told that you must.
a b L Programming
August 18, 2012 5:46:41 PM

I'm not really familiar with Java syntax, but from what randomizer said bool plus = 0 equivalent in Java should be:

  1. boolean plus = false;

And as for i, it's declared in the loop at line 3:
  1. int i = 0;

Finally, operator ! flips the boolean value, if it was true, it becomes false, and if it was false, it becomes true.
August 27, 2012 5:55:26 PM


I was given the same task as ECSD, using the arcsine formula from the first post. Further info: when x=0.5 --> 6*arcsin(x) = Pi . That's how the formula might be incorporated. I'm completely new to Programming and I'm confused as to how to calculate to a power without using the Math.Class... I have the instructions to not use the Math.Class and to save the first 10000 digits of Pi in int-arrays.. any advice would be much appreciated :) 
a b L Programming
August 28, 2012 9:37:58 AM

Do you have instructions to what x degree you need to calculate? Like 1000? 10000?
August 28, 2012 11:59:16 AM

no, other than the 10000 digits of Pi there's nothing specifically specified..
a b L Programming
August 28, 2012 3:22:43 PM

I suppose you'll have to write your own operators: +, *, /, which take arrays as operands instead of numbers. That's the only way I see it would work.