當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


JavaScript Math fround()用法及代碼示例

JavaScript Math.fround() 函數返回數字的最接近的 32 位單精度浮點表示。

用法:

Math.fround(doubleFloat)

fround() 是一個靜態方法,使用 Math 類名調用。

Math.fround() 參數

Math.fround() 函數接受:

  • doubleFloat - 一個 Number

從數學返回值。fround()

  • 返回給定數字的最接近的 32 位單精度浮點表示。
  • 如果是非數字參數,則返回 NaN

示例:使用數學。fround()

var num = Math.fround(1.5);
console.log(num); // 1.5

var num = Math.fround(5.05);
console.log(num); // 5.050000190734863

console.log(2 ** 130); // 1.361129467683754e+39
var num = Math.fround(2 ** 130);
console.log(num); // Infinity

var num = Math.fround(5);
console.log(num); // 5

var num = Math.fround(1.337);
console.log(num); // 1.3370000123977661

輸出

1.5
5.050000190734863
1.361129467683754e+39
Infinity
5
1.3370000123977661

JavaScript 在內部使用 64 位雙精度浮點數。

在這裏,我們可以看到可以在二進製數字係統中完美表示的數字(如 1.5)具有相同的 32 位單精度浮點表示。

但是,一些不能完美表示的(如 1.337 或 5.05)在 32 位和 64 位中有所不同。

自從2**130對於 32 位浮點數來說太大了,fround()返回Infinity對於這樣的數字。

相關用法


注:本文由純淨天空篩選整理自 JavaScript Math fround()。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。