Understanding the Presence of -0

Asked 2 years ago, Updated 2 years ago, 43 views

I think -0 exists for the float type, but I don't know why.
Do you use it for something or treat it as an error like inf or Nan?

c++ c

2022-09-29 20:27

2 Answers

Some of the benefits of having a sign at 0 are mentioned in What Every Computer Scientist Should Know About Floating-Point Artistic.

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 infunity, however that would result in the disruptive sequence of losing the sign of an overflowed quality.

The easiest thing to understand is that the sign is maintained in multiplication and division.For the same reason, infinity also has +Inf and -Inf.

Other mathematical functions that produce discontinuous and underflow near zero, such as log, and -0 are useful for complex number operations (as described in the document).

If a distinction were made when comparison +0 and -0, simple tests like if(x=0) would have very unreliable behavior, dependent on the sign of x.Thus the IEEE standard definitions so that +0=-0, that than-0<+0.
[...]
Altough distinguishing between +0 and -0 has avantages, it can occurably be confusing.For example, signed zero destroys the relationship x=y 11/x=1/y, which is false when x=+0 and y=-0. However, the IEEE commitment designated the timing of the rising

Another disadvantage is that there are two types: +0 and -0.In a floating-point comparison, +0 and -0 are not distinguished, but +0==-0.On the other hand, x==y and 1/x==1/y are no longer equivalent ( )) (1/+0!=1/-0 or +Inf!=-Inf).


2022-09-29 20:27

Maybe it's because the sign exists separately from the numerical representation (the mantissa part).
(In an integer type, -0 and +0 of two 0s occur in the case of a complement or a sign or an absolute value representation.)

Wikipedia's -0 may be helpful.
You should be able to treat it as 0 instead of an error.


2022-09-29 20:27

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.