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…
How does your programming language handle “minus zero” (-0.0)?
41–50 of 219 posts
Re: How does your programming language handle “minus zero” (-0.0)?
#42Earlier 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.
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)?
#43Earlier 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.
Re: How does your programming language handle “minus zero” (-0.0)?
#44* 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)?
#45Re: How does your programming language handle “minus zero” (-0.0)?
#46Does 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.
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)?
#47Does 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.
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)?
#48Re: How does your programming language handle “minus zero” (-0.0)?
#49Does 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?
Re: How does your programming language handle “minus zero” (-0.0)?
#50There 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
I prefer to store all my most important data in the sign bit of floats set to zero.