purescript / purescript-prelude

The PureScript Prelude
BSD 3-Clause "New" or "Revised" License
161 stars 88 forks source link

`signum` should return `zero` when given `zero` (currently returns `one`) #263

Closed triallax closed 2 years ago

triallax commented 3 years ago

So I asked about this on the Slack channel, and @hdgarrood said that this was probably an oversight and that he thinks this should be fixed, based on the assumption that the function is not widely used, as this question was not asked before as far as he is aware.

hdgarrood commented 3 years ago

See e.g. https://en.wikipedia.org/wiki/Sign_function. I think PureScript is pretty much unique in believing that signum 0 = 1.

JamieBallingall commented 2 years ago

How would you feel about changing the implementation to:

signum x =
  if x < zero then negate one
  else if x > zero then one
  else x

I.e., returning x if x equals zero rather than returning zero. In any sane instance of Ring this would be the same but Number instantiates Ring and has two zero-like values: +0.0 and -0.0. The Javascript implementation of Math.sign makes this distinction and I think it would be nice to have signum and sign work the same way on Number.

In fact, perhaps we should implement this new function as sign and depreciate signum.

JordanMartinez commented 2 years ago

@JamieBallingall That implementation is what I have in #280.