Skip to content

Conversation

@GabrielRhodes
Copy link

Created a function for calculating logarithms, including a natural logarithm function. Euler's number is calculated using the Taylor series for e, which could possibly be improved with binary splitting if needed. The logarithm of a number is calculated using the algorithm published by Daniel Shanks here:

https://www.ams.org/journals/mcom/1954-08-046/S0025-5718-1954-0061464-9/S0025-5718-1954-0061464-9.pdf

Example driver code would be:
let a = new BigNumber('9')
console.log(a.logBase(3))
console.log(BigNumber.e().naturalLog())
// output:
// 2
// 1

Returns NaN in the event that the user passes Infinity or a number <= 0 as a parameter. Similarly, if the base of the log is set to 1, the result is also NaN since log(1, n) = undefined

@magmel48
Copy link

@MikeMcl could you please consider this PR?

@MikeMcl
Copy link
Owner

MikeMcl commented May 4, 2023

This PR has not been tested properly by its author and is not likely to be merged.

It's a decent effort but the implementations of the log and ln functions are not reliable or performant enough to be included here.

Here is a zip file of a folder containing some test files that compare the PR against decimal.js.

test.zip

The PR is in the bignumber.mjs file and I have made some corrections to it in bignumber_amended.mjs. With the corrections the implementations seem to be reliable although I have done limited testing.

Unzip the zip file, and from a command line in the resulting directory use Node.js to run the test files.

Run multiple logarithm tests:
$ node logs.mjs {count} {base}

Run multiple natural logarithm tests:
$ node lns.mjs {count}

Run a single logarithm test:
$ node log.mjs {n} {base} {decimal places} {rounding mode}

Run a single natural logarithm test:
$ node ln.mjs {n} {decimal places} {rounding mode}

To test the amended version, amend the import in each test file so that bignumber_amended.mjs is used instead of bignumber.mjs.

@MikeMcl
Copy link
Owner

MikeMcl commented May 4, 2023

@magmel48

Review complete. This PR (amended) could be published separately as extension methods and if the author wants to do that I could add a link to it in the README here but I am not going to include these implementations internally.

@ronknott
Copy link

ronknott commented May 5, 2023 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants