📜  C#| Math.Log()方法

📅  最后修改于: 2021-05-29 19:44:53             🧑  作者: Mango

在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的自然对数,其类型为System.Double

注意:参数val总是以10为基数,返回值取决于传递的参数。以下是一些情况:

  • 如果参数为正,则method将返回自然对数或log e (val)
  • 如果参数为零,则结果为NegativeInfinity
  • 如果参数为Negative(小于零)或等于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

注意:返回值始终取决于传递的参数。下表描述了不同的情况:

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 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/zh-CN/library/x80ywz41(v=vs.110).aspx
  • https://msdn.microsoft.com/zh-CN/library/hd50b6h5(v=vs.110).aspx