513Chapter 27MathematicsConverting code to arbitrary-precisionLet s see what it s (Crystaltech web hosting)

513Chapter 27MathematicsConverting code to arbitrary-precisionLet s see what it s like to take an existing piece of mathematical code and retrofit it to use thearbitrary-precision functions. The following function approximates pi, using the series approximation: sqrt ( 12 - (12/22) + (12/32) - (12/42) + (12/52) - …) (As we ll see, this series does not converge fast enough for our purposes, but it has the virtueof being a simple formula.) function pi_approx($iterations, $print_frequency) { $squared_approx = 12; $next_sign = -1; $denom = 2; for ($iter = 0; $iter < $iterations; $iter++) { $squared_approx += $next_sign * 12/(pow($denom,2)); $denom++; $next_sign = - $next_sign; if ($denom % $print_frequency == 0) { $estimate = sqrt($squared_approx); print( $denom iterations: $estimate
); } } } In addition to performing the calculation itself, this code periodically prints its current esti- mate of pi, so we can see how we are doing. We can call it as follows and then print PHP svalue for comparison: pi_approx(10000, 1000); print( PHP value: . pi() .
); The result looks like: 1000 iterations: 3.14159360947422000 iterations: 3.14159289244163000 iterations: 3.14159275972854000 iterations: 3.14159271328785000 iterations: 3.14159269179466000 iterations: 3.141592680127000 iterations: 3.1415926730818000 iterations: 3.14159266851249000 iterations: 3.141592665380410000 iterations: 3.1415926631401PHP value: 3.1415926535898Now, not only are we not that close, but we can t hope to be more accurate than PHP s valuefor pi, because that already uses all the precision available in the double type.
Note: If you are looking for best quality webspace to host and run your tomcat application check Vision tomcat hosting services

Leave a Reply