C# | Math.Log() Method - GeeksforGeeks

31 Jan, 2019
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
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:
// C# program to demonstrate working// of Math.Log(Double) methodusing System;  class Geeks {      // Main Method    public static void Main(String[] args)    {          // double values whose logarithm         // to be calculated        double a = 4.55;        double b = 0;        double c = -2.45;        double nan = Double.NaN;        double positiveInfinity = Double.PositiveInfinity;               double negativeInfinity = Double.NegativeInfinity;          // Input is positive number so output        // will be logarithm of number        Console.WriteLine(Math.Log(a));          // positive zero as argument, so output         // will be -Infinity        Console.WriteLine(Math.Log(b));          // Input is negative number so output        // will be NaN        Console.WriteLine(Math.Log(c));          // Input is NaN so output        // will be NaN        Console.WriteLine(Math.Log(nan));          // Input is PositiveInfinity so output        // will be Infinity        Console.WriteLine(Math.Log(positiveInfinity));          // Input is NegativeInfinity so output        // will be NaN        Console.WriteLine(Math.Log(negativeInfinity));    }}
1.51512723296286
-Infinity
NaN
NaN
Infinity
NaN

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:
Example:
// C# program to demonstrate working// of Math.Log(Double, Double) methodusing System;  class Geeks {      // Main Method    public static void Main(String[] args)    {          // Here val = 1.3 and base is 0.3 then         // output will be logarithm of given value        Console.WriteLine(Math.Log(1.3, 0.3));                  // Here val is 0.5 and base > 1 then output         // will be -0.5        Console.WriteLine(Math.Log(0.5, 4));          // Here val is 0.7 and base = 1 then output         // will be NaN        Console.WriteLine(Math.Log(0.7, 1));          // Here val is 0.7 and base is NaN then output         // will be NaN        Console.WriteLine(Math.Log(0.7, Double.NaN));    }}
-0.217915440884381
-0.5
NaN
NaN

References:
https://msdn.microsoft.com/en-us/library/x80ywz41(v=vs.110).aspx
https://msdn.microsoft.com/en-us/library/hd50b6h5(v=vs.110).aspx
CSharp-Math
CSharp-method
C#
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
C# | Method Overriding
C# Dictionary with examples
Destructors in C#
Difference between Ref and Out keywords in C#
C# | Delegates
C# | Constructors
Extension Method in C#
C# | String.IndexOf( ) Method | Set - 1
C# | Class and Object
Introduction to .NET Framework