java infinity 处理,是什么决定了Java除法运算中的Nan和Infinity

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);

}

Outputs is:

NaN

NaN

Infinity

Infinity

NaN

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

What is the deciding factor here ?

解决方案

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).

The purpose:

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:

In IEEE 754 arithmetic, a ÷ +0 is positive infinity when a is

positive, negative infinity when a is negative, and NaN when a = ±0.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值