java - Math line only running once in a loop -



java - Math line only running once in a loop -

this first interaction site, have heard things , hope can find reply looking for. learning java , using eclipse ide in computer science class @ high school , came across problem neither teacher or can solve. here instructions.

"the high german mathematician gottfriedleibniz developed follow method approximate value of π.

π/4 = 1 - 1/3 + 1/5 - 1/7 + ...

write programme allows user specify number if iterations used in approximation , displays resulting value."

now code.

import java.util.scanner; public class gottfriedleibnizpi { public static void main(string[] args) { scanner reader = new scanner(system.in); system.out.print("enter how many iterations want go to: "); int iterations = reader.nextint(); double pi = 0; if (iterations >= 0) { (int count = 0; count <= iterations - 1; count++) { system.out.println("pi: " + pi + " count: " + count); //debug statement show pi , count before running code if (count % 2 == 0) { pi = pi + (1 / (1 + (2 * count))); // first run. starts @ pi + 1 , every other loop adds 1/(1+2n) } else { pi = pi - (1 / (1 + (2 * count))); // first run. starts @ pi - 1/3 , every other loop subtracts 1/(1+2n) } system.out.println("pi: " + pi + " count: " + count + "\n"); //debug statement show pi , count after running code } pi = pi * 4; //obtains true value of pi system.out.println("the value of pi after " + iterations + " iterations " + pi); } else { system.out.println("please come in non-negative number"); } } }

here output the debugging statements if come in 5 @ prompt.

come in how many iterations want go to: 5 pi: 0.0 count: 0 pi: 1.0 count: 0 pi: 1.0 count: 1 pi: 1.0 count: 1 pi: 1.0 count: 2 pi: 1.0 count: 2 pi: 1.0 count: 3 pi: 1.0 count: 3 pi: 1.0 count: 4 pi: 1.0 count: 4

the value of pi after 5 iterations 4.0

my math says reply should 3.3396... math in loop not appear run more once. have not found on here close problem, know wrong?

the problem seems roundoff caused integer division. seek replacing relevant part of code this:

if(count % 2 == 0){ pi = pi + (1 / (1 + (2.0 * count))); // first run. starts @ pi + 1 , every other loop adds 1/(1+2n) } else{ pi = pi - (1 / (1 + (2.0 * count))); // first run. starts @ pi - 1/3 , every other loop subtracts 1/(1+2n) }

since "1" in numerator integer, , "(1 + (2*count))" in denominator integer, integer partition truncates remainder division. since denominator in case going greater or equal 1, 1/(positive integer greater 1) result in 0 in integer division. adding decimal 1 of integers java treat double , not perform integer division.

so actually, there no problem execution of lines. when run code above changes same reply given teacher.

the value of pi after 5 iterations 3.3396825396825403

java eclipse math for-loop floating-point

Comments

Popular posts from this blog

How do I check if an insert was successful with MySQLdb in Python? -

delphi - blogger via idHTTP : error 400 bad request -

postgresql - ERROR: operator is not unique: unknown + unknown -