问题描述
为什么原始浮点值可以是-0.0?这是什么意思?
我可以取消该功能吗?
How come a primitive float value can be -0.0? What does that mean?
Can I cancel that feature?
当我有:
float fl;
然后 fl == -0.0
返回 true
,fl == 0
也是如此.但是当我打印它时,它会打印 -0.0
.
Then fl == -0.0
returns true
and so does fl == 0
. But when I print it, it prints -0.0
.
推荐答案
因为 Java 使用 IEEE 浮点标准-Point Arithmetic (IEEE 754),它定义了 -0.0
以及何时应该使用它.
Because Java uses the IEEE Standard for Floating-Point Arithmetic (IEEE 754) which defines -0.0
and when it should be used.
可表示的最小数在次正规有效数中没有 1 位,由符号确定,称为正或 负零.它实际上表示在零和相同符号的最小可表示非零数之间的范围内的数字四舍五入到零,这就是它有符号的原因,以及它的倒数 +Inf 或 -Inf 也有符号.
The smallest number representable has no 1 bit in the subnormal significand and is called the positive or negative zero as determined by the sign. It actually represents a rounding to zero of numbers in the range between zero and the smallest representable non-zero number of the same sign, which is why it has a sign, and why its reciprocal +Inf or -Inf also has a sign.
您可以通过添加 0.0
例如
Double.toString(value + 0.0);
参见:Java浮点数的复杂性
涉及负零的操作
...
(-0.0) + 0.0 -> 0.0
Operations Involving Negative Zero
...
(-0.0) + 0.0 -> 0.0
-
当浮点运算导致负浮点数非常接近 0 以致无法正常表示时,会产生-0.0".
"-0.0" is produced when a floating-point operation results in a negative floating-point number so close to 0 that it cannot be represented normally.
这篇关于原始浮点值如何为-0.0?这意味着什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!