背景

在对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. 学习笔记之DBeaver

    DBeaver Community | Free Universal Database Tool https://dbeaver.io/ Universal Database Tool Free mu ...

  2. Mysql中类似于oracle中nvl()函数的ifnull()函数

    IFNULL(expr1,expr2)  如果expr1不是NULL,IFNULL()返回expr1,否则它返回expr2.IFNULL()返回一个数字或字符串值,取决于它被使用的上下文环境.  my ...

  3. JavaWeb 之 JSTL 标签

    JSTL 标签库 一.概述 1.概念 JSTL : JavaServer Pages Tag Library  JSP标准标签库. 是由 Apache 组织提供的开源的免费的 jsp 标签. 2.作用 ...

  4. 为了做好精益化,精益达选择了它?(MES应用案例)

    企业介绍 郑州精益达汽车零部件有限公司(下文简称“精益达”)是宇通客车重要的业务组成部分,也是宇通集团重要的支柱业务之一,主要从事汽车零部件产品研发.制造.销售与服务.在郑州市拥有三个生产厂区,工艺技 ...

  5. POSIX多线程之创建线程pthread_create && 线程清理pthread_cleanup

    多线程之pthread_create创建线程 pthreads定义了一套C程序语言类型.函数.与常量.以pthread.h和一个线程库实现. 数据类型: pthread_t:线程句柄 pthread_ ...

  6. Devops Reference

    摘自 https://www.cnblogs.com/yibutian/p/9561657.html DevOps 企业实践 实施DevOps的核心目标是加速团队.企业的IT精益运行,从根本上提升IT ...

  7. WPF中的RecognizesAccessKey问题

    Groupbox,CheckBox的模板中默认设置RecognizesAccessKey位True.从而导致下划线没法显示. 只能重写绑定. <CheckBox IsChecked=" ...

  8. 为什么在定义hashcode时要使用31这个数呢?

    散列计算就是计算元素应该放在数组的哪个元素里.准确的说是放到哪个链表里面.按照Java的规则,如果你要想将一个对象放入HashMap中,你的对象的类必须提供hashcode方法,返回一个整数值. ht ...

  9. Jenkins系列之-—DevOps高效插件推荐【转】

    基于Jenkins及其插件生态实现自己的持续交付与DevOps平台. jenkins 插件官网 Blue Ocean Jenkins2.7以后可安装,是Jenkins的一种新视图,能够通过图形化的界面 ...

  10. QuickStart系列:docker部署之Gitlab本地代码仓库

    gitlab是可以在本地搭建的使用git作为源代码管理的仓库. 运行环境: win10+vmware14+docker7+docker 1. 使用命令拉取镜像(非必须,耗时比较久,这里以ce为准,ce ...