gawk: Try To Round
1
1 15.4.3 Try a Few Extra Bits of Precision and Rounding
1 -----------------------------------------------------
1
1 Instead of arbitrary-precision floating-point arithmetic, often all you
1 need is an adjustment of your logic or a different order for the
1 operations in your calculation. The stability and the accuracy of the
1 computation of pi in the earlier example can be enhanced by using the
1 following simple algebraic transformation:
1
1 (sqrt(x * x + 1) - 1) / x == x / (sqrt(x * x + 1) + 1)
1
1 After making this change, the program converges to pi in under 30
1 iterations:
1
1 $ gawk -f pi2.awk
1 -| 3.215390309173473
1 -| 3.159659942097501
1 -| 3.146086215131436
1 -| 3.142714599645370
1 -| 3.141873049979825
1 ...
1 -| 3.141592653589797
1 -| 3.141592653589797
1