Explain: Math.fround () function

How the function works Math.fround().

The function Math.fround()returns the closest floating representation of the number.

But when it is passed a floating point number Math.fround(1.5), it returns the same value (1.5). But when it is transmitted with a float number Math.fround(1.55), it returns a different value 1.5499999523162841. why and how?

  • I am confused by how it works Math.fround().
  • How is it different from a function Math.round()?
  • Also let me know why it is not supported in Internet Explorer.
+4
source share
2 answers

, , :

ECMA script :

Math.fround x, : :

  • x NaN, NaN.
  • x +0, -0, + ∞, -∞, x.
  • x32 x IEEE 754-2008 binary32 roundTiesToEven.
  • x64 - x32 IEEE 754-2008.
  • ECMAScript , x64.

, 1.5 1.55.

Math.fround(1.5)

1) 64-

0 01111111111 1000000000000000000000000000000000000000000000000000

2)

1.1000000000000000000000000000000000000000000000000000 x 2^0

3) 23

1.10000000000000000000000

4) :

1.5

Math.fround(1.55)

1) 64-

0 01111111111 1000110011001100110011001100110011001100110011001101

2)

1.1000110011001100110011001100110011001100110011001101 x 2^0

3) 23

1.10001100110011001100110

4) :

1.5499999523162841
+6

, . , , .


- Math.fround ( x ) ECMAScript® 2015

Math.fround() x :

  • x NaN, NaN.
  • x +0, -0, + ∞, -∞, x.
  • x32 x binary32 IEEE 754-2008 roundTiesToEven.
  • x64 x32 binary64 IEEE 754-2008.
  • ECMAScript Number, x64.

, Float32Array :

Math.fround = Math.fround || (function (array) {
  return function(x) {
    return array[0] = x, array[0];
  };
})(Float32Array(1));

**

0

Source: https://habr.com/ru/post/1680386/


All Articles