Skip to content

Commit

Permalink
added reciprocal to docs and max_decimal_digits to multiply
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielJDufour committed Nov 27, 2022
1 parent b4bdcdb commit f2e8268
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 6 deletions.
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,19 @@ pow("0", "0", { zero_to_the_power_of_zero: undefined })
undefined
```

### reciprocal
Calculate [reciprocal](https://en.wikipedia.org/wiki/Multiplicative_inverse) or [multiplicative inverse](https://en.wikipedia.org/wiki/Multiplicative_inverse)
```js
import reciprocal from "preciso/reciprocal.js"

reciprocal("0.1");
"10"

// return fraction if applicable
reciprocal("3/4", { fraction: true })
"4/3"
```

### remainder
```js
import remainder from "preciso/truncate.js";
Expand Down
6 changes: 4 additions & 2 deletions multiply.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
const multiply_array = require("./multiply_array.js");

function multiply() {
const nums = Array.isArray(arguments[0]) ? arguments[0] : Array.from(arguments);
return multiply_array(nums);
const args = Array.from(arguments);
const options = typeof args[args.length - 1] === "object" ? args[args.length - 1] : undefined;
const nums = Array.isArray(args[0]) ? args[0] : options ? args.slice(0, args.length - 1) : args;
return multiply_array(nums, options);
}

module.exports = multiply;
Expand Down
7 changes: 5 additions & 2 deletions multiply_array.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,12 @@ const multiply_rational = require("./multiply_rational.js");
* @param {Array.<String>} nums - array of numerical strings
* @returns {String} product as a numerical string
*/
function multiply_array(nums) {
function multiply_array(nums, { max_decimal_digits } = {}) {
const imaginary = is_odd(nums.filter(n => is_imaginary(n)).length.toString());
let product = multiply_rational(nums.map(n => n.replace(/i$/, "")));
let product = multiply_rational(
nums.map(n => n.replace(/i$/, "")),
{ max_decimal_digits }
);
if (imaginary) product += "i";
return product;
}
Expand Down
4 changes: 3 additions & 1 deletion multiply_rational.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@ const absolute = require("./absolute.js");
const clean = require("./clean.js");
const compare_positive = require("./compare_positive.js");
const long_multiplication = require("./long_multiplication.js");
const round = require("./round.js");

/**
* @name multiply
* @returns {String} product
*/
function multiply_rational(nums, options = {}) {
function multiply_rational(nums, { max_decimal_digits } = {}) {
let product = clean(nums[0]);
let product_absolute = absolute(product);
let product_sign = product[0] === "-" ? "-" : "";
Expand All @@ -27,6 +28,7 @@ function multiply_rational(nums, options = {}) {

product = product_sign + product_absolute;
}
if (typeof max_decimal_digits === "number") product = round(product, { digits: max_decimal_digits });
return product;
}

Expand Down
2 changes: 1 addition & 1 deletion preciso.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export function max(nums: string[]): string;
// you can also pass in min(a, b, c, d) but I'm not sure how to type that
export function min(nums: string[]): string;

export function multiply(a: string, b: string): string;
export function multiply(a: string, b: string, options?: { max_decimal_digits?: number }): string;
export function pow(base: string, exponent: string, options: { ellipsis?: boolean; max_decimal_digits?: number; zero_to_the_power_of_zero?: string }): string;
export function remainder(dividend: string, divisor: string): string;
export function root(radicand: string, index: string, options?: { imaginary?: boolean; max_decimal_digits?: number }): string;
Expand Down
2 changes: 2 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,8 @@ test("multiply", ({ eq }) => {
// 3573540430663160
eq(multiply("7147008086132632", "5"), "35735040430663160");
eq(multiply("-714.7008086132632", "8135.725531"), "-5814609.6156612701214627592");
eq(multiply("-714.7008086132632", "8135.725531", { max_decimal_digits: 0 }), "-5814610");
eq(multiply("-714.7008086132632", "8135.725531", { max_decimal_digits: 7 }), "-5814609.6156613");
// 46456025598621080
eq(multiply("7147008086132632", "65"), "464555525598621080");

Expand Down

0 comments on commit f2e8268

Please sign in to comment.