java.lang.NumberFormatException: Infinite or NaN原因之浮点类型除数为0结果探究

背景

在对Double类型的数据进行计算操作,将结果转化为BigDecimal时抛出了下面的异常,进行了Debug才发现了问题原因,同时也暴露出了自己在一些基础知识上还有些欠缺。

Exception in thread "main" java.lang.NumberFormatException: Infinite or NaN
	at java.math.BigDecimal.<init>(BigDecimal.java:895)
	at java.math.BigDecimal.<init>(BigDecimal.java:872)
	at com.lingyejun.authenticator.DoubleTest.main(DoubleTest.java:13)

概念补充

在java中进行数字类型运算的时,之前一直有一种错误的观念,即进行除法运算时当除数为0时在运行时会抛出java.lang.ArithmeticException: / by zero运行时异常。如此想当然的以为对于浮点类型如Float和Double也是如此,下面一段代码便可以说明问题。

package com.lingyejun.authenticator;

public class DoubleTest {

    public static void main(String[] args) {
        Double d1 = 10 / 0D;
        Double d2 = -10 / 0D;
        Double d3 = 0.0 / 0D;
        System.out.println("d1=" + d1 + " d2=" + d2 + " d3=" + d3);
    }
}

运算结果为“d1=Infinity d2=-Infinity d3=NaN”,什么?数字运算居然还能算出来了字符串???打印出来的Infinity、-Infinit、NaN其实不是字符串,而是double类型的常量,查看源码注释便懂了。

/**
 * A constant holding the positive infinity of type
 * {@code double}. It is equal to the value returned by
 * {@code Double.longBitsToDouble(0x7ff0000000000000L)}.
 */
public static final double POSITIVE_INFINITY = 1.0 / 0.0;

/**
 * A constant holding the negative infinity of type
 * {@code double}. It is equal to the value returned by
 * {@code Double.longBitsToDouble(0xfff0000000000000L)}.
 */
public static final double NEGATIVE_INFINITY = -1.0 / 0.0;

/**
 * A constant holding a Not-a-Number (NaN) value of type
 * {@code double}. It is equivalent to the value returned by
 * {@code Double.longBitsToDouble(0x7ff8000000000000L)}.
 */
public static final double NaN = 0.0d / 0.0;

正无穷:POSITIVE_INFINITY,正数除以零得到正无穷。

负无穷:NEGATIVE_INFINITY,负数除以零得到负无穷。

非数字:NaN,0除以0时得到非数字。 

异常原因  

通过查看BigDecimal类中针对Double类型数据的构造方法,我们知道了,在构造BigDecimal对象时,构造方法中传入的Double类型为无穷大或非数字时会抛出NumberFormatException异常。

public BigDecimal(double val, MathContext mc) {
        if (Double.isInfinite(val) || Double.isNaN(val))
            throw new NumberFormatException("Infinite or NaN");
拨云见日探究清楚之后,一切都是那样的理所应当。

原文地址:https://www.cnblogs.com/lingyejun/p/11216033.html

时间: 2024-08-09 11:10:16

java.lang.NumberFormatException: Infinite or NaN原因之浮点类型除数为0结果探究的相关文章

java.lang.NumberFormatException: For input string: &quot;undefined&quot;

在将字符串转换为数字时导致此错误,解决此问题的思路:1.添加Try catch语句,2.判断字符串是否为数字,将介绍java中判断字符串是否为数字的方法的几种方法. 完整错误信息: java.lang.NumberFormatException: For input string: "undefined" at java.lang.NumberFormatException.forInputString(Unknown Source) at java.lang.Integer.pars

java.lang.NumberFormatException: empty String

1.错误描述 java.lang.NumberFormatException: empty String at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1020) at java.lang.Double.parseDouble(Double.java:540) service.impl.BillServiceImpl.exportBillExcel(BillServiceImpl.java:301) a

Caused by: java.lang.NumberFormatException: For input string: &quot;&quot;

1.错误描述 java.lang.NumberFormatException: For input string: "" at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65) at java.lang.Long.parseLong(Long.java:453) at java.lang.Long.parseLong(Long.java:483) at org.springfram

java.lang.NumberFormatException

1.错误描述 Exception in thread "main" java.lang.NumberFormatException: For input string: "61.13226212525146" at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65) at java.lang.Integer.parseInt(Integer.java:492) a

Caused by: java.lang.NumberFormatException: For input string: &amp;quot;&amp;quot;

1.错误描写叙述 java.lang.NumberFormatException: For input string: "" at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65) at java.lang.Long.parseLong(Long.java:453) at java.lang.Long.parseLong(Long.java:483) at org.springfr

java.lang.NumberFormatException: For input string: &quot;?0&quot;

在使用JAVA IP地址转成长整型方法时出现此错误 这里对场景进行记录,以备日后遇到类似的错误,参考如何解决 错误输出: 错误原因:怀疑此处为"\0"字符 解决方法: java.lang.NumberFormatException: For input string: "?0"

关于jstl EL用法的注意点(java.lang.NumberFormatException: For input string: &quot;userName&quot;)

在使用jstl tag <c:forEach var="item" items="${managerPurviewList}" begin="0"> 的时候,一般可以用来在页面上展现集合的数据,但是有一点必须要注意的,就是var 和items的含义的问题. 查看api文档,上面只是简单的说明了一下,其实在实现该标签的时候,内部是通过items里面指定的这个集合对象来获取一个Iterator 的,这就是ForEachIterator .

java.lang.NumberFormatException: For input string: &quot;${jdbc.maxActive}&quot;

一.问题 使用SpringMVC和MyBatis整合,将jdbc配置隔离出来的时候出现下面的错误,百度了很久没有找到解决方法,回家谷歌下,就找到解决方法了,不得不说谷歌就是强大,不废话,下面是具体的错误: Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'mySqlDataSource' defined in ServletContext resou

SimpleDateFormat高并发下异常java.lang.NumberFormatException: For input string: &quot;&quot;

1. 原因 SimpleDateFormat(下面简称sdf)类内部有一个Calendar对象引用,它用来储存和这个sdf相关的日期信息,例如sdf.parse(dateStr), sdf.format(date) 诸如此类的方法参数传入的日期相关String, Date等等, 都是交友Calendar引用来储存的.这样就会导致一个问题,如果你的sdf是个static的, 那么多个thread 之间就会共享这个sdf, 同时也是共享这个Calendar引用, java.lang.NumberFo