Skip to content

Commit

Permalink
feat(math): decimal round, floor and ceil
Browse files Browse the repository at this point in the history
  • Loading branch information
medikoo committed Mar 9, 2018
1 parent a5f7d04 commit 39290c6
Show file tree
Hide file tree
Showing 9 changed files with 77 additions and 0 deletions.
29 changes: 29 additions & 0 deletions math/_decimal-adjust.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Credit:
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/round
// #Decimal_rounding

"use strict";

var isValue = require("../object/is-value")
, ensureInteger = require("../object/ensure-integer");

var split = String.prototype.split;

module.exports = function (type) {
return function (value/*, exp*/) {
value = Number(value);
var exp = arguments[1];
if (isValue(exp)) exp = ensureInteger(exp);
if (!value) return value;
if (!exp) return Math[type](value);
if (!isFinite(value)) return value;

// Shift
var tokens = split.call(value, "e");
value = Math[type](tokens[0] + "e" + ((tokens[1] || 0) - exp));

// Shift back
tokens = value.toString().split("e");
return Number(tokens[0] + "e" + (Number(tokens[1] || 0) + exp));
};
};
3 changes: 3 additions & 0 deletions math/ceil-10.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
"use strict";

module.exports = require("./_decimal-adjust")("ceil");
3 changes: 3 additions & 0 deletions math/floor-10.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
"use strict";

module.exports = require("./_decimal-adjust")("floor");
3 changes: 3 additions & 0 deletions math/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,18 @@ module.exports = {
asinh: require("./asinh"),
atanh: require("./atanh"),
cbrt: require("./cbrt"),
ceil10: require("./ceil-10"),
clz32: require("./clz32"),
cosh: require("./cosh"),
expm1: require("./expm1"),
floor10: require("./floor-10"),
fround: require("./fround"),
hypot: require("./hypot"),
imul: require("./imul"),
log10: require("./log10"),
log2: require("./log2"),
log1p: require("./log1p"),
round10: require("./round-10"),
sign: require("./sign"),
sinh: require("./sinh"),
tanh: require("./tanh"),
Expand Down
3 changes: 3 additions & 0 deletions math/round-10.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
"use strict";

module.exports = require("./_decimal-adjust")("round");
6 changes: 6 additions & 0 deletions test/math/_decimal-adjust.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
"use strict";

module.exports = function (t, a) {
// Just sanity check, as real tests are in 'round', 'ceil' and 'floor' variants
a(t("round")(55.55, -1), 55.6);
};
8 changes: 8 additions & 0 deletions test/math/ceil-10.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
"use strict";

module.exports = function (t, a) {
a(t(55.51, -1), 55.6);
a(t(51, 1), 60);
a(t(-55.59, -1), -55.5);
a(t(-59, 1), -50);
};
8 changes: 8 additions & 0 deletions test/math/floor-10.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
"use strict";

module.exports = function (t, a) {
a(t(55.59, -1), 55.5);
a(t(59, 1), 50);
a(t(-55.51, -1), -55.6);
a(t(-51, 1), -60);
};
14 changes: 14 additions & 0 deletions test/math/round-10.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
"use strict";

module.exports = function (t, a) {
a(t(55.55, -1), 55.6);
a(t(55.549, -1), 55.5);
a(t(55, 1), 60);
a(t(54.9, 1), 50);
a(t(-55.55, -1), -55.5);
a(t(-55.551, -1), -55.6);
a(t(-55, 1), -50);
a(t(-55.1, 1), -60);
a(t(1.005, -2), 1.01);
a(t(-1.005, -2), -1.0);
};

0 comments on commit 39290c6

Please sign in to comment.