-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4347334
commit 0e30e7a
Showing
4 changed files
with
107 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |