Pure function given strictly equal arguments yielding non-strictly equal results

后端 未结 6 659
执念已碎
执念已碎 2021-01-04 12:59

Below is a pure function f for which f(a) !== f(b) despite a === b (notice the strict equalities) for some values of a<

6条回答
  •  死守一世寂寞
    2021-01-04 13:43

    I'm not so sure this is so scary ;-) Javascript is not a pure language and the presence of +/-0 and the equality of -0 and +0 are specific to IEEE-754 and are "well defined", even if perhaps sometimes surprising. (Even NaN != NaN always being true is well defined, for instance.)

    From signed zero:

    According to the IEEE 754 standard, negative zero and positive zero should compare as equal with the usual (numerical) comparison operators...

    Technically, because the two inputs to f are different, then the result can also be different. For what it's worth, Haskell will treat 0 == -0 as true but will treat (1 / 0) == (1 / (-0)) as false.

    However, I do find this an interesting question.

    Happy coding.

提交回复
热议问题