【发布时间】:2014-01-05 03:06:28
【问题描述】:
看过Double.java的源码和一些常量是这样的
/**
* Constant for the Not-a-Number (NaN) value of the {@code double} type.
*/
public static final double NaN = 0.0 / 0.0;
/**
* Constant for the positive infinity value of the {@code double} type.
*/
public static final double POSITIVE_INFINITY = 1.0 / 0.0;
/**
* Constant for the negative infinity value of the {@code double} type.
*/
public static final double NEGATIVE_INFINITY = -1.0 / 0.0;
但我想知道为什么它不抛出 ArithmeticException(除以零)?
我试过了
public static final int VALUE = 0/0;
现在它正在抛出异常,但是当我说时
public static final double VALUE = 0d/0d;
它没有抛出异常...
Double 有什么魔力,为什么它不抛出异常?
【问题讨论】:
-
因为双 0 永远不是 0,它通常用 NaN 表示
-
可能是因为0d并不完全是0,而是一个非常接近的值
-
@blackbelt你能解释清楚吗...如果0不是0当它是double时,那么0的值是多少?
-
@GopalRao 见斯蒂芬回答
标签: java android exception math