[英]I want to use big integer in my android app but when ever i apply operators i get error?
I want to calculate factorial my code at present is this: 我现在要计算阶乘我的代码是这样的:
else if(btn.getText().equals("x!"))
{
double LeftVal = Double.parseDouble(currentInput);
double result = Double.NaN;
if(currentInputLen > 0 && LeftVal >0)
{
result = Factorial(LeftVal);
}
resultText.setText(result+"");
}
code to calculate factorial is this: 计算阶乘的代码是这样的:
private double Factorial(double input)
{
double output=input;
for(int i=(int) (input-1);i>0;i--)
{
output*=i;
}
return output;
}
I imported java.math.*; 我导入了java.math。*; and changed my code to this: 并将我的代码更改为此:
else if(btn.getText().equals("x!"))
{
BigInteger LeftVal = BigInteger(currentInput);
BigInteger result = 0;
if(currentInputLen > 0 && LeftVal >0)
{
result = Factorial(LeftVal);
}
resultText.setText(result+"");
}
and factorial method to this 和阶乘方法
private BigInteger Factorial(BigInteger input)
{
BIgInteger output=input;
for(int i=(int) (input-1);i>0;i--)
{
output*=i;
}
return output;
}
I saw on android developer site that BigInteger(string) will convert that string to big integer but that does not work and shows error also there were errors on using regular mathematical errors. 我在android开发人员网站上看到BigInteger(string)会将字符串转换为大整数,但这不起作用并显示错误,并且在使用常规数学错误时也会出现错误。
The reason i want to use big integer is because if i calculate 12! 我要使用大整数的原因是因为如果我计算12! using double it shows answer in scientific form with e raised to power something i want exact answer. 使用double它以科学的形式显示答案,并提出e以增强我想要的确切答案。
For your factorial method using BigInteger you could use: 对于使用BigInteger的析因方法,可以使用:
private BigInteger factorial(BigInteger input) {
BigInteger output = input;
for (BigInteger i = input.subtract(BigInteger.ONE); i.compareTo(BigInteger.ZERO) > 0; i = i.subtract(BigInteger.ONE)) {
output = output.multiply(i);
}
return output;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.