[英]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.dgm$489.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”作为返回:
Math.pow(self.doubleValue(), exponent.doubleValue())
然后,BigDecimal将使用valueof将其转换为BigInteger
BigDecimal.valueOf("Infinity")
这就是为什么您将获得NumberFormatException的原因
溴
提姆
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.