Peter DeMartini
Peter DeMartini

Reputation: 105

Creating a log function with a custom base

I have a formula that uses a logarithm function with a custom base, for example: logarithm with a base of b and value of x. In Objective-C, I know there are log functions that calculate without 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 is there an alternative method of computing this formula?

The basic idea of my formula is this: log1.02 1.26825. This should equal 12.000.

Upvotes: 9

Views: 6962

Answers (3)

paxdiablo
paxdiablo

Reputation: 881633

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;
  • loge1.02 = 0.019802627; and
  • 0.237637997 / 0.019802627 = 12.0003268758.

To five significant digits, that's 12.000.


In fact, 1.0212 is actually 1.268241795 and, if you use that value rathet than the one you gave, you get much closer to twelve:

  • loge1.268241795 = 0.237631528;
  • loge1.02 = 0.019802627; and
  • 0.237631528 / 0.019802627 = 12.000000197.

Upvotes: 4

Dair
Dair

Reputation: 16240

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);
}

Upvotes: 1

Ray Toal
Ray Toal

Reputation: 88378

Like this:

double logWithBase(double base, double x) {
    return log(x) / log(base);
}

Upvotes: 21

Related Questions