es6.math.fround.js 745 B

1234567891011121314151617181920212223242526
  1. // 20.2.2.16 Math.fround(x)
  2. var $export = require('./$.export')
  3. , sign = require('./$.math-sign')
  4. , pow = Math.pow
  5. , EPSILON = pow(2, -52)
  6. , EPSILON32 = pow(2, -23)
  7. , MAX32 = pow(2, 127) * (2 - EPSILON32)
  8. , MIN32 = pow(2, -126);
  9. var roundTiesToEven = function(n){
  10. return n + 1 / EPSILON - 1 / EPSILON;
  11. };
  12. $export($export.S, 'Math', {
  13. fround: function fround(x){
  14. var $abs = Math.abs(x)
  15. , $sign = sign(x)
  16. , a, result;
  17. if($abs < MIN32)return $sign * roundTiesToEven($abs / MIN32 / EPSILON32) * MIN32 * EPSILON32;
  18. a = (1 + EPSILON32 / EPSILON) * $abs;
  19. result = a - (a - $abs);
  20. if(result > MAX32 || result != result)return $sign * Infinity;
  21. return $sign * result;
  22. }
  23. });