fzappaandthem fzappaandthem - 5 months ago 37
Java Question

java: Precision in double numbers

Been working with this code, from How to program in java 9th edition (Deitel), exercise 5.20...

Calculate the value of π from the infinite series.
π = 4 – 4/3 + 4/5 – 4/7 + 4/9 – 4/11 + …
Print a table that shows the value of π approximated by computing the first 200,000 terms of this series. How many terms do you have to use before you first get a value that begins with 3.14159?

the problem is that i get to the value of 3.14159 with this approximation 0.000005 which i don't think is that good... for instance it might say that the term 130657 is the first but it starts with 3.141585... trying to use 0.000001 as precision won't ever let me know that the number starts with 3.14159 even though i see in the output several numbers that start with 3.14159 ...

so, the book doesn't really explain Strings yet, except for String.format maybe... a solution like @compass was nice though but I rather using maths to solve this, I used Math.abs() because I thought it would help, using

if ( 3.14159 <= pi && pi < 3.14159 )


does not help,

problem is not solved yet, is there a way to know by accuracy with math operators when the variable pi starts with 3.14159?

import javax.swing.JOptionPane;

public class Pi {
public static void main (String args[]){
double pi = 4;
double fraccion = 3.0;
double cociente ;
for ( int i = 1 ; i <= 200000 ; i++ ){

cociente = 4.0/fraccion;

if ( i % 2 == 0 )
pi += cociente;
else
pi -= cociente;

System.out.printf("El valor de pi en el término %d, es : %.6f \n", i , pi);

if ( Math.abs( pi - 3.14159 ) <= 0.000005)
JOptionPane.showMessageDialog(null, String.format("Pi vale: %.5f este es el primer valor que empieza con 3.14159 en el término %d", pi, i));

fraccion += 2.0;

}

System.exit(0);
}
}

Answer

This works pretty good, remember that the author hasn't teach how to compare strings or anything like that yet, the code works ok now.

import javax.swing.JOptionPane;

public class Pi {
    public static void main (String args[]){
        double pi = 4;
        double fraccion = 3.0;
        double cociente ;
        int firstSixDigits;
        for ( int i = 1 ; i <= 200000 ; i++ ){          

            cociente = 4.0/fraccion;

            if ( i % 2 == 0 )
                pi += cociente;
            else
                pi -= cociente;

            System.out.printf("El valor de pi en el término %d, es : %.10f \n", i , pi);

            firstSixDigits = (int) ( pi * 100000.0 ) ;



            if ( firstSixDigits % 314159 == 0  )
                    JOptionPane.showMessageDialog(null, String.format("Pi vale: %.10f este es el primer valor que empieza con 3.14159 en el término %d", pi, i));

            fraccion += 2.0;

        }

        System.exit(0);
    }
}

... also, and to close this ...

Actually, the only thing to be corrected was the arguments I used (pi - 3.141595 < 0.000005 ) does the trick), using Math.abs was a great idea... because when, for example pi = 3.141593...

then pi - 3.141595 is equal to -0.000002 (not always, of course, let's asume the number ends right there, and there are no more decimals) ; Math.abs (-0.000002) = 0.000002 is < 0.000005 ... and for any value from 3.14159x with x = { 0 .. 9 } the value 'Math.abs (pi - 3.141595 ) < 0.000005' is true, so it always recognizes when pi starts with 3.14159.

Comments