# Logarithm

Hi experts,
What is the application of logarithm and where to use it and give an example of a problem which we can solve using basic arithmatic(addition,mul,sub,etc...) and solving same problem using logarithm.I am not too skill in maths.Please help me with a good example for logarithm.

Regards,
Vimal.
LVL 2
###### Who is Participating?

Commented:
If I understand your question, you are wanting to replace the logarithm function in c++ with something faster by using the four basic math functions.

I know nothing about how it is done in any specific compiler, but can offer a couple of comments here.

As far as the algorithm goes, there are a number of them out there.  Try Googling "algorithm for logarithm".

As far as speed goes, I'm skeptical.  At least in the Intel world, ever since the 8087 chip (a coprocessor option for the original IBM PC), there has been hardware support for logarithms in the CPU available to most computers.  Since the first Pentium, I believe it has been a standard part of the CPU.  I would be surprised if external code could run faster than internal microcode for calculating logarithms, except in special cases.

One special case would be if you don't need the precision that the CPU offers.  That might make for a faster calculation.

This is all speculation, of course, and would welcome anyone who has better information to comment on it.
0

Commented:
I'm not sure that a problem which is solved with logs is also going to be solved with simple arithmetic.

It's not quite what you are after, but a great example of logarithms is in the news right now - earthquakes are measured on a log scale.

Although the official measurement of quakes is done on the Moment magnitude Scale, ( http://en.wikipedia.org/wiki/Moment_magnitude_scale ), the media still like to report using the old Richter magnitude scale ( http://en.wikipedia.org/wiki/Richter_magnitude_scale ) - probably because more people have heard of it.

The energy released by an earthquake, which closely correlates to its destructive power, scales with the power of 1.5 of the shaking amplitude. A difference in magnitude of 1.0 is equivalent to a factor of 31.6 in the energy released; a difference in magnitude of 2.0 is equivalent to a factor of 1000 in the energy released.

So the difference between the 8.9 size quake in Japan and the recent 6.3 size quake in New Zealand is 8.9-6.3=2.6, so the difference in destructive power is (inverse log 2.6) to the power of 1.5 - which gives an answer of nearly 8000x the destructive force in the recent Japan quake!

By the way, inverse log of x = 10 to the power of x.
0

Commented:
Let's say you have a tournament (with lots of teams) and you want to find out how many rounds there have to be. Each round removes half the group, so you could divide the number by 2 over and over until you get to <1 and count the divisions, or you could just use log. (Log base 2 of course).
Example:
65536 teams
/2 = 32768
/2 = 16384
/2 = 8192
/2 = 4096
/2 = 2048
/2 = 1024
/2 = 512
/2 = 256
/2 = 128
/2 = 64
/2 = 32
/2 = 16
/2 = 8
/2 = 4
/2 = 2
/2 = 1

=16 rounds
log2(65536) = 16. Done!
0

Commented:
If you don't have log2 you can use ln(x)/ln(2)
ln(65536)/ln(2) = 16
0

Commented:
Hi,

as mentioned shortly above, logarithms can play role wherever exponential functions are used.

It's application could be useful when solving some population-related problems.

Consider a single-cell organism, which replicates itself every minute. To solve the question of how many cells will there be after t = 100 minutes you can use following formula: 2^t = 2^100.

To solve an inverse problem: determine the time needed to reaching the number of 1.000.000 cells. Here's where the logarithm will be the way to go:

2^t = 1.000.000 where the variable t is unknown. following formula solves this: t = log2(1.000.000) = ln(1.000.000)/ln(2)
0

Commented:
As an example of the use of logarithms, take a look at a slide ruler.  By having bars in logarithmic scales, you can multiply by adding.  That is, adding logarithms together can give you the product of the original numbers.
0

Author Commented:
Hi All,
I am a C++ programmer we have direct functions in c++ for finding logarithms. From the above comments I understand that when ever we are dealing with exponential  operations we can use logarithm. But I think we have solve the exponential  operations with out log also.For example
as TommySzalapski's example we can solve the same problem by doing LCM. I want to know the complexity of the TommySzalapski's example or the other examples mentioned above all comments which will take less time for do the job. How internally logarithm solves this problems what type of algorithm they are using. Since I am a programmer while designing a program i need to choose a less complexity method.

Thanks & Regards,
Vimal.
0

Commented:
Suppose you are dealing with audio frequencies and you want to jump up one octave.  That is an easy matter of just doubling the frequency.  Suppose instead that you want to write a function that allows you to jump up an arbitrary number of octaves, not necessarily integral.  Logarithms will make that much easier.
0

Commented:
Well, log base 2 can be computed in O(lg(lg(n))) time (which is rediculously fast, it takes 5 instruction sets to compute for numbers in the 4000000000 range).

Wikipedia has one version (you can make it a bit faster by hard coding more).
http://en.wikipedia.org/wiki/Binary_logarithm

Since logs can be converted easily (log10(x) = log2(x)/log2(10)) then you can do any logarithm by using log 2 and then dividing by a constant. So all logarithms have a complexity of O(lg(lg(n))).
0

Author Commented:
Hi CompProbSolv,
Thanks for your precious reply.See in c++ by calling a single api log() we can achieve
the doubling the frequency or exponential operations but what my doubt is what is the internal concept of logarithm what type of maths operation they are using (+,-,/,*,etc). Why i am asking is in c++ application especially signal processing as you said performance is the main factor so before choosing a method we need to know its details. Now i am developing a software dealing with dsp. I think you can guide me for this doubt.

Thanks & regards,
Vimal
0

Commented:
Did you see http:#a35167966? It details how the log can be computed (it's very very efficient). The wikipedia article shows how it can be calculated for integers. For floats (which are stored as a mantissa and exponent) the log base 2 is basically there already. (The exponent is the log base 2 rounded down, and the mantissa gives the rest). So for floats and doubles, it's O(1).
0

Commented:
That should read 'rounded up' not 'rounded down'.
0

Author Commented:
Hi TommySzalapski and CompProbSolv,

I am satisfied with both your comments. One more question in school days i have used a logarithm table now I forget every thing about logarithm table.Because I am remembering we were used log table for solving log based problems with out using scientific calculator.Is CPU using any log tables for solving logarithms.

thanks & Regards,
Vimal.
0

Commented:
Nope. Since you can compute the log2 so quickly in binary (which is what the computer uses of course), there is no need to use a table. Again, floats and doubles are basically stored in log2 form. We just use tables since we store everything as decimals in base 10.
0

Commented:
A log table just gives you the logs for a bunch of numbers. You just pick whichever one is closest to your number to get an approximation.
0

Author Commented:
Hi TommySzalapski and CompProbSolv,

You both has solved my doubt. Thanks Very much. I have some doubts in other maths topics I will post that by 2-3 days.

Thanks & regards,
Vimal.
0

Commented: