Live data from Hacker News

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

lemire.me

1–10 of 219 posts

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

#2
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.

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

#3

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.

Negative zero was introduced because of branch cuts: https://people.freebsd.org/~das/kahan86branch.pdf

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

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

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

#7

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.

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

#8

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

I don't understand what you think a result could look like here that does distinguish the sign? What are you expecting? -2.0? That doesn't make any sense. 2.0 is the correct answer under all circumstances as far as I can see.

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

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

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

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

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

Probably not even rounding, but just printf. The following works on my zsh:

  $ printf '%.0f°\n' 0.3
  0°
  $ printf '%.0f°\n' -0.3
  -0°
Post reply on HN