Question

I'm looking for an algorithm to calculate ln(1-x). x is often small (<0.01), but occasionally it might be larger. Algorithm needs to be accurate, and not too slow. I'd rather not use library for ln(x), because I might lose accuracy.

Was it helpful?

Solution

Depending on the accuracy you want, -x is a good approximation to small ln(1-x). From here.

Edit: If the reason for needing the algorithm is getting the best accuracy, then there are many libraries that are specialised for log(1+x). For example, in Python use log1p. Ditto in C and C++.

OTHER TIPS

If you are using MATLAB the log1p() function was designed specifically for calculating ln(1+x) for small values of x.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top