Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Log functions in C#
With C#, you can easily work with logarithms using the Math class. It provides several methods to calculate logarithms with different bases, including natural logarithms (base e) and common logarithms (base 10).
Available Log Methods
| Method | Description |
|---|---|
| Log(Double) | Returns the natural (base e) logarithm of a specified number. |
| Log(Double, Double) | Returns the logarithm of a specified number in a specified base. |
| Log10(Double) | Returns the base 10 logarithm of a specified number. |
Syntax
Following are the syntax forms for logarithm methods −
Math.Log(number) // Natural logarithm (base e) Math.Log(number, baseValue) // Logarithm with custom base Math.Log10(number) // Base 10 logarithm
Parameters
-
number − A double-precision floating-point number whose logarithm is to be calculated.
-
baseValue − The base of the logarithm (used only in the two-parameter version).
Return Value
All logarithm methods return a double value representing the logarithm result. If the input is negative or zero, the result will be NaN (Not a Number) or negative infinity.
Using Natural Logarithm (Log)
using System;
class Demo {
static void Main() {
double val1 = Math.Log(1);
Console.WriteLine("Natural log of 1: " + val1);
double val2 = Math.Log(Math.E);
Console.WriteLine("Natural log of e: " + val2);
double val3 = Math.Log(10);
Console.WriteLine("Natural log of 10: " + val3);
}
}
The output of the above code is −
Natural log of 1: 0 Natural log of e: 1 Natural log of 10: 2.302585092994046
Using Base 10 Logarithm (Log10)
using System;
class Demo {
static void Main() {
double val1 = Math.Log10(1);
Console.WriteLine("Log10 of 1: " + val1);
double val2 = Math.Log10(10);
Console.WriteLine("Log10 of 10: " + val2);
double val3 = Math.Log10(1000);
Console.WriteLine("Log10 of 1000: " + val3);
}
}
The output of the above code is −
Log10 of 1: 0 Log10 of 10: 1 Log10 of 1000: 3
Using Custom Base Logarithm
using System;
class Demo {
static void Main() {
double val1 = Math.Log(8, 2);
Console.WriteLine("Log base 2 of 8: " + val1);
double val2 = Math.Log(125, 5);
Console.WriteLine("Log base 5 of 125: " + val2);
double val3 = Math.Log(64, 4);
Console.WriteLine("Log base 4 of 64: " + val3);
}
}
The output of the above code is −
Log base 2 of 8: 3 Log base 5 of 125: 3 Log base 4 of 64: 3
Common Use Cases
Logarithms are commonly used in scientific calculations, data analysis, and algorithms. The natural logarithm is frequently used in mathematical formulas, while base 10 logarithms are useful for converting between linear and logarithmic scales.
Conclusion
C# provides three logarithm methods in the Math class: Log() for natural logarithms, Log10() for base 10 logarithms, and Log(number, base) for custom base logarithms. These methods are essential for mathematical calculations involving exponential relationships and data transformations.
