TheDeveloperBlog.com


C# Math.Log and Log10 Methods

Math.Log, Log10. The Math class provides logarithms. With the Math.Log and Math.Log10 methods in the System namespace, we compute logarithms with a specific base or base 10. These methods are tested—they do not need to be debugged.


Example. Here we compute the logarithms of some known numbers. The logarithm of 1 with base "e" is always zero—the method matches this result. The logarithm of 1000 with base 10 is always 3. This is because 10 to the power of 3 is 1000.

Finally: You can duplicate the effects of Log10 with Log by passing 10 as the second argument.

C# program that uses Log and Log10 methods

using System;

class Program
{
    static void Main()
    {
	double a = Math.Log(1);
	Console.WriteLine(a);

	double b = Math.Log10(1000);
	Console.WriteLine(b);

	double c = Math.Log(1000, 10);
	Console.WriteLine(c);
    }
}

Output

0
3
3


Uses. What are some uses for Math.Log and Math.Log10? Generally, these methods are used for scientific formulas. Developing a fractal generator in the C# language would be interesting, and it would involve logarithms.

Logarithm: Wikipedia

Summary. The Math.Log and Math.Log10 methods provide accurate results for logarithms in the C# language. They are built into the Framework. This means they don't require many additional development resources.

Tip: If you need to optimize their performance, try using a lookup table to cache or precompute the most common values.