Skip to content

Commit

Permalink
added root.js
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielJDufour committed Nov 22, 2022
1 parent 4347334 commit 0e30e7a
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 0 deletions.
10 changes: 10 additions & 0 deletions cube_root.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
"use strict";

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

function cube_root(radicand, options) {
return root(radicand, "3", options);
}

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

const absolute = require("./absolute.js");
const clean = require("./clean.js");
const compare_positive = require("./compare_positive.js");
const is_even = require("./is_even.js");
const is_negative = require("./is_negative.js");
const pow = require("./pow.js");
const root_integer_digits = require("./root_integer_digits.js");

function root(radicand, index, { imaginary = true, max_decimal_digits = 100 } = {}) {
radicand = clean(radicand);
index = clean(index);

if (index === "1") return radicand;
if (radicand === "1") return "1";

const rad = absolute(radicand);

const has_imaginary = is_negative(radicand) && is_even(index);
if (has_imaginary && !imaginary) throw new Error("[preciso] root has an imaginary number");

const count_of_integer_places = root_integer_digits(rad, index);

const digits = ["9", "8", "7", "6", "5", "4", "3", "2", "1", "0"];

let left = "";

for (let i = 0; i < count_of_integer_places; i++) {
for (let ii = 0; ii < digits.length; ii++) {
const digit = digits[ii];
const test_start = left + digit;
let test_base = test_start + "0".repeat(count_of_integer_places - i - 1);
const test_res = pow(test_base, index);
const comparison = compare_positive(test_res, rad);
if (comparison === "=") {
if (has_imaginary) test_base += "i";
return test_base;
} else if (comparison === "<") {
left = test_start;
break;
}
}
}

let base = left + ".";

for (let i = 0; i < max_decimal_digits; i++) {
let added = false;
for (let ii = 0; ii < digits.length; ii++) {
const digit = digits[ii];
let test_base = base + digit;
const test_res = pow(test_base, index);
const comparison = compare_positive(test_res, rad);
if (comparison === "=") {
if (has_imaginary) test_base += "i";
return test_base;
} else if (comparison === "<") {
base = test_base;
added = true;
break;
}
}
if (!added) break;
}

if (has_imaginary) base += "i";

return base;
}

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

const ceil = require("./ceil.js");
const count_integer_digits = require("./count_integer_digits");
const divide = require("./divide.js");

function root_integer_digits(radicand, index) {
const digits = count_integer_digits(radicand);
if (digits === "0") return "0";
return ceil(divide(digits, index, { max_decimal_digits: 1 }));
}

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

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

function square_root(radicand, options) {
return root(radicand, "2", options);
}

module.exports = square_root;
module.exports.default = square_root;

0 comments on commit 0e30e7a

Please sign in to comment.