我的代码的目的是编写一个程序,使用Rational类计算以下求和系列:1/2 + 2/3 + 3/4 + ... + 99/100。
我的代码如下所示:
import java.math.BigInteger;
public class Exercise {
public static void main(String[] args){
final BigInteger N = new BigInteger("99");
RationalV8 sum = new RationalV8();
for (BigInteger i = BigInteger.ONE; i.compareTo(N) <= 0; i = i.add(BigInteger.ONE)){
sum = sum.add(new RationalV8(i, i.add(BigInteger.ONE)));
}
System.out.println("The sum : " + sum + " = "
+ sum.doubleValue());
}
}
class RationalV8 extends Number implements Comparable <RationalV8>{
private BigInteger numerator = BigInteger.ZERO;
private BigInteger denominator = BigInteger.ONE;
public RationalV8(){
this(BigInteger.ZERO, BigInteger.ONE);
}
public RationalV8( BigInteger numerator, BigInteger denominator){
BigInteger gcd = gcd(numerator, denominator);
if (denominator.compareTo(BigInteger.ZERO) < 0 ){
this.numerator = numerator.multiply(new BigInteger("-1")).divide(gcd);
} else {
this.numerator = numerator.divide(gcd);
}
this.denominator = denominator.abs().divide(gcd);
}
public static BigInteger gcd(BigInteger n, BigInteger d){
BigInteger n1 = n.abs();
BigInteger n2 = d.abs();
BigInteger gcd = BigInteger.ONE;
for (BigInteger i = BigInteger.ONE; i.compareTo(n1) <= 0 && i.compareTo(n2) <= 0;
i = i.add(BigInteger.ONE) ){
if (n1.remainder(i).equals(BigInteger.ZERO) && n2.remainder(i).equals(BigInteger.ZERO)){
gcd = i;
}
}
return gcd;
}
public BigInteger getNumerator(){
return numerator;
}
public BigInteger getDenominator(){
return denominator;
}
public RationalV8 add(RationalV8 secondRational){
BigInteger n = numerator.multiply(secondRational.getDenominator()).add(
denominator.multiply(secondRational.getNumerator()));
BigInteger d = denominator.multiply(secondRational.getDenominator());
return new RationalV8(n, d);
}
public RationalV8 subtract(RationalV8 secondRational){
BigInteger n = numerator.multiply(secondRational.getDenominator()).subtract(
denominator.multiply(secondRational.getNumerator()));
BigInteger d = denominator.multiply(secondRational.getDenominator());
return new RationalV8(n, d);
}
public RationalV8 multiply(RationalV8 secondRational){
BigInteger n = numerator.multiply(secondRational.getNumerator());
BigInteger d = denominator.multiply(secondRational.getDenominator());
return new RationalV8(n, d);
}
public RationalV8 divide(RationalV8 secondRational){
BigInteger n = numerator.multiply(secondRational.getDenominator());
BigInteger d = denominator.multiply(secondRational.getNumerator());
return new RationalV8(n, d);
}
@Override
public int compareTo(RationalV8 o){
if ( (this.subtract((RationalV8) o)).getNumerator().compareTo(BigInteger.ZERO) > 0){
return 1;
}
else if ( (this.subtract((RationalV8) o)).getNumerator().compareTo(BigInteger.ZERO) < 0){
return -1;
} else {
return 0;
}
}
@Override
public String toString(){
if (denominator.equals(BigInteger.ONE)){
return numerator + " ";
} else {
return numerator + "/" + denominator;
}
}
@Override
public boolean equals(Object param1){
if ((this.subtract( (RationalV8) (param1) )).getNumerator().equals(BigInteger.ZERO) ){
return true;
} else {
return false;
}
}
@Override
public int hashCode(){
return new Double( this.doubleValue() ).hashCode();
}
//@Override
public int intValue(){
return (int)doubleValue();
}
//@Override
public float floatValue(){
return (float)doubleValue();
}
public double doubleValue(){
return numerator.doubleValue() / denominator.doubleValue();
}
public long longValue(){
return (long)doubleValue();
}
}
此代码的运行输出为: 第一个系列的总和是1/0 =无穷大。
但由于整数溢出,输出不正确。
谁能告诉我问题出在哪里?
答案 0 :(得分:0)
我认为BigInteger n2 = n.abs();
行应该是BigInteger n2 = d.abs();
。我不确定这是唯一的错误。