<i id='pau8s'><tr id='pau8s'><dt id='pau8s'><q id='pau8s'><span id='pau8s'><b id='pau8s'><form id='pau8s'><ins id='pau8s'></ins><ul id='pau8s'></ul><sub id='pau8s'></sub></form><legend id='pau8s'></legend><bdo id='pau8s'><pre id='pau8s'><center id='pau8s'></center></pre></bdo></b><th id='pau8s'></th></span></q></dt></tr></i><div id='pau8s'><tfoot id='pau8s'></tfoot><dl id='pau8s'><fieldset id='pau8s'></fieldset></dl></div>
      <tfoot id='pau8s'></tfoot>
      <legend id='pau8s'><style id='pau8s'><dir id='pau8s'><q id='pau8s'></q></dir></style></legend>

      <small id='pau8s'></small><noframes id='pau8s'>

        • <bdo id='pau8s'></bdo><ul id='pau8s'></ul>

        为什么Java除以0.0时不抛出异常?

        时间:2023-10-01

          <tbody id='DbTuJ'></tbody>

        • <small id='DbTuJ'></small><noframes id='DbTuJ'>

            <bdo id='DbTuJ'></bdo><ul id='DbTuJ'></ul>
              <i id='DbTuJ'><tr id='DbTuJ'><dt id='DbTuJ'><q id='DbTuJ'><span id='DbTuJ'><b id='DbTuJ'><form id='DbTuJ'><ins id='DbTuJ'></ins><ul id='DbTuJ'></ul><sub id='DbTuJ'></sub></form><legend id='DbTuJ'></legend><bdo id='DbTuJ'><pre id='DbTuJ'><center id='DbTuJ'></center></pre></bdo></b><th id='DbTuJ'></th></span></q></dt></tr></i><div id='DbTuJ'><tfoot id='DbTuJ'></tfoot><dl id='DbTuJ'><fieldset id='DbTuJ'></fieldset></dl></div>
            • <tfoot id='DbTuJ'></tfoot>
              <legend id='DbTuJ'><style id='DbTuJ'><dir id='DbTuJ'><q id='DbTuJ'></q></dir></style></legend>

                  本文介绍了为什么Java除以0.0时不抛出异常?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我有代码来计算两个数字之间的百分比差异 - (oldNum - newNum)/oldNum * 100; - 其中两个数字都是 doubles.我希望在 oldNum 为 0 的情况下必须添加某种检查/异常处理.但是,当我对 oldNum 和 newNum 的值都为 0.0 进行测试运行时,执行继续进行,就好像什么都没发生并且没有引发错误一样.使用 ints 运行此代码肯定会导致算术除零异常.为什么Java在涉及到doubles时会忽略它?

                  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?

                  推荐答案

                  除以零的结果在数学上是undefined,可以用float/double表示(如NaN - 不是一个数字),但它在任何基本意义上都没有错.

                  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.

                  由于整数必须包含特定的数值,因此在处理它们时必须在除以零时引发错误.

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

                  这篇关于为什么Java除以0.0时不抛出异常?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:int 类型的字面量 xyz 超出范围 下一篇:在Java中将布尔值转换为int

                  相关文章

                  最新文章

                  1. <i id='VEcU8'><tr id='VEcU8'><dt id='VEcU8'><q id='VEcU8'><span id='VEcU8'><b id='VEcU8'><form id='VEcU8'><ins id='VEcU8'></ins><ul id='VEcU8'></ul><sub id='VEcU8'></sub></form><legend id='VEcU8'></legend><bdo id='VEcU8'><pre id='VEcU8'><center id='VEcU8'></center></pre></bdo></b><th id='VEcU8'></th></span></q></dt></tr></i><div id='VEcU8'><tfoot id='VEcU8'></tfoot><dl id='VEcU8'><fieldset id='VEcU8'></fieldset></dl></div>

                      <tfoot id='VEcU8'></tfoot>

                      <small id='VEcU8'></small><noframes id='VEcU8'>

                    1. <legend id='VEcU8'><style id='VEcU8'><dir id='VEcU8'><q id='VEcU8'></q></dir></style></legend>
                        <bdo id='VEcU8'></bdo><ul id='VEcU8'></ul>