{JAVA} Calculating PI without using class.math

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?
12 answers Last reply
More about java calculating class math
  1. That's not formula to calculate PI.. That's a formula for arcsine.

    Shouldn't be that hard
    double Pi = 4;
    bool plus = 0;
    for (int i = 3; i < 10000000; i += 2)
        if (plus)
            Pi += 4.0 / i;
            Pi -= 4.0 / i;
        plus = !plus;
  2. Is that C?
  3. 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.
  4. I don't know what
    "bool plus = 0;" means "


    "plus = !plus;"
  5. 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.
  6. Thanks. Should "i" be also a variable?
    Because in Java you can't divide i if it is not declared as a variable.
  7. 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.
  8. I'm not really familiar with Java syntax, but from what randomizer said bool plus = 0 equivalent in Java should be:

    boolean plus = false;

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

    Finally, operator ! flips the boolean value, if it was true, it becomes false, and if it was false, it becomes true.
  9. Hi,

    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 :)
  10. Do you have instructions to what x degree you need to calculate? Like 1000? 10000?
  11. no, other than the 10000 digits of Pi there's nothing specifically specified..
  12. 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.
Ask a new question

Read More

Programming Formula Java Apps