Calculate logarithms in different bases in C#
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) = Log(N)/Log(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.
private void btnFindLog_Click(object sender, EventArgs e)
{
double num = double.Parse(txtNumber.Text);
double log_base = double.Parse(txtBase.Text);
double result = Math.Log(num) / Math.Log(log_base);
txtResult.Text = result.ToString();
txtVerify.Text = Math.Pow(log_base, result).ToString();
}


Comments