纯函数给出严格相等的参数,产生非严格相等的结果 [英] Pure function given strictly equal arguments yielding non-strictly equal results

查看:78
本文介绍了纯函数给出严格相等的参数,产生非严格相等的结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是纯函数 f 为此 f(a)!== f(b)尽管 a === b (注意<对于 a b 的某些值,em>严格等于):

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

var f = function (x) {
   return 1 / x;
}

+0 === -0 // true
f(+0) === f(-0) // false

此类函数的存在可能导致难以发现的错误。还有其他我应该厌倦的例子吗?

The existence of such functions can lead to difficult-to-find bugs. Are there other examples I should be weary of?

推荐答案

是的,因为 NaN!== NaN

var f = function (x) { return Infinity - x; }

Infinity === Infinity // true
f(Infinity) === f(Infinity) // false

f(Infinity) // NaN

其他一些产生 NaN 的例子,其参数可以是严格相等:

Some other examples that yield NaN whose arguments can be strictly equal:

0/0
Infinity/Infinity
Infinity*0
Math.sqrt(-1)
Math.log(-1)
Math.asin(-2)

这篇关于纯函数给出严格相等的参数,产生非严格相等的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆