views:

100

answers:

1

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.

+3  A: 

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

Muhammad Alkarouri
The links you provide do not include an algorithm. I found one using log1p as a search term: http://golang.org/src/pkg/math/log1p.go does, but is has float precision
willem
They don't include an algorithm, but every programming language I know of can use a C library in one way or another. Also, the example you found has `float64` precision, which is IEEE double precision. Can you please tell us what is your acceptable error and why you don't want to use available libraries? According to your need, somebody here may be able to help.
Muhammad Alkarouri
Aah, I understand that float64 is similar/equal to double. Then the algorithm will probably suffice. My allowable error would be a relative error of about 10^{-9}.
willem