JavaScript Number.EPSILON 属性方法
JavaScript Number.EPSILON 属性表示 1 与大于 1 的最小浮点数之间的差值。
浮点数 是带有小数点的正整数或负整数。例如,1.2、2.3、4.5 等。
语法
以下是 JavaScript Number.EPSILON() 属性的语法 -
Number.EPSILON
参数
- 不接受任何参数
返回值
此属性没有返回值。
示例 1
在下面的示例中,我们使用 JavaScript Number.EPSILON 属性来表示 1 与 大于 1 的 最小浮点数之间的差值。
<html>
<head>
<title>JavaScript Number.EPSILON Property</title>
</head>
<body>
<script>
var result = Number.EPSILON;
document.write("epsilon 的值: ", result);
</script>
</body>
</html>
输出
上述程序生成的 epsilon 值为 −
epsilon 的值:2.220446049250313e-16
示例 2
以下是 JavaScript Number.EPSILON 属性的另一个示例。我们定义一个名为 equal(x, y) 的函数,该函数将 value 与 Number.EPSILON 进行比较,并根据比较结果返回 true 或 false。
<html>
<head>
<title>JavaScript Number.EPSILON Property</title>
</head>
<body>
<script>
function equal(x, y) {
return (x-y) < Number.EPSILON;
}
let x = 10.1;
let y = 10.2;
document.write("x = ", x, ", y = ", y);
//调用函数
document.write("<br>The result of '(x-y) < Number.EPSILON': ", equal(x, y));
document.write("<br>The result of '(x = x+y, y) < Number.EPSILON': ", equal(x+y, y));
</script>
</body>
</html>
输出
执行上述程序后,将返回以下输出 -
x = 10.1, y = 10.2 The result of '(x-y) < Number.EPSILON': true The result of '(x = x+y, y) < Number.EPSILON': false

