Math.fround()
function returns the nearest
32-bit single precision
float representation of a
Number
.
The source for this interactive example is stored in a GitHub repository. If you'd like to contribute to the interactive examples project, please clone https://github.com/mdn/interactive-examples and send us a pull request.
var singleFloat = Math.fround(doubleFloat);
doubleFloat
Number
. If the parameter is of a different type, it will get converted to a number or to
NaN
if it cannot be converted.
The nearest 32-bit single precision float representation of the given number.
JavaScript uses 64-bit double floating-point numbers internally, which offer a very high precision. However, sometimes you may be working with 32-bit floating-point numbers, for example if you are reading values from a
Float32Array
. This can create confusion: Checking a 64-bit float and a 32-bit float for equality may fail even though the numbers are seemingly identical.
To solve this,
Math.fround()
can be used to cast the 64-bit float to a 32-bit float. Internally, JavaScript continues to treat the number as a 64-bit float, it just performs a "round to even" on the 23rd bit of the mantissa, and sets all following mantissa bits to
0
. If the number is outside the range of a 32-bit float,
or
Infinity
-Infinity
被返回。
因为
fround()
is a static method of
Math
, you always use it as
Math.fround()
, rather than as a method of a
Math
object you created (
Math
is not a constructor).
This can be emulated with the following function, if
Float32Array
are supported:
Math.fround = Math.fround || (function (array) {
return function(x) {
return array[0] = x, array[0];
};
})(new Float32Array(1));
Supporting older browsers is slower, but also possible:
if (!Math.fround) Math.fround = function(arg) {
arg = Number(arg);
// Return early for ±0 and NaN.
if (!arg) return arg;
var sign = arg < 0 ? -1 : 1;
if (sign < 0) arg = -arg;
// Compute the exponent (8 bits, signed).
var exp = Math.floor(Math.log(arg) / Math.LN2);
var powexp = Math.pow(2, Math.max(-126, Math.min(exp, 127)));
// Handle subnormals: leading digit is zero if exponent bits are all zero.
var leading = exp < -127 ? 0 : 1;
// Compute 23 bits of mantissa, inverted to round toward zero.
var mantissa = Math.round((leading - arg / powexp) * 0x800000);
if (mantissa <= -0x800000) return sign * Infinity;
return sign * powexp * (leading - mantissa / 0x800000);
};
Math.fround()
The number 1.5 can be precisely represented in the binary numeral system, and is identical in 32-bit and 64-bit:
Math.fround(1.5); // 1.5 Math.fround(1.5) === 1.5; // true
However, the number 1.337 cannot be precisely represented in the binary numeral system, so it differs in 32-bit and 64-bit:
Math.fround(1.337); // 1.3370000123977661 Math.fround(1.337) === 1.337; // false
is too big for a 32-bit float, so
Infinity
is returned:
2 ** 150; // 1.42724769270596e+45 Math.fround(2 ** 150); // Infinity
If the parameter cannot be converted to a number, or it is
not-a-number
(
NaN
),
Math.fround()
will return
NaN
:
Math.fround('abc'); // NaN
Math.fround(NaN); // NaN
| 规范 |
|---|
|
ECMAScript (ECMA-262)
The definition of 'Math.fround' in that specification. |
The compatibility table in this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request.
更新 GitHub 上的兼容性数据| Desktop | Mobile | Server | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
fround
|
Chrome 38 | Edge 12 | Firefox 26 | IE No | Opera 25 | Safari 8 | WebView Android 38 | Chrome Android 38 | Firefox Android 26 | Opera Android 25 | Safari iOS 8 | Samsung Internet Android 3.0 | nodejs 0.12 |
完整支持
不支持
Math
Math.abs()
Math.acos()
Math.acosh()
Math.asin()
Math.asinh()
Math.atan()
Math.atan2()
Math.atanh()
Math.cbrt()
Math.ceil()
Math.clz32()
Math.cos()
Math.cosh()
Math.exp()
Math.expm1()
Math.floor()
Math.fround()
Math.hypot()
Math.imul()
Math.log()
Math.log10()
Math.log1p()
Math.log2()
Math.max()
Math.min()
Math.pow()
Math.random()
Math.round()
Math.sign()
Math.sin()
Math.sinh()
Math.sqrt()
Math.tan()
Math.tanh()
Math.trunc()
Object
Object.prototype.__defineGetter__()
Object.prototype.__defineSetter__()
Object.prototype.__lookupGetter__()
Object.prototype.__lookupSetter__()
Object.prototype.hasOwnProperty()
Object.prototype.isPrototypeOf()
Object.prototype.propertyIsEnumerable()
Object.prototype.toLocaleString()
Object.prototype.toSource()
Object.prototype.toString()
Object.prototype.valueOf()
Object.setPrototypeOf()