java中π用Math.PI表示,圆周率常量π被定义在java.lang.Math类中。输出:3.141592653589793
代码如下:
PI (π)的源码如下:
/**
* The {@code double} value that is closer than any other to
* <i>pi</i>, the ratio of the circumference of a circle to its
* diameter.
*/
public static final double PI = 3.14159265358979323846
扩展资料:
Java Math 类包含了用于执行基本数学运算的属性和方法,如初等指数、对数、平方根和三角函数。
Math 的方法都被定义为 static 形式,通过 Math 类可以在主函数中直接调用。
比较常见的还有一个底数e,在java Math中表示如下:
public static final double E = 2.7182818284590452354
参考资料:
Orcale官方API接口-Class Math
代码如下://圆周率 π = 4∑(k=0,..∞)(-1)^k/(2k+1)
//π = 3.14159265358979323846
public class yzq {
public static void main(String[] args) {
// TODO Auto-generated method
stub
int n=0
double a=0
for (n=0n<=10000000n++ )
{
a = a + 4*Math.pow(-1,n)/(2*n+1)
System.out.println("第 "+n+"
位
"+a)
}
System.out.println("最后一位
"+a)
}
}
//程序运行结果
//第10
次计算
3.232315809405594
//第100
次计算
3.1514934010709914
//第1000
次计算
3.1425916543395442
//第10,000
次计算
3.1416926435905346
//第100,000
次计算
3.1416026534897203
//第1000,000
次计算
3.1415936535887745
//第10,000,000次计算
3.1415927535897814
//第100,000,000次计算
3.141592643589326
//第999,999,990次计算
3.1415926545880506
//第999,999,991次计算
3.1415926525880504
//第999,999,992次计算
3.1415926545880506
//第999,999,993次计算
3.1415926525880504
//第999,999,994次计算
3.1415926545880506
//第999,999,995次计算
3.1415926525880504
//第999,999,996次计算
3.1415926545880506
//第999,999,997次计算
3.1415926525880504
//第999,999,998次计算
3.1415926545880506
//第999,999,999次计算
3.1415926525880504
//第1,000,000,000次计算
3.1415926545880506