Live data from Hacker News

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

lemire.me

11–20 of 219 posts

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

#11
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?

I believe that's actually a standard presentation, I've seen it in several weather graphs. It's basically a rounding yeah, signifying it's just slightly below zero, but not enough to round to -1°.

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

#12

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.

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

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

#14

Elixir does not distinguish signs in zero, 0. Therefore; -0.0 + 2.0 = 2.0 and 0.0 + 2.0 = 2.0

I think you're right that they're indistinguishable, in that -0.0 == 0.0

The division by 0 in that article gives the same ArithmeticError in each case, notice the 0.0 in both errors:

    iex(8)> 1.0 / -0.0
    ** (ArithmeticError) bad argument in arithmetic expression: 1.0 / 0.0
        :erlang./(1.0, 0.0)
    iex(8)> 1.0/0.0
    ** (ArithmeticError) bad argument in arithmetic expression: 1.0 / 0.0
        :erlang./(1.0, 0.0)
It's also ignored when rounding:

    iex(10)> Float.round(-0.01, 1)
    0.0
    iex(11)> Float.round(0.01, 1)
    0.0

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

#15
post #6

Elixir does not distinguish signs in zero, 0. Therefore; -0.0 + 2.0 = 2.0 and 0.0 + 2.0 = 2.0

Isn't that true for all correct implementations of negative zero? It's still a neutral for addition

Not quite neutral. You return -0.0 only when needed.

  >>> 0.0+0.0
  0.0
  >>> 0.0+(-0.0)
  0.0
  >>> (-0.0)+0.0
  0.0
  >>> (-0.0)+(-0.0)
  -0.0

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

#16

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.

0 does double duty as meaning itself and to indicate underflow due to multiplying very small numbers.

If you have a function that can experience underflow it can be useful to preserve the sign of the underflowing value.

Otherwise you'd need more checks to get that information.

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

#17
post #4

Earlier quoted context omitted.

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

Sometimes the sign is used to indicate which "direction" the temperature is moving. If it was -10° overnight and it's -0° now, the puddles outside will still be frozen. If it was 10° overnight and it's 0° now, the puddles will still be liquid. (Edit: no idea whether this applies to the Apple watch, it's just a use case for -0 with regards to temperature.)

Your predictions about the state of the puddle are most likely right, but not for the reasons that you think.

Air temperature is commonly measured at 2m above ground. An measurement of 0° air temperature does not imply 0° ground temperature. The more significant effect is that the ground has a higher thermal capacity than the air, so it changes temperature more slowly throughout the day. If it's been colder before and now it's 0°, the ground is still way below 0° and thus puddles are frozen. If it was warmer and now the air has cooled down to 0°, the ground is going to be a bit warmer still and thus puddles are liquid.

Denoting this difference as +0° and -0° does not seem very useful since the same effect is going to be nearly equally significant at 1° or -2°.

(Sidenote: The thermal capacity of the ground is also the reason why air temperature is not measured anywhere near the ground.)

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

#18

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.

It’s used when writing SIMD code all the time to quickly mask bits as needed or to check the sign of a floating point number with an SSE intrinsic. _mm_xor_ps(_mm_set1_ps(-0.f), reg) as an example negates all four components of reg.

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

#19

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.

Exactly for things like OP’s argument: 1/-0 is -inf, and that may be important in asymptotics

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

#20

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

[deleted]
Post reply on HN