首页 > Math.min() > Math.max() ?

Math.min() > Math.max() ?

不明白为什么这样设计:

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

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
【热门文章】
【热门文章】