Java BigInteger 从 int 到 BigInteger 的转换
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18478280/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
BigInteger Conversion from int to BigInteger
提问by Jessica M.
I'm having trouble working with BigIntegers. I'm having trouble with the add
method in the Rational class. In the Rational(int x, int y)
constructor I'm trying to convert the parameters datatype int
into the instance variable datatype of BigInteger
though the use of thetoString(int n)
method.
我在使用 BigIntegers 时遇到问题。我add
在使用 Rational 类中的方法时遇到了问题。在Rational(int x, int y)
构造函数中,我试图将参数数据类型int
转换BigInteger
为使用该toString(int n)
方法的实例变量数据类型。
- Am I doing the conversion correctly inside the
Rational(int x, int y)
constructor? - They way the
add
method is written I'm getting an error under all of n.num and n.den. I don't understand why I'm getting that error. Am I not correctly using theadd
method from the BigInteger class? http://docs.oracle.com/javase/1.4.2/docs/api/java/math/BigInteger.html
- 我在
Rational(int x, int y)
构造函数中正确地进行了转换吗? - 他们
add
编写方法的方式我在所有 n.num 和 n.den 下都收到错误。我不明白为什么我会收到那个错误。我没有正确使用add
BigInteger 类中的方法吗? http://docs.oracle.com/javase/1.4.2/docs/api/java/math/BigInteger.html
Suppose one class has the following
假设一个类有以下内容
Rational a = new Rational(1,2);
Rational b = new Rational(1,3);
Rational c = new Rational(1,6);
Rational sum = a.add(b).add(c);
println(sum);
and the Rational class includes
和 Rational 类包括
import acm.program.*;
import java.math.*;
public class Rational{
public Rational(int x, int y) {
num = new BigInteger(toString(x));
den = new BigInteger(toString(y));
}
public toString(int n) {
return toString(n);
}
public BigInteger add(BigInteger n) {
return new BigInteger(this.num * n.den + n.num * this.den, this.den * n.den)
}
/* private instance variables */
private BigInteger num;
private BigInteger den;
}
采纳答案by trogdor
To convert an int to BigInteger I would use BigInteger.valueOf(int)
.
要将 int 转换为 BigInteger,我会使用BigInteger.valueOf(int)
.
Also, you cannot use operators with BigIntegers, you must use its own methods. Your methos should be like this:
此外,您不能将运算符与 BigIntegers 一起使用,您必须使用它自己的方法。你的方法应该是这样的:
public Rational add(Rational n) {
return new Rational(
this.num.multiply(n.den).add(n.num.multiply(this.den)).intValue(),
this.den.multiply(n.den).intValue());
}
回答by Jim Garrison
A simple error:
一个简单的错误:
public Rational add(Rational n) {
return new Rational(
this.num.multiply(n.den).add(n.num.multiply(this.den)),
this.den.multiply(n.den));
}
Also, when creating a new BigInteger
you should use the valueOf(int)
method instead of converting to String
此外,在创建新的时,BigInteger
您应该使用该valueOf(int)
方法而不是转换为String
回答by Jayamohan
1) Am I doing the conversion correctly inside the Rational(int x, int y) constructor?
1) 我在 Rational(int x, int y) 构造函数中正确地进行了转换吗?
You can use
您可以使用
BigInteger num = BigInteger.valueOf(x);
Making a String first is is not required.
不需要先制作字符串。
2. They way the add method is written I'm getting an error .....
Your add method is wrong and its not clear what your are trying to acheive in your add method. But if your want to do addition in BigInteger you should use BigInteger#addmethod and for multiplication between BigInteger you should use BigInteger#multiplymethod.
您的 add 方法是错误的,并且不清楚您要在您的 add 方法中实现什么。但是如果你想在 BigInteger 中做加法,你应该使用BigInteger#add方法,对于 BigInteger 之间的乘法你应该使用BigInteger#multiply方法。
回答by Dawood ibn Kareem
To stop the denominators blowing up exponentially, I would use the lowest common multiple of the two denominators as the denominator of the result, not their product. This would look like this.
为了阻止分母呈指数爆炸,我将使用两个分母的最小公倍数作为结果的分母,而不是它们的乘积。这看起来像这样。
public Rational add(Rational rhs) {
BigInteger commonFactor = den.gcd(rhs.den);
BigInteger resultNumerator =
num.multiply(rhs.den).add(rhs.num.multiply(den)).divide(commonFactor);
BigInteger resultDenominator = den.multiply(rhs.den).divide(commonFactor);
return new Rational(resultNumerator, resultDenominator);
}
To use this exactly how I've written it, you'll need a new constructor that takes two BigInteger
arguments; but you probably want that anyway.
要完全按照我的方式使用它,您需要一个带有两个BigInteger
参数的新构造函数;但无论如何你可能想要那个。