This article describes the simple calculation of pi in Java. Share it for your reference, as follows:
I saw a question on the Internet these two days. It is known that Pi can be calculated using the function 4 * (1 1/3 + 1/5 1/7 + …) to calculate the five decimal places (calculate the pi). So I wrote a small demo manually, and everyone is welcome to leave a message, communicate and give advice.
The complete example code is as follows:
package math;public class PiTest { private static int bytelimit = 5;// limit the number of digits after decimal/** * @author zhao33699 */ public static void main(String[] args) { // TODO Auto-generated method stub // It is known that Pi can be calculated using function 4 * (1 1/3 + 1/5 1/7 + …) and five decimal places after decimal ----calculate pi// Idea: Calculate the 5-bit value according to the above formula, compare it with the next 5-bit value, if the same, it is the final result, // If it is different, save the new value and continue to compare it with the next value until the same double pitemp = 0;// The value obtained by the formula is double finalpi = 0; // Last value obtained (five decimal places) double pi = 0;// This value obtained (five decimal places) int i = 0;// Counter double b = 0;// The value in brackets of the formula is int ii = 20;// The number of times the same value can be limited to the same value obtained int finalii = 20;// The same as the value of ii, is used to restore the ii value after the ii value changes // int iii=20;// Limit the same number of times while (true) { // Number of times control if (i == 1000000) { break; } double rs = 1 + 2 * i; double d = 1 / rs; if (i % 2 == 0 && i != 1) { b = b + d; // System.out.println("---"+i+"----------***"+b); } else { b = b - d; // System.out.println("---"+i+"-----------***"+b); } i = i + 1; // System.out.println(b); pitemp = (b) * 4; // System.out.println(pitemp); // The number of digits after the decimal point is greater than or equal to 5 digits if (String.valueOf(pitemp).length() > bytelimit) { pi = subInt(pitemp);// Intercept the value of 5 digits after the decimal point System.out.println(i + "time---pi--####" + pi + "---final--####" + finalpi); // If the last result is the same as this result, limit the number of consecutive times -1; // If it is not the same, regardless of how many consecutive times the previous result is the same, limit the number of consecutive times to restore the initial value, and save the new value of this time, and continue to prepare for comparison with the next time if (finalpi == pi) { System.out.println("th" + (finali + 1 - ii) + "time----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 0) { System.out.println("$ final decimal" + pitemp); System.out.println("$ final decimal" + pi); break; } } else { finalpi = pi; ii = finalii; } } } } } // Format decimal public static double subInt(double i) { String s = String.valueOf(i).substring(0, bytelimit + 2);// Get the 5 decimal places after the decimal point String ss = String.valueOf(i).substring(bytelimit + 2, bytelimit + 3);// Get the 6th decimal point double dd = Double.parseDouble(s);// Convert to a decimal point to retain the 5 decimal places// If the value of the 6th digit is greater than or equal to 5, according to rounding, add the converted decimal to 0.00001 if (ss.compareTo("5") >= 0) { dd = dd + 0.00001; } return dd; }}Running results:
PS: Here are a few calculation tools for you to refer to:
Online unary function (eq) solution calculation tool:
http://tools.VeVB.COM/jisuanqi/equ_jisuanqi
Scientific Calculator Online Use_Advanced Calculator Online Calculator:
http://tools.VeVB.COM/jisuanqi/jsqkeexue
Online Calculator_Standard Calculator:
http://tools.VeVB.COM/jisuanqi/jsq
For more information about Java algorithms, readers who are interested in this site can view the topics: "Java Data Structure and Algorithm Tutorial", "Summary of Java Operation DOM Node Tips", "Summary of Java File and Directory Operation Tips" and "Summary of Java Cache Operation Tips"
I hope this article will be helpful to everyone's Java programming.