import java.math.*;
public class wrenwren
{ //PI=4*(1-1/3+1/5-1/7...)
public static void main(String args[])
{
int count = 2E31-1;
double pi = 0;
double k = 1;
for (int i = 1, j = 3; i < count; i++)
{
if (j % 2 != 0)
{
pi += 1.0 / k;
k += 2;
j = 2;
}
else
{
pi -= 1.0 / k;
j = 3;
k += 2;
} }
System.out.print("pi=" + 4*pi);
}
}怎么越位了??
public class wrenwren
{ //PI=4*(1-1/3+1/5-1/7...)
public static void main(String args[])
{
int count = 2E31-1;
double pi = 0;
double k = 1;
for (int i = 1, j = 3; i < count; i++)
{
if (j % 2 != 0)
{
pi += 1.0 / k;
k += 2;
j = 2;
}
else
{
pi -= 1.0 / k;
j = 3;
k += 2;
} }
System.out.print("pi=" + 4*pi);
}
}怎么越位了??
为什么int i=1;正确 int i=2E31-1就不行呢??
int count = Integer.MAX_VALUE
Java double 型 15 位精度左右
看来精度可能不够