Groovy 在处理非常大的数字时抛出 NumberformaException
Groovy throws Numberformatexception when dealing with extremely large numbers
因此在 Intellij IDEA 中使用 groovy,我使用以下代码得到异常:
def t = (100000G**100000000000G)
现在我知道这些数字是任何理智的人都不想计算的,但是出于提问的目的和出于我的好奇心,为什么会抛出以下异常?
Exception in thread "main" java.lang.NumberFormatException
at java.math.BigDecimal.<init>(BigDecimal.java:494)
at java.math.BigDecimal.<init>(BigDecimal.java:383)
at java.math.BigDecimal.<init>(BigDecimal.java:806)
at java.math.BigDecimal.valueOf(BigDecimal.java:1274)
at org.codehaus.groovy.runtime.DefaultGroovyMethods.power(DefaultGroovyMethods.java:14303)
at org.codehaus.groovy.runtime.dgm9.invoke(Unknown Source)
at org.codehaus.groovy.runtime.callsite.PojoMetaMethodSite$PojoMetaMethodSiteNoUnwrapNoCoerce.invoke(PojoMetaMethodSite.java:274)
at org.codehaus.groovy.runtime.callsite.PojoMetaMethodSite.call(PojoMetaMethodSite.java:56)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:47)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:116)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:128)
at dev.folling.code.Main.main(Main.groovy:11)
将 **
替换为 .power()
不会改变任何内容。
该错误显然是由于在某些时候出现了非法字符引起的。我怀疑这可能是内存错误,但我不确定。
你可以试着给你的代码打个断点,然后再深挖一下。
这就是幕后发生的事情。
public static BigInteger power(BigInteger self, BigInteger exponent) {
return exponent.signum() >= 0 && exponent.compareTo(BI_INT_MAX) <= 0?self.pow(exponent.intValue()):BigDecimal.valueOf(Math.pow(self.doubleValue(), exponent.doubleValue())).toBigInteger();
}
在 运行 进程中,它使用以下代码将 "Infinity" 作为 return 返回:
Math.pow(self.doubleValue(), exponent.doubleValue())
然后 BigDecimal 将使用 valueof 将其转换为 BigInteger
BigDecimal.valueOf("Infinity")
这就是为什么你会得到一个 NumberFormatException
Br,
蒂姆
因此在 Intellij IDEA 中使用 groovy,我使用以下代码得到异常:
def t = (100000G**100000000000G)
现在我知道这些数字是任何理智的人都不想计算的,但是出于提问的目的和出于我的好奇心,为什么会抛出以下异常?
Exception in thread "main" java.lang.NumberFormatException
at java.math.BigDecimal.<init>(BigDecimal.java:494)
at java.math.BigDecimal.<init>(BigDecimal.java:383)
at java.math.BigDecimal.<init>(BigDecimal.java:806)
at java.math.BigDecimal.valueOf(BigDecimal.java:1274)
at org.codehaus.groovy.runtime.DefaultGroovyMethods.power(DefaultGroovyMethods.java:14303)
at org.codehaus.groovy.runtime.dgm9.invoke(Unknown Source)
at org.codehaus.groovy.runtime.callsite.PojoMetaMethodSite$PojoMetaMethodSiteNoUnwrapNoCoerce.invoke(PojoMetaMethodSite.java:274)
at org.codehaus.groovy.runtime.callsite.PojoMetaMethodSite.call(PojoMetaMethodSite.java:56)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:47)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:116)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:128)
at dev.folling.code.Main.main(Main.groovy:11)
将 **
替换为 .power()
不会改变任何内容。
该错误显然是由于在某些时候出现了非法字符引起的。我怀疑这可能是内存错误,但我不确定。
你可以试着给你的代码打个断点,然后再深挖一下。
这就是幕后发生的事情。
public static BigInteger power(BigInteger self, BigInteger exponent) {
return exponent.signum() >= 0 && exponent.compareTo(BI_INT_MAX) <= 0?self.pow(exponent.intValue()):BigDecimal.valueOf(Math.pow(self.doubleValue(), exponent.doubleValue())).toBigInteger();
}
在 运行 进程中,它使用以下代码将 "Infinity" 作为 return 返回:
Math.pow(self.doubleValue(), exponent.doubleValue())
然后 BigDecimal 将使用 valueof 将其转换为 BigInteger
BigDecimal.valueOf("Infinity")
这就是为什么你会得到一个 NumberFormatException
Br,
蒂姆