Live data from Hacker News

How does your programming language handle “minus zero” (-0.0)?

lemire.me

41–50 of 219 posts

Re: How does your programming language handle “minus zero” (-0.0)?

#41

Earlier quoted context omitted.

Ok, so what's true? -0<+0 or -0==+0

You shouldn't really use equality on floating point numbers, except on very special circumstances (and I imagine the == behavior for 0 breaks things more often than it helps). But the wikipedia page on -0 has your case covered: > According to the IEEE 754 standard, negative zero and positive zero should compare as equal with the usual (numerical) comparison operators, like the == operators of C and Java. In those lan…

IEEE 754 also defines a total order of all fp values, where -0.0 immediately precedes 0.0

Re: How does your programming language handle “minus zero” (-0.0)?

#42
post #36

Earlier quoted context omitted.

∞ is not a number. Using it as a number is a hack invented by mathematicians.

∞ is not used as a number by mathematicians. Maybe by engineers.

That is not entirely correct. Schmieden and Laugwitz for example developed in the 1950s a nonstandard Analysis which adjoins an infinitely large element (called Ω) to the natural numbers. The basic idea was a formula A(Ω) was true if A(n) was true for almost all finite natural n.

While it wasn't immensely useful going forward, it helped to clarify the use of infinity and infinitesimals in earlier work.

Re: How does your programming language handle “minus zero” (-0.0)?

#43

Earlier quoted context omitted.

∞ is not used as a number by mathematicians. Maybe by engineers.

That is not entirely correct. Schmieden and Laugwitz for example developed in the 1950s a nonstandard Analysis which adjoins an infinitely large element (called Ω) to the natural numbers. The basic idea was a formula A(Ω) was true if A(n) was true for almost all finite natural n. While it wasn't immensely useful going forward, it helped to clarify the use of infinity and infinitesimals in earlier work.

I'm well aware of nonstandard analysis, but ∞ is still not a number there, even though there are infinitely many infinitely large elements .

Re: How does your programming language handle “minus zero” (-0.0)?

#44
It's also worth understanding whether +0.0 and -0.0 are equivalent in your environment:

* Do they compare equal?

* If they're convertible to boolean, do they both evaluate to false?

* If you serialize/deserialize the value (e.g., to JSON and back), does it maintain the distinction?

* If you send this value to a database (esp. one with stored procedures), how does its behavior compare to your language?

Re: How does your programming language handle “minus zero” (-0.0)?

#46

Does anyone have a good example of a programming problem where we _need_ signed zero? Or where it makes things significantly simpler? As far as I know, there is no distinction between -0 and +0 in math, so I have never really understood why this is a thing in computers.

I agree with many of the other replies, but I would also add, perfectly serious and with no sarcasm, do not be fooled; computers do not use "math" numbers. They use what they use; machine-word bounded integers and IEEE floats most commonly, unbounded integers, some variants on rational numbers, and sometimes some more exotic things, but they never use real numbers. They're bounded to the computable numbers.

So whether or not there's a -0 or +0 in some "math" isn't relevant, because computers aren't using "some math" but very specific mathematical constructs that must be understood on their own terms. I haven't seen very many mathematical systems that have a reified "NaN" object that can be explicitly passed around as a valid value to functions. (I know of many systems that have a "bottom" but I would say bottom is usually presented not as an object you can "have" but as a property of some mathematical object. Haskell for instance uses this idea; there are some trivial ways to have or produce something that has the property of being "bottom", like calling "error", but you can't just "have the bottom value".)

Moreover, there are mathematical systems in which such things can appear. There are many exotic and interesting number systems in the mathematical world, which even a bachelor's degree in mathematics may only scratch the surface of, depending on which junior/senior level courses you take. Real numbers are without a doubt the most studied, and they do not contain a -0 (though even then you'll still see it show up sometimes as a special notation in limits), but they are the beginning of number systems, not the end.

I mention all this because it's important; it's a very common misconception that computers use the numbers like you learned in school, and it will lead to nothing but pain. The next misconception is that, ok, sure, they aren't those numbers exactly, but they're so close that I don't have to worry about it. This works as long as you don't push your floats too hard, and a lot of us don't, but also breaks down surprisingly quickly. It's important for programming professionals to understand in general that IEEE floats are their own thing. Whenever I use them I do at least take a couple of seconds to double-check mentally that the sharp pointy bits aren't going to stab me, even for simple things like adding durations of a request to some floating point accumulator for metric purposes.

Re: How does your programming language handle “minus zero” (-0.0)?

#47

Does anyone have a good example of a programming problem where we _need_ signed zero? Or where it makes things significantly simpler? As far as I know, there is no distinction between -0 and +0 in math, so I have never really understood why this is a thing in computers.

Sign/magnitude ADCs almost have signed zero in hardware, by generating a sign bit and zero or more magnitude bits. The typical method to map to voltages doubles the span between adjacent values and skips over zero. So a 2-bit sign/magnitude ADC measures values in the series {-3, -1, 1, 3}.

So strictly speaking this number system doesn't have a representation of zero at all. Tiny measurements either round up or round down to +/- 1.

Re: How does your programming language handle “minus zero” (-0.0)?

#49
post #4

Does anyone have a good example of a programming problem where we _need_ signed zero? Or where it makes things significantly simpler? As far as I know, there is no distinction between -0 and +0 in math, so I have never really understood why this is a thing in computers.

Amusingly enough the Apple Watch reports temperature as 0° and sometimes as -0°, I've not determined what causes that, perhaps rounding around 0?

Maybe the actual temperature value has a few more digits that are truncated during formatting: "-0.001" -> "-0"

Re: How does your programming language handle “minus zero” (-0.0)?

#50
post #45

There are still (a few) computers in the world (Sperry Univac legacy support) using one's complement arithmetic, hence having a positive and negative zero in the architecture. https://en.wikipedia.org/wiki/Ones%27_complement

Well all computers supporting IEEE 754 floating point numbers have a positive and a negative 0. Thats quite a few.

I prefer to store all my most important data in the sign bit of floats set to zero.

Post reply on HN