javascript - 解释: Math. fround()函数

标签 javascript angularjs angular typescript math

Math.fround() 函数是如何工作的。

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

但是当它传递 float Math.fround(1.5) 时,它返回相同的 (1.5) 值。 但是当它传递 float Math.fround(1.55) 时,它返回一个不同的值 1.5499999523162841为什么以及如何?

  • 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.

最佳答案

要了解此功能的工作原理,您实际上必须了解以下主题:

ECMA 脚本指定 the following algorithm对于转换:

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.

那么,让我们为 1.51.55 这样做。

数学.fround(1.5)

1)以64位 float 表示

0 01111111111 1000000000000000000000000000000000000000000000000000

2)用科学记数法表示

1.1000000000000000000000000000000000000000000000000000 x 2^0

3) 四舍五入到23位尾数

1.10000000000000000000000

4) 转换为十进制:

1.5

Math.fround(1.55)

1)以64位 float 表示

0 01111111111 1000110011001100110011001100110011001100110011001101

2)用科学记数法表示

1.1000110011001100110011001100110011001100110011001101 x 2^0

3) 四舍五入到23位尾数

1.10001100110011001100110

4) 转换为十进制:

1.5499999523162841

关于javascript - 解释: Math. fround()函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44817932/

相关文章:

Angular 2 验证器未按预期工作

javascript - 结合 Matlab 和 JavaScript : Methods of integration?

javascript - 有没有办法在 Vue.js 中动态插入点击事件?

javascript - 内联样式和 ReactCSSTransitionGroup

angularjs - 停止 Angular-auto-validate 插件的样式验证

javascript - 如何处理来自封装函数的 Promise

javascript - 谷歌地图 : Change infoWindow's Frame

javascript - ng-click 没有触发工厂类

css - angular-animate - 动画在中间是 'stuck'

node.js - 很难启动 Angular CLI 服务器