重构使用长参数的函数,改用BigInteger。

huangapple go评论72阅读模式
英文:

Refactoring function that uses long argument to use BigInteger

问题

以下是翻译好的部分:

原始代码:

static void ConditionalSum_1(long n) {

    long soma = 0;

    int i = 1;
    while (i < n) {
        for (int j = 0; j < i; j++) {
            soma++;
        }
        i *= 2;
    }
    System.out.println("Soma C1 = " + soma);
}

转换后的代码:

static void ConditionalSum_2(BigInteger n) {

    BigInteger soma = BigInteger.ZERO;

    BigInteger i = BigInteger.ONE;
    while (i.compareTo(n) < 0) {
        for (BigInteger j = BigInteger.ZERO; j.compareTo(i) < 0; j = j.add(BigInteger.ONE)) {
            soma = soma.add(BigInteger.ONE);
        }
        i = i.multiply(BigInteger.TWO);
    }
    System.out.println("Soma C2 = " + soma);
}

函数调用部分:

public static void main(String[] args) {

    ConditionalSum_1(999999L); //正常工作
    ConditionalSum_2(new BigInteger("999999")); //无限循环

}

由于某些我无法确定的原因,ConditionalSum_2 函数似乎无法正常工作,没有抛出异常,变量似乎没有改变,因此例程进入了无限循环。我对 Java 还有点陌生,所以我确定我在这里漏掉了一些基本的东西。帮助将不胜感激。

英文:

I need to convert some methods which use long arguments, it turns out 64 bit longs are too small for what I need, what I did was to convert these so they can take BigInteger.

Here is reproducible example:

Original:

static void ConditionalSum_1(long n) {

    long soma = 0;
 
    int i = 1;
    while (i &lt; n) {
        for (int j = 0; j &lt; i; j++) {
            soma++;
        }
        i *= 2;
    }
    System.out.println(&quot;Soma C1 = &quot; + soma);
}

Converted:

static void ConditionalSum_2(BigInteger n) {

    BigInteger soma = BigInteger.ZERO;

    BigInteger i = BigInteger.ONE;
    while (i.compareTo(n) &lt; 0) {
        for (BigInteger j = BigInteger.ZERO; j.compareTo(i) &lt; 0; j.add(BigInteger.ONE)) {
            soma.add(BigInteger.ONE);
        }
        i.multiply(BigInteger.TWO);
    }
    System.out.println(&quot;Soma C2 = &quot; + soma);
}

Function calls:

public static void main(String[] args) {

    ConditionalSum_1(999999L); //works fine
    ConditionalSum_2(new BigInteger(&quot;999999&quot;)); //infinite loop

}

For reasons I can't pinpoint the ConditionalSum_2 function doesn't seem to be working, there are no exceptions thrown, the variables don't seem to be changing and as a consequence the routine enters an infinite loop.

I'm kind of new to Java so I'm sure I'm missing something basic here. Help would be apreciated.

答案1

得分: 3

当您将一个BigInteger与另一个BigInteger相加时,和将作为结果返回。因此,您必须将返回值再次赋值给适当的变量。

for (BigInteger j = BigInteger.ZERO; j.compareTo(i) < 0; j = j.add(BigInteger.ONE)) 
{
    soma = soma.add(BigInteger.ONE);
}

i = i.multiply(BigInteger.TWO);
英文:

When you sum a BigInteger with another BigInteger the sum will be returned as the result. So you have to assign the return value to the appropriate variable again.

   for (BigInteger j = BigInteger.ZERO; j.compareTo(i) &lt; 0; j = j.add(BigInteger.ONE)) 
   {
        soma = soma.add(BigInteger.ONE);
   }

   i = i.multiply(BigInteger.TWO);

huangapple
  • 本文由 发表于 2020年10月11日 00:42:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/64295853.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定