gpt4 book ai didi

javascript - isFinite(Number.MAX_VALUE + 1) 返回 true?

转载 作者:行者123 更新时间:2023-11-28 01:01:53 32 4
gpt4 key购买 nike

我正在学习JavaScript,书上有一个例子:

<!DOCTYPE html>
<html>
<head>
<title>Number Example 2</title>
<script type="text/javascript">

var result = Number.MAX_VALUE + 1;
alert(isFinite(result)); // the book says it should be false, but I got "true"

</script>

</head>
<body>

</body>
</html>

运行此代码时我收到警报消息“true”,有什么问题吗?

最佳答案

Number.MAX_VALUE 加上任何其他值都会返回 Number.MAX_VALUE

因此,例如,以下语句是true:

 Number.MAX_VALUE + 1 ===  Number.MAX_VALUE + 2

既然我们已经做到了,下面的陈述也是true:

Number.MAX_VALUE + 1 ===  Number.MAX_VALUE + 999999

在某些时候,该语句会变为false:

Number.MAX_VALUE + 1 ===  Number.MAX_VALUE + 999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999 // true

但是再添加一个 9 会返回 false:

Number.MAX_VALUE + 1 ===  Number.MAX_VALUE + 9999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999 // false

但是,如果我们将 Number.MAX_VALUE 添加到自身,它会返回以下内容:

Number.MAX_VALUE + (Number.MAX_VALUE) === Infinity

乘法有点棘手。当我们将 Number.MAX_VALUE 与任何大于 1 的值相乘时,它会返回 Infinity

Number.MAX_VALUE * 1.00000001 === Infinity

关于javascript - isFinite(Number.MAX_VALUE + 1) 返回 true?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25532240/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com