在C#中,MathF.Log()是MathF類方法。用於返回指定數字的對數。可以通過更改傳遞的參數數量來重載此方法。 MathF.Log()方法的重載列表中共有2種方法,如下所示:
- MathF.Log(Single)方法
- MathF.Log(Single, Single)方法
MathF.Log(Single) Method
此方法用於返回指定數字的自然對數(以e為底)。
用法: public static float Log (float x);
Here, x is the specified number whose natural (base e) logarithm to be calculated and its type is System.Single.
返回值:返回x的自然對數,其類型為System.Single。
注意:參數x始終指定為以10為底的數字。返回值取決於傳遞的參數。以下是一些情況:
- 如果參數為正,則方法將返回自然對數或loge(val)。
- 如果參數為零,則結果為NegativeInfinity。
- 如果參數為負(小於零)或等於NaN,則結果為NaN。
- 如果參數為PositiveInfinity,則結果為PositiveInfinity。
- 如果參數為NegativeInfinity,則結果為NaN。
例:
// C# program to demonstrate working
// of MathF.Log(Single) method
using System;
class Geeks {
// Main Method
public static void Main(String[] args)
{
// Single values whose logarithm
// to be calculated
float a = 9.78f;
float b = 0f;
float c = -4.56f;
float nan = Single.NaN;
float positiveInfinity = Single.PositiveInfinity;
float negativeInfinity = Single.NegativeInfinity;
// Input is positive number so output
// will be logarithm of number
Console.WriteLine(MathF.Log(a));
// positive zero as argument, so output
// will be -Infinity
Console.WriteLine(MathF.Log(b));
// Input is negative number so output
// will be NaN
Console.WriteLine(MathF.Log(c));
// Input is NaN so output
// will be NaN
Console.WriteLine(MathF.Log(nan));
// Input is PositiveInfinity so output
// will be Infinity
Console.WriteLine(MathF.Log(positiveInfinity));
// Input is NegativeInfinity so output
// will be NaN
Console.WriteLine(MathF.Log(negativeInfinity));
}
}
輸出:
2.280339 -Infinity NaN NaN Infinity NaN
Math.Log(Single, Single) Method
此方法用於以指定的底數返回指定數字的對數。
用法: public static float Log (float x, float y);
參數:
x:它是要計算其對數的指定數字,其類型為System.Single。 y:是類型System.Single的對數的底。
返回值:它返回x的對數,其類型為System.Single。
注意:返回值始終取決於傳遞的參數。下表描述了不同的情況:
值 | 基礎 | 返回值 |
---|---|---|
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 MathF.Log(Single, Single) method
using System;
class Geeks {
// Main Method
public static void Main(String[] args)
{
// Here val = 4.4f and base is 0.4f then
// output will be logarithm of given value
Console.WriteLine(MathF.Log(4.4f, 0.4f));
// Here val is 0.5f and base > 1f then output
// will be -0.5f
Console.WriteLine(MathF.Log(0.5f, 4f));
// Here val is 0.9f and base = 1f then output
// will be NaN
Console.WriteLine(MathF.Log(0.9f, 1f));
// Here val is 0.4f and base is NaN then output
// will be NaN
Console.WriteLine(MathF.Log(0.4f, Single.NaN));
}
}
輸出:
-1.616959 -0.5 NaN NaN
相關用法
- C# MathF.Tan()用法及代碼示例
- C# MathF.Abs()用法及代碼示例
- C# MathF.Sin()用法及代碼示例
- C# MathF.Pow()用法及代碼示例
- C# MathF.Cos()用法及代碼示例
- C# MathF.Max()用法及代碼示例
- C# MathF.Exp()用法及代碼示例
- C# MathF.Min()用法及代碼示例
- C# SByte.Equals用法及代碼示例
- C# SByte.GetHashCode用法及代碼示例
- C# Stack.ToString()用法及代碼示例
- C# SByte.CompareTo()用法及代碼示例
- C# SByte.GetTypeCode用法及代碼示例
- C# UInt32.GetHashCode用法及代碼示例
- C# UInt16.GetHashCode用法及代碼示例
注:本文由純淨天空篩選整理自Kirti_Mangal大神的英文原創作品 MathF.Log() Method in C# with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。