gpt4 book ai didi

java - BigDecimal求余方法可能返回负值,为什么?

转载 作者:太空宇宙 更新时间:2023-11-04 12:32:51 25 4
gpt4 key购买 nike

什么情况下BigDecimal的求余方法会返回负值?

Javadoc here 中有说明:

The remainder is given by this.subtract(this.divideToIntegralValue(divisor).multiply(divisor)). Note that this is not the modulo operation (the result can be negative).

最佳答案

根据JavaDoc,该操作的结果计算如下:

The remainder is given by

this.subtract(this.divideToIntegralValue(divisor).multiply(divisor))

所以:如果 BigInteger 的实例指向负数,则结果可能是负数..

示例:

  public static void main(String[] args) {
BigInteger a = new BigInteger("-100");
BigInteger b = new BigInteger("3");
BigInteger result = a.remainder(b);
System.out.println(result);
}

这将打印-1

关于java - BigDecimal求余方法可能返回负值,为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37661823/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com