JavaScript Number.EPSILON 属性表示 1 与大于 1 的最小浮点数之间的差值。
浮点数是带有小数点的正整数或负整数。例如,1.2、2.3、4.5、...等。
语法
以下是 JavaScript Number 的语法。EPSILON() 属性 −
Number.EPSILON
参数
它不接受任何参数
返回值
此属性没有 return 返回值。
示例 1
在下面的示例中,我们使用 JavaScript Number.EPSILON 属性表示 1 与大于 1 的最小浮点数之间的差。
<html>
<head>
<title>JavaScript Number.EPSILON Property</title>
</head>
<body>
<script>
var result = Number.EPSILON;
document.write("Value of the epsilon: ", result);
</script>
</body>
</html>
输出
上述程序将 epsilon 值生成为 −
Value of the 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);
//callig the function
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
The result of '(x-y) < Number.EPSILON': true
The result of '(x = x+y, y) < Number.EPSILON': false