What is Math.Log() in C#?
C# has a built-in Math class that provides useful mathematical functions and operations. The class has the Log() function, which is used to compute the log of a specific number.
Natural log
Syntax
public static double Log (double value);
Parameter
value: It is of theDoubletype and represents the input value that is determined by findingLog(). Its range is all double numbers.
Return value
-
Double: It returns aDoublenumber representing the natural log of thevalue. -
NaN: It returns this for negative orNaNinput invalue. -
PositiveInfinity: It returns this ifvalueisPositiveInfinity. -
NegativeInfinity: It returns this ifvalueis0.
The natural log is the log to the base
e, which is a mathematical expression with value 2.71828.
Log to a base
Syntax
public static double Log (double value, double newbase);
Parameter
value: It is one of theDoubletype and represents the input value, which is determined by findingLog(). Its range is all double numbers.newbase: It is of theDoubletype and represents the new base value forLog(). Its range is all double numbers.
Return value
-
Double: It returns aDoublenumber representing the log to the basenewbaseof thevalue. -
NaN: It returns this if:value< 0;newbase= anyvalue= any;newbase< 0value!= 1;newbase= 0value!= 1;newbase= +∞value= NaN;newbase= anyvalue= any;newbase= NaNvalue= any;newbase= 1
-
PositiveInfinity: It returns this if:value= 0; 0 <newbase< 1value= + ∞;newbase> 1
-
NegativeInfinity: It returns this if:value= 0;newbase> 1value= +∞; 0 <newbase< 1
Example
using System;class Educative{static void Main(){Double result = Math.Log(1.2);System.Console.WriteLine("Log(1.2) = "+ result);Double result1 = Math.Log(9.9, 0.1);System.Console.WriteLine("Log(9.9, 0.1) = "+ result1);}}