javascript - Math.min() > Math.max() ?
巴扎黑
巴扎黑 2017-04-10 12:42:39
0
2
633

不明白为什么这样设计:

console.log(Math.min());//Infinity
console.log(Math.max());//-Infinity
console.log(Math.min()>Math.max());//true
巴扎黑
巴扎黑

membalas semua(2)
洪涛

http://ecma-international.org/ecma-26...

就是这么规定的,无参时
Math.max()返回负无穷
Math.min()返回正无穷

所以Math.min()>Math.max()

为什么这么设计?这里给出了解释,
http://stackoverflow.com/questions/88...

比如Math.min(5)==Math.min(5,Infinity)==5
如果Math.min()等于负无穷,Math.min()就恒等于负无穷了,因为没有数比负无穷小。

左手右手慢动作

看来你不明白的是为什么

Math.min()

返回的是正无穷大吧。我估计设计者的思路是:任何比正无穷大Infinity小的数都有可能是一个list中的最小值。因此返回Infinity

Math.min()

返回的是负无穷大。同理,任何比负无穷大的大的数都有可能是一个list中的最大值。因此返回-Infinity
那么当然有:

console.log(Math.min()>Math.max());//true
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan