Java Float.isNaN Double.isNaN方法工作原理
2016-08-11 16:56
441 查看
From Java Language Specification:
Floating-point equality testing is performed in accordance with the rules of the IEEE 754 standard:
If either operand is NaN, then the result of == is false but the result of != is true. Indeed, the test x!=x is true if and only if the value of x is NaN. (The methods Float.isNaN and Double.isNaN may also be used to test whether a value is NaN.)
Positive zero and negative zero are considered equal. Therefore, -0.0==0.0 is true, for example.
Otherwise, two distinct floating-point values are considered unequal by the equality operators. In particular, there is one value representing positive infinity and one value representing negative infinity; each compares equal
only to itself, and each compares unequal to all other values.
Floating-point equality testing is performed in accordance with the rules of the IEEE 754 standard:
If either operand is NaN, then the result of == is false but the result of != is true. Indeed, the test x!=x is true if and only if the value of x is NaN. (The methods Float.isNaN and Double.isNaN may also be
used to test whether a value is NaN.)
Positive zero and negative zero are considered equal. Therefore, -0.0==0.0 is true, for example.
Otherwise, two distinct floating-point values are considered unequal by the equality operators. In particular, there is one value representing positive infinity and one value representing negative infinity; each compares equal only to itself, and each compares
unequal to all other values.
That method can return true for certain operations, for example:
Basically,
an undefined value. The value of
and
gives you
See the javadoc of
It is equivalent to the value returned by
And then
If the argument is any value in the range
in the range
the result is a
No IEEE 754 floating-point operation provided by Java can distinguish between two NaN values of the same type with different bit patterns.
From Java Language Specification:
Floating-point equality testing is performed in accordance with the rules of the IEEE 754 standard:
If either operand is NaN, then the result of == is false but the result of != is true. Indeed, the test x!=x is true if and only if the value of x is NaN. (The methods Float.isNaN and Double.isNaN may also be used to test whether a value is NaN.)
Positive zero and negative zero are considered equal. Therefore, -0.0==0.0 is true, for example.
Otherwise, two distinct floating-point values are considered unequal by the equality operators. In particular, there is one value representing positive infinity and one value representing negative infinity; each compares equal only to itself, and each compares
unequal to all other values.
Floating-point equality testing is performed in accordance with the rules of the IEEE 754 standard:
If either operand is NaN, then the result of == is false but the result of != is true. Indeed, the test x!=x is true if and only if the value of x is NaN. (The methods Float.isNaN and Double.isNaN may also be used to test whether a value is NaN.)
Positive zero and negative zero are considered equal. Therefore, -0.0==0.0 is true, for example.
Otherwise, two distinct floating-point values are considered unequal by the equality operators. In particular, there is one value representing positive infinity and one value representing negative infinity; each compares equal
only to itself, and each compares unequal to all other values.
Floating-point equality testing is performed in accordance with the rules of the IEEE 754 standard:
If either operand is NaN, then the result of == is false but the result of != is true. Indeed, the test x!=x is true if and only if the value of x is NaN. (The methods Float.isNaN and Double.isNaN may also be
used to test whether a value is NaN.)
Positive zero and negative zero are considered equal. Therefore, -0.0==0.0 is true, for example.
Otherwise, two distinct floating-point values are considered unequal by the equality operators. In particular, there is one value representing positive infinity and one value representing negative infinity; each compares equal only to itself, and each compares
unequal to all other values.
That method can return true for certain operations, for example:
System.out.println(Float.isNaN(0.0f / 0.0f)); System.out.println(Double.isNaN(Math.sqrt(-1)));
Basically,
NaNrepresents
an undefined value. The value of
0.0 / 0.0is
NaN,
and
Nan != NaN. It may seem logical because
Math.sqrt(-1)also
gives you
NaN.
See the javadoc of
Double.NaN:
It is equivalent to the value returned by
Double.longBitsToDouble(0x7ff8000000000000L)
And then
Double.longBitsToDouble():
If the argument is any value in the range
0x7ff0000000000001Lthrough
0x7fffffffffffffffLor
in the range
0xfff0000000000001Lthrough
0xffffffffffffffffL,
the result is a
NaN.
No IEEE 754 floating-point operation provided by Java can distinguish between two NaN values of the same type with different bit patterns.
From Java Language Specification:
Floating-point equality testing is performed in accordance with the rules of the IEEE 754 standard:
If either operand is NaN, then the result of == is false but the result of != is true. Indeed, the test x!=x is true if and only if the value of x is NaN. (The methods Float.isNaN and Double.isNaN may also be used to test whether a value is NaN.)
Positive zero and negative zero are considered equal. Therefore, -0.0==0.0 is true, for example.
Otherwise, two distinct floating-point values are considered unequal by the equality operators. In particular, there is one value representing positive infinity and one value representing negative infinity; each compares equal only to itself, and each compares
unequal to all other values.
相关文章推荐
- java中double和float精度丢失问题及解决方法
- [JAVA] float,double大数据不要科学计数方法显示
- JAVA对double或者 float的浮点数精度计算控制方法
- java中double和float精度丢失问题及解决方法
- Java 对double或者float的浮点数精度计算控制方法
- java中double和float精度丢失问题及解决方法
- Float.isNaN() 方法
- JAVA对double或者float的浮点数精度计算控制方法
- [JAVA] float,double计算方法
- java使double/float保留两位小数的多方法 java保留两位小数
- JAVA对double或者float的浮点数精度计算控制方法
- 创建一个名为myinput.java的程序:包含从键盘读取 int、double、float、boolean、short、byte和string值的方法
- java.math.BigDecimal 类型,float、double得出结果NaN
- Java中double和float精度丢失问题及解决方法
- JAVA float double数据类型保留2位小数点5种方法
- JAVA对double或者float的浮点数精度计算控制方法
- 出现java.lang.IllegalArgumentException: id to load is required for loading的解决方法
- java中数据类型转换 Integer String Long Float Double Date
- java中数据类型转换 Integer String Long Float Double Date Char
- JAVA中控制double类型小数点后位数的方法