热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

为什么Java除以0.0时不会抛出异常?-Whydoesn'tJavathrowanExceptionwhendividingby0.0?

Ihavecodetocalculatethepercentagedifferencebetween2numbers-(oldNum-newNum)oldNum*

I have code to calculate the percentage difference between 2 numbers - (oldNum - newNum) / oldNum * 100; - where both of the numbers are doubles. I expected to have to add some sort of checking / exception handling in case oldNum is 0. However, when I did a test run with values of 0.0 for both oldNum and newNum, execution continued as if nothing had happened and no error was thrown. Running this code with ints would definitely cause an arithmetic division-by-zero exception. Why does Java ignore it when it comes to doubles?

我有代码来计算2个数字之间的百分比差异 - (oldNum - newNum)/ oldNum * 100; - 两个数字都是双打的。我希望在oldNum为0的情况下添加某种检查/异常处理。但是,当我为oldNum和newNum进行了值为0.0的测试运行时,执行继续,好像什么都没发生并且没有抛出错误。使用int运行此代码肯定会导致算术除零异常。为什么Java在双打时会忽略它?

5 个解决方案

#1


43  

The result of division by zero is, mathematically speaking, undefined, which can be expressed with a float/double (as NaN - not a number), it isn't, however, wrong in any fundamental sense.

从数学上讲,除以零的结果是未定义的,可以用float / double表示(如NaN - 不是数字),但在任何基本意义上都不是错误的。

As an integer must hold a specific numerical value, an error must be thrown on division by zero when dealing with them.

由于整数必须包含特定的数值,因此在处理它们时必须将除以零的错误抛出。

#2


106  

Java's float and double types, like pretty much any other language out there (and pretty much any hardware FP unit), implement the IEEE 754 standard for floating point math, which mandates division by zero to return a special "infinity" value. Throwing an exception would actually violate that standard.

Java的float和double类型,就像其他任何语言(以及几乎任何硬件FP单元)一样,实现了浮点数学的IEEE 754标准,它要求除以零以返回特殊的“无穷大”值。抛出异常实际上会违反该标准。

Integer arithmetic (implemented as two's complement representation by Java and most other languages and hardware) is different and has no special infinity or NaN values, thus throwing exceptions is a useful behaviour there.

整数算术(由Java和大多数其他语言和硬件实现为二进制补码表示)是不同的,没有特殊的无穷大或NaN值,因此抛出异常是一种有用的行为。

#3


4  

The way a double is stored is quite different to an int. See http://firstclassthoughts.co.uk/java/traps/java_double_traps.html for a more detailed explanation on how Java handles double calculations. You should also read up on Floating Point numbers, in particular the concept of Not a Number (NaN).

存储double的方式与int完全不同。有关Java如何处理双重计算的更详细说明,请参见http://firstclassthoughts.co.uk/java/traps/java_double_traps.html。您还应该阅读浮点数,特别是非数字(NaN)的概念。

If you're interested in learning more about floating point representation, I'd advise reading this document (Word format, sorry). It delves into the binary representation of numbers, which may be helpful to your understanding.

如果您有兴趣了解有关浮点表示的更多信息,我建议您阅读本文档(Word格式,抱歉)。它深入研究数字的二进制表示,这可能有助于您的理解。

#4


3  

Though Java developers know about the double primitive type and Double class, while doing floating point arithmetic they don't pay enough attention to Double.INFINITY, NaN, -0.0 and other rules that govern the arithmetic calculations involving them.

虽然Java开发人员知道双原语类型和Double类,但在进行浮点运算时,他们没有足够重视Double.INFINITY,NaN,-0.0和其他规则来控制涉及它们的算术计算。

The simple answer to this question is that it will not throw ArithmeticException and return Double.INFINITY. Also, note that the comparison x == Double.NaN always evaluates to false, even if x itself is a NaN.

这个问题的简单答案是它不会抛出ArithmeticException并返回Double.INFINITY。另外,请注意,即使x本身是NaN,比较x == Double.NaN也始终求值为false。

To test if x is a NaN, one should use the method call Double.isNaN(x) to check if given number is NaN or not. This is very close to NULL in SQL.

要测试x是否为NaN,应该使用方法调用Double.isNaN(x)来检查给定的数字是否为NaN。这在SQL中非常接近NULL。

It may helpful for you.

它可能对你有帮助。

#5


1  

When divided by zero ( 0 or 0.00 )

除以零(0或0.00)时

  1. If you divide double by 0, JVM will show Infinity.

    如果将double除以0,JVM将显示Infinity。

    public static void main(String [] args){ double a=10.00; System.out.println(a/0); }

    public static void main(String [] args){double a = 10.00;的System.out.println(A / 0); }

    Console: Infinity

    控制台:无限

  2. If you divide int by 0, then JVM will throw Arithmetic Exception.

    如果将int除以0,则JVM将抛出算术异常。

    public static void main(String [] args){ int a=10; System.out.println(a/0); }

    public static void main(String [] args){int a = 10;的System.out.println(A / 0); }

    Console: Exception in thread "main" java.lang.ArithmeticException: / by zero

    控制台:线程“main”中的异常java.lang.ArithmeticException:/ by zero

  3. But if we divide int by 0.0, then JVM will show Infinity:

    但是如果我们将int除以0.0,那么JVM将显示Infinity:

    public static void main(String [] args){ int a=10; System.out.println(a/0.0); }

    public static void main(String [] args){int a = 10;的System.out.println(A / 0.0); }

    Console: Infinity

    控制台:无限

This is because JVM will automatically type cast int to double, so we get infinity instead of ArithmeticException.

这是因为JVM会自动将cast int类型化为double,因此我们得到无穷大而不是ArithmeticException。


推荐阅读
  • Python正则表达式学习记录及常用方法
    本文记录了学习Python正则表达式的过程,介绍了re模块的常用方法re.search,并解释了rawstring的作用。正则表达式是一种方便检查字符串匹配模式的工具,通过本文的学习可以掌握Python中使用正则表达式的基本方法。 ... [详细]
  • 微软头条实习生分享深度学习自学指南
    本文介绍了一位微软头条实习生自学深度学习的经验分享,包括学习资源推荐、重要基础知识的学习要点等。作者强调了学好Python和数学基础的重要性,并提供了一些建议。 ... [详细]
  • 本文介绍了设计师伊振华受邀参与沈阳市智慧城市运行管理中心项目的整体设计,并以数字赋能和创新驱动高质量发展的理念,建设了集成、智慧、高效的一体化城市综合管理平台,促进了城市的数字化转型。该中心被称为当代城市的智能心脏,为沈阳市的智慧城市建设做出了重要贡献。 ... [详细]
  • 向QTextEdit拖放文件的方法及实现步骤
    本文介绍了在使用QTextEdit时如何实现拖放文件的功能,包括相关的方法和实现步骤。通过重写dragEnterEvent和dropEvent函数,并结合QMimeData和QUrl等类,可以轻松实现向QTextEdit拖放文件的功能。详细的代码实现和说明可以参考本文提供的示例代码。 ... [详细]
  • Linux重启网络命令实例及关机和重启示例教程
    本文介绍了Linux系统中重启网络命令的实例,以及使用不同方式关机和重启系统的示例教程。包括使用图形界面和控制台访问系统的方法,以及使用shutdown命令进行系统关机和重启的句法和用法。 ... [详细]
  • android listview OnItemClickListener失效原因
    最近在做listview时发现OnItemClickListener失效的问题,经过查找发现是因为button的原因。不仅listitem中存在button会影响OnItemClickListener事件的失效,还会导致单击后listview每个item的背景改变,使得item中的所有有关焦点的事件都失效。本文给出了一个范例来说明这种情况,并提供了解决方法。 ... [详细]
  • sklearn数据集库中的常用数据集类型介绍
    本文介绍了sklearn数据集库中常用的数据集类型,包括玩具数据集和样本生成器。其中详细介绍了波士顿房价数据集,包含了波士顿506处房屋的13种不同特征以及房屋价格,适用于回归任务。 ... [详细]
  • ALTERTABLE通过更改、添加、除去列和约束,或者通过启用或禁用约束和触发器来更改表的定义。语法ALTERTABLEtable{[ALTERCOLUMNcolu ... [详细]
  • Java太阳系小游戏分析和源码详解
    本文介绍了一个基于Java的太阳系小游戏的分析和源码详解。通过对面向对象的知识的学习和实践,作者实现了太阳系各行星绕太阳转的效果。文章详细介绍了游戏的设计思路和源码结构,包括工具类、常量、图片加载、面板等。通过这个小游戏的制作,读者可以巩固和应用所学的知识,如类的继承、方法的重载与重写、多态和封装等。 ... [详细]
  • IB 物理真题解析:比潜热、理想气体的应用
    本文是对2017年IB物理试卷paper 2中一道涉及比潜热、理想气体和功率的大题进行解析。题目涉及液氧蒸发成氧气的过程,讲解了液氧和氧气分子的结构以及蒸发后分子之间的作用力变化。同时,文章也给出了解题技巧,建议根据得分点的数量来合理分配答题时间。最后,文章提供了答案解析,标注了每个得分点的位置。 ... [详细]
  • 本文讨论了在Windows 8上安装gvim中插件时出现的错误加载问题。作者将EasyMotion插件放在了正确的位置,但加载时却出现了错误。作者提供了下载链接和之前放置插件的位置,并列出了出现的错误信息。 ... [详细]
  • CSS3选择器的使用方法详解,提高Web开发效率和精准度
    本文详细介绍了CSS3新增的选择器方法,包括属性选择器的使用。通过CSS3选择器,可以提高Web开发的效率和精准度,使得查找元素更加方便和快捷。同时,本文还对属性选择器的各种用法进行了详细解释,并给出了相应的代码示例。通过学习本文,读者可以更好地掌握CSS3选择器的使用方法,提升自己的Web开发能力。 ... [详细]
  • http:my.oschina.netleejun2005blog136820刚看到群里又有同学在说HTTP协议下的Get请求参数长度是有大小限制的,最大不能超过XX ... [详细]
  • 本文介绍了Windows操作系统的版本及其特点,包括Windows 7系统的6个版本:Starter、Home Basic、Home Premium、Professional、Enterprise、Ultimate。Windows操作系统是微软公司研发的一套操作系统,具有人机操作性优异、支持的应用软件较多、对硬件支持良好等优点。Windows 7 Starter是功能最少的版本,缺乏Aero特效功能,没有64位支持,最初设计不能同时运行三个以上应用程序。 ... [详细]
  • 本文介绍了如何使用Express App提供静态文件,同时提到了一些不需要使用的文件,如package.json和/.ssh/known_hosts,并解释了为什么app.get('*')无法捕获所有请求以及为什么app.use(express.static(__dirname))可能会提供不需要的文件。 ... [详细]
author-avatar
小葵小小葵_530
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有