Hacker News new | past | comments | ask | show | jobs | submit login

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.


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


> Not quite neutral.

Hmm? Your examples show -0.0 being additively neutral in every case. +0.0 is the one that behaves weirdly.


heh, looks like abs(-0.0) is slightly less than abs(+0.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




Join us for AI Startup School this June 16-17 in San Francisco!

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: