piUnderstanding Mathematical Formula in Java

Im just trying to get back into the way of programming in the Java language after having a rather large break so I decided to write a program that calculates PI to the users input number:

e.g. they enter 5, it would return 3.14159.

I've come across the Leibnez formula after a bit of searching but don't know how to implement in code. This seems to be a recurring problem so I'd like to rectify that. Anyone got any resources that demonstrate how to code mathematical formula?

For example, stuff that explains 1e6 and so on?


As a starting point you could use this code, it implements the sum notation as described here Leibniz formula for π

public class PI {
    public static void main( String[] args) {
        double PI4 = 1.0;
        for ( int i = 1 ; i <= 50 ; i++ ) {
            double nom = Math.pow( -1, i );
            double denom = 2 * i + 1;
            PI4 += nom / denom;
        System.out.println( "result=" + PI4 * 4 );


After 50 iterations the result is 3.1611986129870506

I recommend reading a good math tutorial online so you understand the math first. Then a good basic programming tutorial should do the job.

Stack Exchange's Math section is a good reference of math concepts as well.

Related Articles
  • I'm an Android Developer and as part of my next app I will need to evaluate a large variety of user created mathematical expressions and equations. I am looking for a good java library that is lightweight and can evaluate mathematical expressions usi
  • Im looking for a way to modify a binary byte value on Arduino. Because of the Hardware, its neccesarry, to split a two digit number into 2 4-bit. the code to set output is wire.write(byte, 0xFF) which sets all outputs on High. 0xFF = binary 1111 1111
  • I need to have a way to draw a mathematical formula in Windows Forms or WPF. Some "FormulaTextBox" control would be exactly what I need. I'm not asking about full implementation of LaTEX, but at least something beyond RichTextBox possibilities,
  • So, Leibniz's formula for pi is pi/4 = 1 - 1/3 + 1/5 - 1/7 + 1/9... I can't figure out how to use this formula in Java. I just need some help on how to incorporate the second part into it. I know I need a loop and I need the have a divisor variable,
  • I have created my own structures for representing mathematical graphs in Java. Now i need a library that supports creating a PDF file with that graph. Does anyone know a Java library that supports creating PDF files with graphs. By graph i mean somet
  • I am working on a statistical calculator... STILL... I am having some issues still.. I have worked most of the kinks out, however, I am still running into issues.. The median sorts but doesn't actually calculate what the median is. Also, I honestly h
  • Briefing Suppose we had a continuous piecewise polynomial, how can we express it with a single equation or formula, through any programming language (preferably Java)? In pure mathematics, I've found that it's possible to express the function in term
  • I've been reading a lot about Markov Decision Processes (using value iteration) lately but I simply can't get my head around them. I've found a lot of resources on the Internet / books, but they all use mathematical formulas that are way too complex
  • I've been looking at Smalltalk (VisualWorks) for the past couple of months - and the more I learn the more I'm impressed. However, I think I must be missing something as Smalltalk doesn't seem to be popular these days - and perhaps it never was. What
You Might Also Like