背景

在对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");
拨云见日探究清楚之后,一切都是那样的理所应当。

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

  1. java.lang.NumberFormatException: Infinite or NaN

      1.异常提示: java.lang.NumberFormatException: Infinite or NaN 2.原因:无法格式化的数字,此数字要么不是个数字,要么是无穷大的数字,从而导致 B ...

  2. SpringBoot整合Swagger2案例,以及报错:java.lang.NumberFormatException: For input string: ""原因和解决办法

    原文链接:https://blog.csdn.net/weixin_43724369/article/details/89341949 SpringBoot整合Swagger2案例 先说SpringB ...

  3. java.lang.NumberFormatException: For input string: "1608020001 " 错误

    错误: java.lang.NumberFormatException: For input string: "1608020001 "    at java.lang.Numbe ...

  4. java.lang.NumberFormatException: For input string: "Y"

    nested exception is org.apache.ibatis.exceptions.PersistenceException: ### Error querying database.  ...

  5. mybatis 报错:Caused by: java.lang.NumberFormatException: For input string

    mybatis的if标签之前总是使用是否为空,今天要用到字符串比较的时候遇到了困难,倒腾半天,才在一个论坛上找到解决方法.笔记一下,如下: 转自:https://code.google.com/p/m ...

  6. java.lang.NumberFormatException

    1.错误描述 Exception in thread "main" java.lang.NumberFormatException: For input string: " ...

  7. MyBatis报错:Caused by: java.lang.NumberFormatException: For input string: "XX"

    <select id="sltTreatment" resultType="com.vitaminmd.sunny.core.bo.Treatment"& ...

  8. 异常java.lang.NumberFormatException解决

    原因一:超出了int类型的取值范围 项目中要把十六进制字符串转化为十进制, 用到了到了Integer.parseInt(str1.trim(), 16):这个是不是后抛出java.lang.Numbe ...

  9. Caused by: java.lang.NumberFormatException: For input string: "18446744073709551615"

    问题:Caused by: java.lang.NumberFormatException: For input string: "18446744073709551615" 原因 ...

随机推荐

  1. 神奇的外部嵌套(使用ROW_NUMBER()查询带条件的时候提示列名无效)

    declare @pageIndex int -- 第几页 declare @pageSize int -- 每页包含的记录数 --这里注意一下,不能直接把变量放在这里,要用select select ...

  2. 【转载】C#中List集合使用LastIndexOf判断元素最后一次出现的索引位置

    在C#的List集合操作中,有时候需要判断元素对象在List集合中第一次出现的索引位置信息,此时需要使用到List集合的IndexOf方法来判断,如果元素存在List集合中,则IndexOf方法返回所 ...

  3. 反序列化之PHP原生类的利用

    目录 基础知识 __call SoapClient __toString Error Exception 实例化任意类 正文 文章围绕着一个问题,如果在代码审计中有反序列化点,但是在原本的代码中找不到 ...

  4. 在eclipse下给android应用添加jar包

    右键工程,Build path,java build path,选择libraries在右边的按钮中点击“Add Library”选择“User library”,点击“下一步”点击“User lib ...

  5. 【DATAGUARD】物理dg的switchover切换(五)

    [DATAGUARD]物理dg的switchover切换(五) 一.1  BLOG文档结构图 一.2  前言部分 一.2.1  导读 各位技术爱好者,看完本文后,你可以掌握如下的技能,也可以学到一些其 ...

  6. H3C WLAN相关组织和标准

  7. Qt编译出错 GL/gl.h:No such file or directory

    系统:Ubuntu18.04 软件:QtCreator4.8 Qt5.9.8 编译Qt Widgets Application类型的项目时,出现错误,提示: GL/gl.h: No such file ...

  8. 如何临时修改 macOS 应用的界面语言?

    一般情况下,应用程序的界面语言会和系统语言保持一致.但有些时候,我们也会希望临时换用一种不同的界面语言.例如,一些程序的中文翻译词不达意,有必要参考英文界面来确定功能的准确含义:又如,一些网页会强制按 ...

  9. 如何使用anaconda安装pygame

    超级方便!!! 打开Anaconda, 输入: pip install pygame 等待下载安装完成. 如图: 显示成功安装:

  10. Linux-普通用户切换为root身份

    Linux 下普通用户免密码切换 root 用户 具体步骤: 1)编辑 su 文件打开第四行注释 # vim /etc/pam.d/su 2)将用户添加到 wheel 组 # usermod -G w ...