Anda di halaman 1dari 3

Calculating Change-Base-Log in C++

Before we start to learn Calculating Change-Base-Log in C++ it would be better to an Intro About it: 1) What is Logarithm? 2) How to Use it ? 3) What is the Formula for Calculating Change-Base log?

1) Logarithms were introduced by John Napier in the early 17th century as a means to simplify calculations. The logarithm of a number is the exponent to which another fixed value, the base, must be raised to produce that number. For example, the logarithm of 1000 to base 10 is 3, because 1000 is 10 to the power 3: 1000 = 101010 = 103. More generally, if x = by, then y is the logarithm of x to base b, and is written y = logb(x), so log10(1000) = 3. (WiKi)

2) Q. How many 2s do we multiply to get 16? Answer: 2 2 2 x 2 = 16, so we needed to multiply 3 of the 2s to get 8 So the logarithm is 4 As we Write log2(16)=4

3) Formula:
In order to evaluate a non-standard-base log, you have to use the Change-of-Base formula:

What this rule says, in practical terms, is that you can evaluate a non-standard-base log by converting it to the fraction of the form "(standard-base log of the argument) divided by (same-standard-base log of the non-standard-base)". I keep this straight by looking at the position of things. In the original log, the argument is "above" the base (since the base is subscripted), so I leave things that way when I split them up:

Source: http://www.purplemath.com/modules/logrules5.htm

Ok now back to Our main Topic How to Use change Base in C++. As you know c++ support log2 and log10 only .see Below Example:

#include <iostream> #include <cmath> using namespace std; int main() { cout << "Log of 8 is:: " << log2(8) << endl; cout << "Log of 100 is:: " << log10(100) << endl; return 0; }

So When We will Compile the above code we will get exact natural Log. But what if we want to calculate log5(625), Lets try to calculate as we try above : #include <iostream> #include <cmath> using namespace std; int main() { cout << "Log of 625 is:: " << log5(625) << endl; return 0; }

Your Compiler is not going to Compile this program at all because c++ can calculate only log2() and log10() base Logarithms so thats why I am writing this to Show you how to Calculate log5 in c++. This is very Simple as you see Above the Formula for change base log :

Using this we can calculate and get our answer. I am not going more in Detail just let me show you how to do that with Example. Lets say we want to calculate log5(625) in c++ so lets try it first on a paper so get answer. As we write on paper log5(625)=4 so now we know the answer so lets try it with c++ and see what answer we will get for the same log5(625).

#include <iostream> #include <cmath> using namespace std; int main() { cout << "Log of 8 is:: " << log2(8) << endl; cout << "Log of 100 is:: " << log10(100) << endl; // we will use change base formula here which is logb(x)=logd(x)/log(d)b cout << "log5(625)= " << log(625)/log(5) << endl; return 0; } As we compile this Bingo we got the right answer. I hope it helps you and you understand what I want to Explain For any kind of Question mention me @hacktw or Like us on facebook/hackbookk Regard Aamir khan

Anda mungkin juga menyukai