我想出了十六进制答案,但是我不知道将答案转换为十进制。
import java.io.Console;
import java.math.BigDecimal;
public class Main {
public static double n = 0;
public static String ALPHABET = "0123456789ABCDEF";
public static void main(String[] args) {
System.out.println("Enter how many decimal places you want for Pi?");
String line = System.console().readLine();
n = Integer.parseInt(line);
double S1 =S(1);
double S4 = S(4);
double S5 = S(5);
double S6 = S(6);
double y = 4*S1 -2*S4 - S5 - S6;
y = Math.abs(y);
y = y-(int)y; // remove start of fraction
String HEX = HEX(y);
System.out.println(HEX);
//System.out.println(new BigDecimal(y).toPlainString());
}
public static double S(double j) {
double S = 0;
for(double k = 0; k <= n; k++) {
S += (Math.pow(16.0,n-k)%(8*k+j))/(8*k+j);
}
for(double k = n + 1; k <= (n + 3); k++) {
S += Math.pow(16.0,n-k)/(8*k+j);
}
return S;
}
public static String HEX(double y) {
String HEX1 = "";
for(int i = 0; i < 4; i++) {
y = y * 16;
int z = (int)y; //remove end of fraction
z = z%16;
HEX1 += ALPHABET.substring(z,z+1);
}
return HEX1;
}
}
是否无法从BBP算法转换十六进制答案? 我希望有可能。我花了很长时间。