javascript - Explain: Math.fround() function - Stack Overflow

How do works Math.fround() function.The Math.fround() function returns the nearest float representation

How do works Math.fround() function.

The Math.fround() function returns the nearest float representation of a number.

But when it is passed the float number Math.fround(1.5) it returns the same(1.5) value. But when it is passed the float number Math.fround(1.55) it returns a different value 1.5499999523162841. why and How?

  • I am confused about how Math.fround() works.
  • How is it different from the Math.round() function?
  • Also let me know why it's not supported in Internet Explorer.

How do works Math.fround() function.

The Math.fround() function returns the nearest float representation of a number.

But when it is passed the float number Math.fround(1.5) it returns the same(1.5) value. But when it is passed the float number Math.fround(1.55) it returns a different value 1.5499999523162841. why and How?

  • I am confused about how Math.fround() works.
  • How is it different from the Math.round() function?
  • Also let me know why it's not supported in Internet Explorer.
Share Improve this question edited Aug 24, 2017 at 17:53 JakeBoggs 2744 silver badges17 bronze badges asked Jun 29, 2017 at 6:34 Ramesh RajendranRamesh Rajendran 38.7k49 gold badges159 silver badges240 bronze badges 7
  • you can start your understanding with JavaScript’s Number type in details – Max Koretskyi Commented Jun 29, 2017 at 6:36
  • 6 Did you read about single-precision? en.m.wikipedia/wiki/Single-precision_floating-point_format – Gerardo Furtado Commented Jun 29, 2017 at 6:37
  • I couldn't see that page :( – Ramesh Rajendran Commented Jun 29, 2017 at 6:37
  • 1 write the number in binary and you will understand. 1.5 is 2^0 + 2^-1 but 1.55 can not be represented as sum of powers of 2 so it is like iracional number for binary representation ... that is why you got so many diigtis ... – Spektre Commented Jun 29, 2017 at 6:38
  • 1 @RobG it's funny, but their polyfill doesn't work on pliant browsers since they don't construct Float32Array with new.. – Patrick Roberts Commented Jun 29, 2017 at 6:47
 |  Show 2 more ments

2 Answers 2

Reset to default 9

To understand how this function works you actually have to know the following topics:

  • JavaScript’s Number type in details
  • The simple math behind decimal-binary conversion algorithms
  • How to round binary numbers
  • The mechanics behind exponent bias in floating point

The ECMA script specifies the following algorithm for the conversion:

When Math.fround is called with argument x, the following steps are taken:

  • If x is NaN, return NaN.
  • If x is one of +0, -0, +∞, -∞, return x.
  • Let x32 be the result of converting x to a value in IEEE 754-2008 binary32 format using roundTiesToEven.
  • Let x64 be the result of converting x32 to a value in IEEE 754-2008 binary64 format.
  • Return the ECMAScript Number value corresponding to x64.

So, let's do that for 1.5 and 1.55.

Math.fround(1.5)

1) Represent in 64bit float

0 01111111111 1000000000000000000000000000000000000000000000000000

2) Represent in the scientific notation

1.1000000000000000000000000000000000000000000000000000 x 2^0

3) Round to 23 bit mantissa

1.10000000000000000000000

4) Convert to decimal:

1.5

Math.fround(1.55)

1) Represent in 64bit float

0 01111111111 1000110011001100110011001100110011001100110011001101

2) Represent in the scientific notation

1.1000110011001100110011001100110011001100110011001101 x 2^0

3) Round to 23 bit mantissa

1.10001100110011001100110

4) Convert to decimal:

1.5499999523162841

I got these below things which is I got from some of the documents. Kindly share your answers if you have more details.


I have found something about Math.fround ( x ) from ECMAScript® 2015 Language Specification

When Math.fround() is called with argument x the following steps are taken:

  • If x is NaN, return NaN.
  • If x is one of +0, −0, +∞, −∞, return x.
  • Let x32 be the result of converting x to a value in IEEE 754-2008 binary32 format using roundTiesToEven.
  • Let x64 be the result of converting x32 to a value in IEEE 754-2008 binary64 format.
  • Return the ECMAScript Number value corresponding to x64.

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];
  };
})(Float32Array(1));

** Kindly share more answers for learning more details

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744906731a4600304.html

相关推荐

  • javascript - Explain: Math.fround() function - Stack Overflow

    How do works Math.fround() function.The Math.fround() function returns the nearest float representation

    1天前
    70

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信