当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


C# MathF.Log()用法及代码示例


在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


相关用法


注:本文由纯净天空筛选整理自Kirti_Mangal大神的英文原创作品 MathF.Log() Method in C# with Examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。