Re: Math.sign vs ±0

Claude Pache claude.pache at gmail.com
Wed Oct 30 05:08:01 PDT 2013


Le 30 oct. 2013 à 11:44, K. Gadd <kg at luminance.org> a écrit :

> Unfortunately Claude, ES Math.sign is not Signum; it has five outputs, not three, like Oliver was asking about. Observe:
> 
> > Math.sign(1 / 0)
> 1
> > Math.sign(-1 / 0)
> -1
> > Math.sign(-1 / 0 * 0)
> NaN
> > Math.sign(0 * -1)
> -0
> > Math.sign(0 * 1)
> 0
> 
> Signum as specified in your link produces three outputs: 0, -1, and 1.

Sure, ES `Math.sign` *cannot* be Signum, because real numbers *cannot* be represented in ES: Numbers in ES are just an *approximation* of a mathematical concept.

From a mathematical point of view, `+0` and `-0` is the same thing and `NaN` does not exist; so that `Math.sign` has really three meaningful outputs when interpreted mathematically: -1, 0, and 1. 

In general, I expect that the `Math` namespace to hold functions that correspond to definite mathematical operations, and to provide an approximation of those operations, as close as it is reasonable when taking in account the similarities and the differences between numbers in ES and numbers in maths. (From that point of view, I think that `Math.imul` and `Math.fround` should probably not belong to the `Math` namespace... but I digress.)

—Claude



More information about the es-discuss mailing list