MikeMcl / bignumber.js

A JavaScript library for arbitrary-precision decimal and non-decimal arithmetic
http://mikemcl.github.io/bignumber.js
MIT License
6.68k stars 742 forks source link

Logarithms #268

Open ZippyMagician opened 4 years ago

ZippyMagician commented 4 years ago

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 commented 4 years ago

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 commented 3 years ago

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

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

MikeMcl commented 3 years ago

@emilioplatzer

Okay, I'll reconsider adding those functions.

Thanks for your input.

c0ncentus commented 1 year ago

any update on this ? @MikeMcl

merlox commented 1 year ago

Even 3 years later it is needed.

j4ys0n commented 1 year ago

help us @MikeMcl 🙂

MikeMcl commented 1 year ago

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 commented 1 year ago

@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.