問題描述
為什么原始浮點(diǎn)值可以是-0.0?這是什么意思?
我可以取消該功能嗎?
How come a primitive float value can be -0.0? What does that mean?
Can I cancel that feature?
當(dāng)我有:
float fl;
然后 fl == -0.0
返回 true
,fl == 0
也是如此.但是當(dāng)我打印它時(shí),它會(huì)打印 -0.0
.
Then fl == -0.0
returns true
and so does fl == 0
. But when I print it, it prints -0.0
.
推薦答案
因?yàn)?Java 使用 IEEE 浮點(diǎn)標(biāo)準(zhǔn)-Point Arithmetic (IEEE 754),它定義了 -0.0
以及何時(shí)應(yīng)該使用它.
Because Java uses the IEEE Standard for Floating-Point Arithmetic (IEEE 754) which defines -0.0
and when it should be used.
可表示的最小數(shù)在次正規(guī)有效數(shù)中沒有 1 位,由符號(hào)確定,稱為正或 負(fù)零.它實(shí)際上表示在零和相同符號(hào)的最小可表示非零數(shù)之間的范圍內(nèi)的數(shù)字四舍五入到零,這就是它有符號(hào)的原因,以及它的倒數(shù) +Inf 或 -Inf 也有符號(hào).
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浮點(diǎn)數(shù)的復(fù)雜性
涉及負(fù)零的操作
...
(-0.0) + 0.0 -> 0.0
Operations Involving Negative Zero
...
(-0.0) + 0.0 -> 0.0
-
當(dāng)浮點(diǎn)運(yùn)算導(dǎo)致負(fù)浮點(diǎn)數(shù)非常接近 0 以致無法正常表示時(shí),會(huì)產(chǎn)生-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.
這篇關(guān)于原始浮點(diǎn)值如何為-0.0?這意味著什么?的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!