在C#中,Math.Log()是Math類方法。用於返回指定數字的對數。可以通過更改傳遞的參數數量來重載此方法。 Math.Log()方法的重載列表中共有2種方法,如下所示:
- Math.Log(Double)方法
- Math.Log(Double,Double)方法
Math.Log(Double)方法
此方法用於返回指定數字的自然對數(以e為底)。
用法:
public static double Log(double val)
參數:
- val:它是要計算其自然對數(以e為底)的指定數字,其類型為System.Double。
返回值:返回val的自然對數,其類型為System.Double。
注意:始終將參數val指定為以10為底的數字。返回值取決於傳遞的參數。以下是一些情況:
- 如果參數為正,則方法將返回自然對數或loge(val)。
- 如果參數為零,則結果為NegativeInfinity。
- 如果參數為負(小於零)或等於NaN,則結果為NaN。
- 如果參數為PositiveInfinity,則結果為PositiveInfinity。
- 如果參數為NegativeInfinity,則結果為NaN。
例:
// C# program to demonstrate working
// of Math.Log(Double) method
using 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
Math.Log(Double,Double)方法
此方法用於以指定的底數返回指定數字的對數。
用法:
public static double Log(double val, double base)
參數:
- val:它是要計算其對數的指定數字,其類型為System.Double。
- base:它是System.Double類型的對數的底。
返回值:它返回val的對數,其類型為System.Double。
注意:返回值始終取決於傳遞的參數。下表描述了不同的情況:
值 | 基礎 | 返回值 |
---|---|---|
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 |
例:
// C# program to demonstrate working
// of Math.Log(Double, Double) method
using 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
參考文獻:
- https://msdn.microsoft.com/en-us/library/x80ywz41(v=vs.110).aspx
- https://msdn.microsoft.com/en-us/library/hd50b6h5(v=vs.110).aspx
相關用法
- C# Dictionary.Add()用法及代碼示例
- C# DateTimeOffset.Add()用法及代碼示例
- C# SortedDictionary.Add()用法及代碼示例
- C# TimeSpan.Add()用法及代碼示例
- C# Uri.IsHexEncoding()用法及代碼示例
注:本文由純淨天空篩選整理自Kirti_Mangal大神的英文原創作品 C# | Math.Log() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。