在C#中, MathF.Log()是MathF类方法。用于返回指定数字的对数。可以通过更改传递的参数数量来重载此方法。 MathF.Log()方法的重载列表中总共有2种方法,如下所示:
- MathF.Log(Single)方法
- MathF.Log(单,单)方法
MathF.Log(Single)方法
此方法用于返回指定数字的自然对数(以e为底)。
Syntax: 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为底的数字。返回值取决于传递的参数。以下是一些情况:
- 如果参数为正,则method将返回自然对数或log e (val) 。
- 如果参数为零,则结果为NegativeInfinity 。
- 如果参数为Negative(小于零)或等于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)方法
此方法用于以指定的底数返回指定数字的对数。
Syntax: public static float Log (float x, float y);
Parameters:
x: It is the specified number whose logarithm to be calculated and its type is System.Single.
y: It is the base of the logarithm of type System.Single.
返回值:返回x的对数,类型为System.Single 。
注意:返回值始终取决于传递的参数。下表描述了不同的情况:
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 |
例子:
// 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