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("Value of the epsilon: ", result);
</script>
</body>
</html>

輸出

上述程式輸出的 epsilon 值為:

Value of the epsilon: 2.220446049250313e-16

示例 2

以下是 JavaScript Number.EPSILON 屬性的另一個示例。我們定義一個名為equal(x, y)的函式,該函式比較值與 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
廣告