从int到BigInteger的转换

时间:2021-10-15 16:42:12

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.

我在处理大整数时有困难。我在Rational类的add方法上遇到了麻烦。在Rational(int x, int y)构造函数中,我试图通过使用tostring (int n)方法将参数数据类型int转换为BigInteger的实例变量数据类型。

  1. Am I doing the conversion correctly inside the Rational(int x, int y) constructor?
  2. 我是否正确地在Rational(int x, int y)构造函数中进行转换?
  3. 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 the add method from the BigInteger class? http://docs.oracle.com/javase/1.4.2/docs/api/java/math/BigInteger.html

  4. add方法是这样写的我在n下得到一个错误。num n.den。我不明白我为什么会犯那样的错误。我没有正确地使用BigInteger类中的add方法吗?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;
}

4 个解决方案

#1


2  

To convert an int to BigInteger I would use BigInteger.valueOf(int).

要将int转换为BigInteger,我将使用bigint . 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());
}

#2


2  

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

#3


2  

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#add method and for multiplication between BigInteger you should use BigInteger#multiply method.

您的add方法是错误的,并且不清楚您要在add方法中尝试实现什么。但是如果你想要对BigInteger进行加法,你应该使用BigInteger#add方法,对于BigInteger之间的乘法,你应该使用BigInteger#乘法。

#4


1  

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参数;但不管怎样,你可能还是想要。

#1


2  

To convert an int to BigInteger I would use BigInteger.valueOf(int).

要将int转换为BigInteger,我将使用bigint . 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());
}

#2


2  

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

#3


2  

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#add method and for multiplication between BigInteger you should use BigInteger#multiply method.

您的add方法是错误的,并且不清楚您要在add方法中尝试实现什么。但是如果你想要对BigInteger进行加法,你应该使用BigInteger#add方法,对于BigInteger之间的乘法,你应该使用BigInteger#乘法。

#4


1  

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参数;但不管怎样,你可能还是想要。