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");
拨云见日探究清楚之后,一切都是那样的理所应当。
java.lang.NumberFormatException: Infinite or NaN原因之浮点类型除数为0结果探究的更多相关文章
- java.lang.NumberFormatException: Infinite or NaN
1.异常提示: java.lang.NumberFormatException: Infinite or NaN 2.原因:无法格式化的数字,此数字要么不是个数字,要么是无穷大的数字,从而导致 B ...
- SpringBoot整合Swagger2案例,以及报错:java.lang.NumberFormatException: For input string: ""原因和解决办法
原文链接:https://blog.csdn.net/weixin_43724369/article/details/89341949 SpringBoot整合Swagger2案例 先说SpringB ...
- java.lang.NumberFormatException: For input string: "1608020001 " 错误
错误: java.lang.NumberFormatException: For input string: "1608020001 " at java.lang.Numbe ...
- java.lang.NumberFormatException: For input string: "Y"
nested exception is org.apache.ibatis.exceptions.PersistenceException: ### Error querying database. ...
- mybatis 报错:Caused by: java.lang.NumberFormatException: For input string
mybatis的if标签之前总是使用是否为空,今天要用到字符串比较的时候遇到了困难,倒腾半天,才在一个论坛上找到解决方法.笔记一下,如下: 转自:https://code.google.com/p/m ...
- java.lang.NumberFormatException
1.错误描述 Exception in thread "main" java.lang.NumberFormatException: For input string: " ...
- MyBatis报错:Caused by: java.lang.NumberFormatException: For input string: "XX"
<select id="sltTreatment" resultType="com.vitaminmd.sunny.core.bo.Treatment"& ...
- 异常java.lang.NumberFormatException解决
原因一:超出了int类型的取值范围 项目中要把十六进制字符串转化为十进制, 用到了到了Integer.parseInt(str1.trim(), 16):这个是不是后抛出java.lang.Numbe ...
- Caused by: java.lang.NumberFormatException: For input string: "18446744073709551615"
问题:Caused by: java.lang.NumberFormatException: For input string: "18446744073709551615" 原因 ...
随机推荐
- Flutter Platform Channels
Flutter Platform Channels(一) https://www.jianshu.com/p/33ac774f99b1 https://www.jianshu.com/p/c1e206 ...
- 【开发笔记】- Velocity中特殊符号展示乱码的问题
问题 需求是需要在后台将收货国家对应的币种.币种符号返回给前台并展示,在返回给前端后出现了页面币种符号展示乱码的问题. 解决方式 在获取货币符号时添加以下代码,防止velocity对特殊符号进行转义处 ...
- js文本对象模型[DOM]【续】(Node节点类型)
一.Document类型 document实例1.常用的一些属性documentElement 始终指向HTML页面中的<html>元素.body 直接指向<body>元 ...
- 英语DYAMAUND钻石DYAMAUND单词
dyamaund and the English words dyamaund The Vertu of the Dyamaund": Gemstones, Knowledge and Va ...
- Oracle间隔(interval)分区
(一)什么是间隔分区 间隔分区是Oracle 11.1引入的新功能,通过该功能,可以在输入相应分区的数据时自动创建相应的分区.在没有间隔分区技术之前,DBA通常会创建一个maxvalue分区以避免OR ...
- 使用ZeroClipboard 复制指定内容到剪切板
有些时候,我们希望让用户在网页上完成某个操作就能自动将指定的内容复制到用户计算机的剪贴板中.但是出于安全原因,大多数现代浏览器都未提供通用的剪贴板复制接口(或即便有,也默认被禁用).只有IE浏览器可以 ...
- CAD中如何将图形按一定的比例放大或缩小
1.双击CAD快捷键图标,打开CAD绘图界面: 2.以正五边形为例,点击左边的正多边形按钮: 3.绘制好后得到五边形图形: 4.给图形做好尺寸标注方便直观比较: 5.选择图像在命令行输入sc命令,按键 ...
- VMware15.5版本安装Windows_Server_2008_R2
VMware15.5版本安装Windows_Server_2008_R2一.从VMware15.5中新建虚拟机1.打开VMware,在首页面选择创建新的虚拟机. 2.新建虚拟机向导,选择典型配置. 3 ...
- 记一次对上传对jsp限制的绕过
当访问网站任何.jsp后缀的文件时都会显示如下图所示或者session timeout等提示, 并且网站防护会,对上传大马和一句话会被查杀. 解决方法: 利用jspx包含,利用jspx包含图片或者cs ...
- python笔记36-装饰器之wraps
前言 前面一篇对python装饰器有了初步的了解了,但是还不够完美,领导看了后又提出了新的需求,希望运行的日志能显示出具体运行的哪个函数. __name__和doc __name__用于获取函数的名称 ...