Title: Calculate logarithms in different log bases in C#
This example shows how to calculate logarithms with log bases other than base 10 and base e (the natural logarithm).
The .NET Framework's Math.Log function calculates the natural logarithm of a number. The Math.Log10 function calculates the logarithm in base 10. To calculate a log in a different base, you can use this formula:
LogB(N) = LogA(N)/LogA(B)
When you enter a number and a base and click the Find Log button, this example calculates the log of the number using that base. It displays the result and then raises the base to the calculated power to verify the result. The example uses the following LogBase method to perform the calculation.
// Calculate log(number) in the indicated log base.
private double LogBase(double number, double log_base)
{
return Math.Log(number) / Math.Log(log_base);
}
Download the example to experiment with it and to see additional details.
|