float型については-0が存在すると思いますが、なぜ存在するのかわかりません。
何かに利用されたりinfやNanのようにエラーとして扱うものですか
2 件の回答
0に符号があることのメリットは、What Every Computer Scientist Should Know About Floating-Point Arithmeticで幾つか言及されています。
When a multiplication or division involves a signed zero, the usual sign rules apply in computing the sign of the answer. Thus 3·(+0) = +0, and +0/-3 = -0. If zero did not have a sign, then the relation 1/(1/x) = x would fail to hold when x = ±∞. The reason is that 1/-∞ and 1/+∞ both result in 0, and 1/0 results in +∞, the sign information having been lost. One way to restore the identity 1/(1/x) = x is to only have one kind of infinity, however that would result in the disastrous consequence of losing the sign of an overflowed quantity.
一番理解しやすいのは「乗除算において符号が維持されること」でしょうか。同様の理由で、無限大にも+Inf
と-Inf
が存在します。
他には、logのように0近傍で不連続かつアンダーフローが生じる数学関数や、複素数演算で-0
が役立ちます(と同ドキュメントには書いてありました)。
If a distinction were made when comparing +0 and -0, simple tests like if (x = 0) would have very unpredictable behavior, depending on the sign of x. Thus the IEEE standard defines comparison so that +0 = -0, rather than -0 < +0.
[...]
Although distinguishing between +0 and -0 has advantages, it can occasionally be confusing. For example, signed zero destroys the relation x = y ⇔ 1/x = 1/y, which is false when x = +0 and y = -0. However, the IEEE committee decided that the advantages of utilizing the sign of zero outweighed the disadvantages.
+0
と-0
の2種類が存在することのデメリットもあります。浮動小数点数同士の比較では+0
と-0
は区別されず、+0 == -0
となります。一方で、x == y
と1/x == 1/y
は同値(⇔)でなくなってしまいます(1/+0 != 1/-0
つまり+Inf != -Inf
)。