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


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


在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


参考文献:



相关用法


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