Creating a log function with a custom base
I have a formula and this formula uses a log function with a custom base for example log with a base of b and value of x. In objective-c, I know there are log functions that calculate with开发者_JAVA百科out a base and base of either 2 or 10.
Is there a function that has the ability to calculate a log function with a custom/variable base? or maybe there is an alternative method of completing this formula.
The basic idea of my formula is this log(1+0.02)(1.26825) (1+0.02 is the base). This should equal 12.000.
Like this:
double logWithBase(double base, double x) {
return log(x) / log(base);
}
You can calculate arbitrary logarithms with logbx = logcx / logcb, where c is one of the more readily available bases such as 10 or e.
For your particular example, loge1.26825 = 0.237637997 and loge1.02 = 0.019802627. That's 12.000 (within the limits of my calculator's accuracy): 0.237637997 / 0.019802627 = 12.000326876.
In fact, 1.0212 is actually 1.268241795 and, if you use that value, you get much closer to 12:
loge1.268241795 = 0.237631528loge1.02 = 0.0198026270.237631528 / 0.019802627 = 12.000000197.
Ray is right but here is a Obj-C method modification of it:
-(double) logWithBase:(double)base andNumber:(double)x {
return log(x) / log(base);
}
加载中,请稍侯......
精彩评论