是什么决定了Java除法运算中的Nan和Infinity [英] What decides Nan and Infinity in java division operations

查看:603
本文介绍了是什么决定了Java除法运算中的Nan和Infinity的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下代码的输出使我感到困惑。为什么有时 NaN 有时是

Output of the below code confusing me. Why NaN sometimes and Infinity other times ?

public static void main (String[] args) {

    double a = 0.0;
    double b = 1.0;
    int c = 0; 
    System.out.println(a/0.0);
    System.out.println(a/0);
    System.out.println(b/0.0);
    System.out.println(b/0);
    System.out.println(c/0.0);
    System.out.println(c/0);
}

输出为:

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

这里的决定因素是什么?

What is the deciding factor here ?

推荐答案

这是因为 IEEE浮点算法标准(IEEE 754),它是1985年由浮点运算所建立的技术标准。电气和电子工程师协会(IEEE)。

This is because of The IEEE Standard for Floating-Point Arithmetic (IEEE 754) which is a technical standard for floating-point computation established in 1985 by the Institute of Electrical and Electronics Engineers (IEEE).


IEEE浮点标准..指定每个浮点
点算术运算(包括除以零的 都具有
井)定义的结果
。该标准支持带符号的零以及
infinity NaN (不是数字)。有两个零:+0(正零)和-0(负零),并且此 除以除 时的
模糊度。

The IEEE floating-point standard,.. specifies that every floating point arithmetic operation, including division by zero, has a well-defined result. The standard supports signed zero, as well as infinity and NaN (not a number). There are two zeroes: +0 (positive zero) and −0 (negative zero) and this removes any ambiguity when dividing.






规则:




The Rule:


在IEEE 754算法中,当 a 时, a÷+0 正无穷大。是 b 负无穷大(当 a 和<当 a =±0时,strong> NaN 。

In IEEE 754 arithmetic, a ÷ +0 is positive infinity when a is positive, negative infinity when a is negative, and NaN when a = ±0.






来源

这篇关于是什么决定了Java除法运算中的Nan和Infinity的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆