Code coverage report for nodash/lib/realfrac.js

Statements: 100% (19 / 19)      Branches: 100% (11 / 11)      Functions: 100% (5 / 5)      Lines: 100% (19 / 19)      Ignored: none     

All files » nodash/lib/ » realfrac.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51      1     5 20   9   9   2     1 10 10     5     2 2           10 10 10 10   2   6   2                    
/* vim: set et sw=2 ts=2: */
'use strict';
 
module.exports = [ 'Math', 'signum', 'tuple',
  function (Math, signum, tuple) {
 
  var truncate = Math.trunc || function (x) {
    switch (signum(x)) {
    case -1:
      return Math.ceil(x);
    case 1:
      return Math.floor(x);
    }
    return 0;
  };
 
  function properFraction(x) {
    var num = truncate(x);
    return [ num, -(num - x) ];
  }
 
  return {
 
    properFraction: function (x) {
      var num = truncate(x);
      return tuple(num, -(num - x));
    },
 
    truncate: truncate,
 
    round: function (x) {
      var fraction = properFraction(x);
      var n = fraction[0];
      var m = fraction[1] < 0 ? n - 1 : n + 1;
      switch (signum(Math.abs(fraction[1]) - 0.5)) {
        case -1:
          return n;
        case 0:
          return n % 2 === 0 ? n : m;
        case 1:
          return m;
      }
    },
 
    ceiling: Math.ceil,
 
    floor: Math.floor
 
  };
}];