Live data from Hacker News

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

lemire.me

31–40 of 219 posts

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

#31

Earlier quoted context omitted.

Computers (obviously) have to approximate the majority of actual mathematical numbers, as they do not have infinite storage. If you've got two numbers, +0.0000001 and -0.0000001, but you can't represent that precision, can you see how it's less bad to round to +0.0000 and -0.0000 rather than to just 0.0000? It's encoding strictly more information.

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 languages, special programming tricks may be needed to distinguish the two values

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

#32
post #13

The inverse of zero is infinity? Shouldn't it be undefined?

Floating point numbers represent the extended real set, where infinity exists. What is completely different from the integral numbers most computers use, on those division by zero is undefined.

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

#33

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.

Floating point math was devised to be used in numerical methods that are applied iteratively, like gradient descent. In that world what you are always doing is "converging" to solutions step by step, and a negative or positive zero can tell you which direction you're converging from.

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

#35
post #28

Earlier quoted context omitted.

Not if you know that it's positive or negative. The inverse of a negative infinitesimal can't be anything other than a negative infinite number, and the inverse of a positive infinitesimal can't be anything other than a positive infinite number. There's no difficulty with the definitions.

There is the difficulty that you don't actually have zero, you have postive and negative infinitesimal.

That is the meaning of "+0.0" and "-0.0". Actual zero is neither.

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

#36
post #30

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.

When does this need arise? Well, otherwise inverting a value can change it's sign and in particular inverting -∞ twice will give you +∞, and being off by "2∞" is a pretty large error for a lot of computations ;) You can end up with zeros and infinities pretty easily because you overflow or underflow the range of floating point precision, and generally you want something sensible to happen in typical cases, even if so…

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

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

#38
post #13

The inverse of zero is infinity? Shouldn't it be undefined?

Floating point numbers represent the extended real set, where infinity exists. What is completely different from the integral numbers most computers use, on those division by zero is undefined.

Division by zero is still undefined in the extended reals. To define it you'd need something like the projective reals. But it doesn't matter; floating point numbers don't have a zero; they have two zeroes of different signs.

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

#39
post #36
post #30

Earlier quoted context omitted.

When does this need arise? Well, otherwise inverting a value can change it's sign and in particular inverting -∞ twice will give you +∞, and being off by "2∞" is a pretty large error for a lot of computations ;) You can end up with zeros and infinities pretty easily because you overflow or underflow the range of floating point precision, and generally you want something sensible to happen in typical cases, even if so…

∞ 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.

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

#40
post #30

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.

When does this need arise? Well, otherwise inverting a value can change it's sign and in particular inverting -∞ twice will give you +∞, and being off by "2∞" is a pretty large error for a lot of computations ;) You can end up with zeros and infinities pretty easily because you overflow or underflow the range of floating point precision, and generally you want something sensible to happen in typical cases, even if so…

Posits handle this by using the smallest non-zero number where floating point would go to zero. They also use the largest represent able number instead of infinity. These exist for both positive and negative numbers. At least that's the way I read it.
Post reply on HN