Currently I calculate log as following:
#define MAXLOG 1001
double myloglut[MAXLOG];
void MyLogCreate()
{
int i;
double exp, expinc;
expinc = (2.0 - 0.1) / MAXLOG;
for (i = 0, exp = 0.1; i <= MAXLOG; ++i, exp += expinc)
myloglut[i] = log(exp);
myloglut[478] = 0; // this one need to be precise
}
double MyLog(double v)
{
int idx = (int)((MAXLOG*(v - 0.1)) / (2.0 - 0.1));
return myloglut[idx];
}
As you can see I’m interested only in range 0.1 - 2.0. However, I need more precision around 0. How can I achieve that non linear calculation? Also is there any way to use some interpolation in this function for better precision?
Last version
Last versions’s characteristics:
loghas been calculated.Advantages over the last version:
cbrt, usespowinstead.ACRTPT))Third version
This version is cleaner and easier to maintain than the previous ones. If you need anything else, please leave a comment.
You can configure its behavior using the macros at the top of the file.
Caracteristics:
loghas been calculated.Second version
Well, here is my second solution. See below it for the original comment.
Original comment
The linearity of your calculation comes from the fact that you’re using a linear increment. On each iteration of your for loop, you increment
expby(2.0 - 0.1) / MAXLOG.To get more precise values around 0, you will need:
i(or onexp, depending on how you do it), so you know precisely the “offset” of the number you are trying to calculate (and the amount you need to incrementexpwith). Of course, you will calculate more results around 0.Here is my current implementation of it:
I’ve created a new type in order to store the value of exp too, which could be useful for knowing what value the result is the log of.
Update: I’m not sure about what you want to do. Do you want to be precise around log(x) = 0 or around x = 0? On the first case, I might have to re-write the code again for it to work as you want. Also, do you want the results to be more precise at it gets close to 0, or do you want the results to be more precise in a given range (as it is now)?