Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Logarithms #268

Open
ZippyMagician opened this issue Aug 29, 2020 · 8 comments
Open

Logarithms #268

ZippyMagician opened this issue Aug 29, 2020 · 8 comments

Comments

@ZippyMagician
Copy link

Will a logarithm/natural logarithm function be introduced at some point? I am aware decimal.js supports these functions, but I prefer the base parsing of this library over the former.

@MikeMcl
Copy link
Owner

MikeMcl commented Aug 31, 2020

I have no plans to do this. I did introduce a logarithm function to bignumber.js so non-integer powers could be calculated, but it required so many changes that I instead released it as a separate library, decimal.js.

Dr Ron Knott, author of the excellent Dr Ron Knott's web pages on Mathematics, created a bignumberLIB.js library which adds a log function. See A BigNumber.js Library of Mathematical Constants and Functions.

It uses the older bignumber.js API, but I have worked around that and converted it to a module:

BigNumber LIB test.zip

It still needs some work though as can be seen from running node test.mjs, but it is not something I will be working on in the near future.

@emilioplatzer
Copy link

emilioplatzer commented Aug 4, 2021

If one day you want to consider it again, these are my arguments:

  • In finance calculations based on compound interest require ln and exp
  • Weighted geometric averages are usually used in the calculation of the consumer price index and in the cost of sales entry in accounting (geometric average = exp(sum(ln(n))/count(n)))

decimal.js cannot be used in finance because it rounds by precision and not by decimal positions.

@MikeMcl
Copy link
Owner

MikeMcl commented Aug 5, 2021

@emilioplatzer

Okay, I'll reconsider adding those functions.

Thanks for your input.

@c0ncentus
Copy link

any update on this ? @MikeMcl

@merlox
Copy link

merlox commented Apr 8, 2023

Even 3 years later it is needed.

@j4ys0n
Copy link

j4ys0n commented Apr 11, 2023

help us @MikeMcl 🙂

@MikeMcl
Copy link
Owner

MikeMcl commented Apr 12, 2023

I would like to add these functions but my time is very limited at the moment.

What is the maximum number of decimal places that would be required for ln and exp, as implementing them would be considerably simpler if that number was limited rather than arbitrarily large?

@emilioplatzer
Copy link

@MikeMcl your question is excelent. I think that the decimal places can be a parameter of the function (may be an optional one that by default is the default of the lib). For all, but in special for the ln function

TL;DR

In my use case (geometric average = exp(sum(ln(n))/count(n))) for be acurated its needed that ln has more decimals than the normal calculations. If you are calculating amounts in dolars most operations need to be with 2 decimal places (+, *, -, exp) and other need to have more(/, ln).

The reason is the unit of the magnitud. dolar + dolar = dolar, dolar - dolar = dolar, but dolar/dolar = real (without unit), that may be a factor, then there is needed more decimal places. But if you are dividing a total in parts you have dolar / real = dolar (that needs 2 decimal places). ln(dolar) is of type ln of dolar need many decimal places, and when it goes to exp(a) (if a = sum(ln(dolar))) we need only 2 decimal places to exp bacause exp(ln of dolar) has the unit dolar.

Appreciation

I like a lot your lib. If you add exp with de normal decimal places and ln with 8 or programable decimal places I be very happy. Or may be you can have 2 default, one for normal operations, other for ln and div.

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

No branches or pull requests

6 participants