[C# Helper]
Index Books FAQ Contact About Rod
[Beginning Database Design Solutions, Second Edition]

[Beginning Software Engineering, Second Edition]

[Essential Algorithms, Second Edition]

[The Modern C# Challenge]

[WPF 3d, Three-Dimensional Graphics with WPF and C#]

[The C# Helper Top 100]

[Interview Puzzles Dissected]

[C# 24-Hour Trainer]

[C# 5.0 Programmer's Reference]

[MCSD Certification Toolkit (Exam 70-483): Programming in C#]

Title: Calculate logarithms in different log bases in C#

example

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.

© 2009-2023 Rocky Mountain Computer Consulting, Inc. All rights reserved.