Skip to content

Commit

Permalink
added binomial_coefficient.js, count_decimal_digits.js, factorial.js,…
Browse files Browse the repository at this point in the history
… multiply_range.js, round.js, truncate_decimal.js
  • Loading branch information
DanielJDufour committed Aug 27, 2022
1 parent 200fc34 commit 5d9aa07
Show file tree
Hide file tree
Showing 47 changed files with 445 additions and 41 deletions.
41 changes: 41 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,19 @@
The following functions are supported:
- [absolute](#absolute)
- [add](#add)
- [binomial_coefficient](#binomial_coefficient)
- [ceil](#ceil)
- [compare](#compare)
- [divide](#divide)
- [factorial](#factorial)
- [floor](#floor)
- [max](#max)
- [mean](#mean)
- [min](#min)
- [multiply](#multiply)
- [pow](#pow)
- [remainder](#remainder)
- [round](#round)
- [sign](#sign)
- [sort](#sort)
- [subtract](#subtract)
Expand Down Expand Up @@ -67,6 +70,15 @@ add("0.1", "0.2"); // 0.1 + 0.2
"0.3"
```

### binomial_coefficient
```js
import binomial_coefficient from "preciso/binomial_coefficient.js";

// 7 choose 3
binomial_coefficient("7", "3")
"35"
```

### compare
```js
import compare from "preciso/compare.js";
Expand Down Expand Up @@ -102,6 +114,20 @@ divide("-714.7008086132632", "8135.725531"); // -714.7008086132632 / 8135.725531
'-0.0878472123832102762218908980055167989417759034280282678823325216230183564682007707223868489179001533'
```

### factorial
```js
import factorial from "preciso/factorial.js";

factorial("0");
"1"

factorial("3");
"6"

factorial("10");
"3628800"
```

### floor
```js
import floor from "preciso/floor.js";
Expand Down Expand Up @@ -177,6 +203,21 @@ remainder("-0.5", "2"); // -0.5 % 2
"-0.5"
```

### round
```js
import round from "preciso/round.js";

round("0.99")
"1"

round("0.12345");
"0"

// round to the specified number of digits
round("0.12345", { digits: 2 })
"0.12"
```

### sign
```js
import sign from "preciso/subtract.js";
Expand Down
2 changes: 2 additions & 0 deletions absolute.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
"use strict";

const clean = require("./clean.js");

function absolute(n) {
Expand Down
2 changes: 2 additions & 0 deletions add.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
"use strict";

const compare_positive = require("./compare_positive.js");
const clean = require("./clean.js");
const long_addition = require("./long_addition.js");
Expand Down
31 changes: 31 additions & 0 deletions binomial_coefficient.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
"use strict";

const clean = require("./clean.js");
const compare_positive = require("./compare_positive.js");
const factorial = require("./factorial.js");
const long_addition = require("./long_addition.js");
const long_subtraction = require("./long_subtraction.js");
const long_division = require("./long_division.js");
const multiply_range = require("./multiply_range.js");

function binomial_coefficient(n, k) {
n = clean(n);
k = clean(k);

switch (compare_positive(n, k)) {
case "=":
return "1";
case ">": {
const diff = long_subtraction(n, k);
const numerator = multiply_range(long_addition(k, "1"), n);
const denominator = factorial(diff);
return long_division(numerator, denominator);
}
case "<": {
throw new Error("[binominal_coefficient] unsupported");
}
}
}

module.exports = binomial_coefficient;
module.exports.default = binomial_coefficient;
2 changes: 2 additions & 0 deletions ceil.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
"use strict";

const absolute = require("./absolute.js");
const clean = require("./clean.js");
const long_addition = require("./long_addition.js");
Expand Down
4 changes: 4 additions & 0 deletions clean.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
"use strict";

const expand = require("./expand.js");

module.exports = function clean(n) {
Expand All @@ -16,5 +18,7 @@ module.exports = function clean(n) {
// should improve this, so it identifies zero earlier
if (n === "") n = "0";

if (n === "-0") n = "0";

return n;
};
2 changes: 2 additions & 0 deletions compare.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
"use strict";

const clean = require("./clean.js");
const compare_positive = require("./compare_positive.js");

Expand Down
2 changes: 1 addition & 1 deletion compare_positive.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const clean = require("./clean.js");
"use strict";

// given:
// - a and b are positive numbers
Expand Down
2 changes: 2 additions & 0 deletions constants.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
"use strict";

// Internet Explorer doesn't support Number.MAX_SAFE_INTEGER
// so we just define the constant ourselves
const MAX_SAFE_INTEGER = 9007199254740991;
Expand Down
17 changes: 17 additions & 0 deletions count_decimal_digits.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
"use strict";

const clean = require("./clean.js");

function count_decimal_digits(n) {
n = clean(n);

const i = n.indexOf(".");

// n is an integer
if (i === -1) return "0";

return (n.length - i - 1).toString();
}

module.exports = count_decimal_digits;
module.exports.default = count_decimal_digits;
2 changes: 2 additions & 0 deletions divide.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
"use strict";

const absolute = require("./absolute.js");
const clean = require("./clean.js");
const long_division = require("./long_division.js");
Expand Down
4 changes: 3 additions & 1 deletion expand.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
"use strict";

// convert exponential notation to normal string
// not optimized yet and no support for big numbers
function expand(n) {
Expand Down Expand Up @@ -36,7 +38,7 @@ function expand(n) {
return sign + result;
} else if (normshift < 0) {
// need to add zeros in decimal places
result = "0.";
let result = "0.";
for (let i = 0; i > normshift; i--) result += "0";
result += base;
return sign + result;
Expand Down
11 changes: 11 additions & 0 deletions factorial.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
"use strict";

const multiply_range = require("./multiply_range.js");

function factorial(n) {
if (n === "0") return "1";
return multiply_range("1", n);
}

module.exports = factorial;
module.exports.default = factorial;
2 changes: 2 additions & 0 deletions floor.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
"use strict";

const absolute = require("./absolute.js");
const clean = require("./clean.js");
const long_addition = require("./long_addition.js");
Expand Down
8 changes: 8 additions & 0 deletions is_factorial.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
"use strict";

function is_factorial(n) {
return !!n.match(/^\d+!$/i);
}

module.exports = is_factorial;
module.exports.default = is_factorial;
9 changes: 7 additions & 2 deletions is_infinity.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
module.exports = function is_infinity(n) {
"use strict";

function is_infinity(n) {
return !!n.match(/^(|-|\+)inf(inity)?$/i);
};
}

module.exports = is_infinity;
module.exports.default = is_infinity;
9 changes: 7 additions & 2 deletions is_integer.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
"use strict";

const clean = require("./clean.js");
const is_infinity = require("./is_infinity.js");

module.exports = function is_integer(n) {
function is_integer(n) {
if (is_infinity(n)) return false;
n = clean(n);
return !n.includes(".");
};
}

module.exports = is_integer;
module.exports.default = is_integer;
11 changes: 11 additions & 0 deletions is_negative.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
"use strict";

const clean = require("./clean");

function is_negative(n) {
n = clean(n);
return n[0] === "-";
}

module.exports = is_negative;
module.exports.default = is_negative;
9 changes: 7 additions & 2 deletions is_negative_infinity.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
module.exports = function is_negative_infinity(n) {
"use strict";

function is_negative_infinity(n) {
return !!n.match(/^-inf(inity)?$/i);
};
}

module.exports = is_negative_infinity;
module.exports.default = is_negative_infinity;
9 changes: 7 additions & 2 deletions is_positive_infinity.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
module.exports = function is_positive_infinity(n) {
"use strict";

function is_positive_infinity(n) {
return !!n.match(/^\+?inf(inity)?$/i);
};
}

module.exports = is_positive_infinity;
module.exports.default = is_positive_infinity;
9 changes: 7 additions & 2 deletions is_zero.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
module.exports = function is_zero(n) {
"use strict";

function is_zero(n) {
return /^[-+]?0(\.0+)?(e[\.\d]+)?$/.test(n);
};
}

module.exports = is_zero;
module.exports.default = is_zero;
9 changes: 7 additions & 2 deletions long_addition.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
"use strict";

const { MAX_SAFE_INTEGER_LENGTH } = require("./constants.js");

// assumes both numbers are positive numbers
module.exports = function long_addition(a, b) {
function long_addition(a, b) {
const alen = a.length;
const blen = b.length;

Expand Down Expand Up @@ -86,4 +88,7 @@ module.exports = function long_addition(a, b) {
if (result[0] === ".") result = "0" + result;

return result;
};
}

module.exports = long_addition;
module.exports.default = long_addition;
10 changes: 7 additions & 3 deletions long_division.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
"use strict";

const compare_positive = require("./compare_positive.js");
const add = require("./add.js");
const multiply = require("./multiply.js");
const subtract = require("./subtract.js");
const round_last_decimal = require("./round_last_decimal.js");

// given dividend and divisor are positive numberical strings
module.exports = function long_division(dividend, divisor, { max_decimal_digits = 100, ellipsis = false } = {}) {
function long_division(dividend, divisor, { max_decimal_digits = 100, ellipsis = false } = {}) {
// remove unnecessary starting zeros
// ex: 0.5 => .5
if (dividend[0] === "0") dividend = dividend.substring(1);
Expand Down Expand Up @@ -195,4 +196,7 @@ module.exports = function long_division(dividend, divisor, { max_decimal_digits
if (quotient[0] === ".") quotient = "0" + quotient;

return quotient;
};
}

module.exports = long_division;
module.exports.default = long_division;
9 changes: 7 additions & 2 deletions long_multiplication.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
"use strict";

const { MAX_SAFE_INTEGER_LENGTH } = require("./constants.js");

const CHUNK_SIZE = 15;
Expand All @@ -9,7 +11,7 @@ const CHUNK_SIZE = 15;
* @returns {String} product - result of multiplying a with b
*/

module.exports = function long_multiplication(a, b) {
function long_multiplication(a, b) {
if (a === "0" || b === "0") return "0";

const top_index_of_dot = a.indexOf(".");
Expand Down Expand Up @@ -132,4 +134,7 @@ module.exports = function long_multiplication(a, b) {
}

return result;
};
}

module.exports = long_multiplication;
module.exports.default = long_multiplication;
9 changes: 7 additions & 2 deletions long_subtraction.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
"use strict";

// const lookup = {};
// const vals = [undefined, 0, 1, 2, 3, 4, 5, 6, 8, 9];
// vals.forEach(top => {
Expand All @@ -11,7 +13,7 @@ const { MAX_SAFE_INTEGER_LENGTH } = require("./constants.js");

// assumes (1) both a and b are positive numbers
// and (2) a is larger than b
module.exports = function long_subtraction(a, b) {
function long_subtraction(a, b) {
const alen = a.length;
const blen = b.length;

Expand Down Expand Up @@ -141,4 +143,7 @@ module.exports = function long_subtraction(a, b) {
if (result[0] === ".") result = "0" + result;

return result;
};
}

module.exports = long_subtraction;
module.exports.default = long_subtraction;
2 changes: 2 additions & 0 deletions max.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
"use strict";

const clean = require("./clean.js");
const compare = require("./compare.js");

Expand Down

0 comments on commit 5d9aa07

Please sign in to comment.