In C#,
Math.Log() is a Math class method. It is used to return the logarithm of a specified number. This method can be overloaded by changing the number of the arguments passed. There are total 2 methods in the overload list of the
Math.Log() method as follows:
- Math.Log(Double) Method
- Math.Log(Double, Double) Method
Math.Log(Double) Method
This method is used to return the natural (base e) logarithm of a specified number.
Syntax:
public static double Log(double val)
Parameter:
val: It is the specified number whose natural (base e) logarithm to be calculated and its type is System.Double.
Return Value: Returns the natural logarithm of
val and its type is
System.Double.
Note: Parameter
val is always specified as a base 10 number.The return value is depend on the argument passed. Below are some cases:
- If the argument is positive then method will return the natural logarithm or loge(val).
- If the argument is zero, then the result is NegativeInfinity.
- If the argument is Negative(less than zero) or equal to NaN, then the result is NaN.
- If the argument is PositiveInfinity, then the result is PositiveInfinity.
- If the argument is NegativeInfinity, then the result is NaN.
Example:
Output:
1.51512723296286
-Infinity
NaN
NaN
Infinity
NaN
Math.Log(Double, Double) Method
This method is used to return the logarithm of a specified number in a specified base.
Syntax:
public static double Log(double val, double base)
Parameter:
val: It is the specified number whose logarithm to be calculated and its type is System.Double.
base: It is the base of the logarithm of type System.Double.
Return Value: It returns the logarithm of
val and its type is
System.Double.
Note: The return value is always depend on the argument passed. Below table depicts the different cases:
| val |
base |
Returned Value |
| val > 0 |
(0 < Base < 1) or(Base > 1) |
logbase(val) |
| val < 0 |
any value |
NaN |
| any value |
base < 0 |
NaN |
| val != 1 |
base = 0 |
NaN |
| val != 1 |
base = +ve Infinity |
NaN |
| val = NaN |
any value |
NaN |
| any value |
base = NaN |
NaN |
| any value |
base = 1 |
NaN |
| val = 0 |
(0 < Base < 1) |
+ve Infinity |
| val = 0 |
base > 1 |
-ve Infinity |
| val = +ve Infinity |
(0 < Base < 1) |
-ve Infinity |
| val = +ve Infinity |
base > 1 |
+ve Infinity |
| val = 1 |
base = 0 |
0 |
| val = 1 |
base = +ve Infinity |
0 |
Example: