問題描述
為什么雙打有 -0
和 +0
?有什么背景和意義?
-0
(通常)被視為 0
*******.當(dāng) negative 浮點(diǎn)數(shù)非常接近于零以至于它可以被認(rèn)為是 0
時(shí),它可能會(huì)導(dǎo)致(要清楚,我指的是 )
用數(shù)學(xué)術(shù)語:
這說明了 0
和 -0
在計(jì)算意義上的一個(gè)顯著區(qū)別.
這里有一些相關(guān)資源,其中一些已經(jīng)提出.為了完整起見,我將它們包括在內(nèi):
- 關(guān)于簽名零的維基百科文章
- "每個(gè)計(jì)算機(jī)科學(xué)家都應(yīng)該知道的浮點(diǎn)運(yùn)算知識(shí)"(參見有符號(hào)零部分)
- (PDF) "Much Ado About Nothing's Sign Bit" - 一篇有趣的論文由 W. Kahan 撰寫.
Why do doubles have -0
as well as +0
? What is the background and significance?
-0
is (generally) treated as 0
*******. It can result when a negative floating-point number is so close to zero that it can be considered 0
(to be clear, I'm referring to arithmetic underflow, and the results of the following computations are interpreted as being exactly ±0
, not just really small numbers). e.g.
System.out.println(-1 / Float.POSITIVE_INFINITY);
-0.0
If we consider the same case with a positive number, we will receive our good old 0
:
System.out.println(1 / Float.POSITIVE_INFINITY);
0.0
******* Here's a case where using -0.0
results in something different than when using 0.0
:
System.out.println(1 / 0.0);
System.out.println(1 / -0.0);
Infinity -Infinity
This makes sense if we consider the function 1 / x
. As x
approaches 0
from the +
-side, we should get positive infinity, but as it approaches from the -
-side, we should get negative infinity. The graph of the function should make this clear:
(source)
In math-terms:
This illustrates one significant difference between 0
and -0
in the computational sense.
Here are some relevant resources, some of which have been brought up already. I've included them for the sake of completeness:
- Wikipedia article on signed zero
- "What Every Computer Scientist Should Know About Floating-Point Arithmetic" (See Signed Zero section)
- (PDF) "Much Ado About Nothing's Sign Bit" - an interesting paper by W. Kahan.
這篇關(guān)于為什么浮點(diǎn)數(shù)有符號(hào)零?的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!