問題描述
為什么原始浮點值可以是-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?這意味著什么?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!