Calculate logarithms in different log bases in C#

calculate log bases

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 Example




This entry was posted in algorithms, mathematics and tagged , , , , , , , , , , . Bookmark the permalink.

One Response to Calculate logarithms in different log bases in C#

  1. Waylon says:

    It is really a nice and helpful piece of info. I’m happy that you simply shared this useful info with
    us. Please stay us informed like this. Thank you for sharing.

Leave a Reply

Your email address will not be published. Required fields are marked *