Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> C#

เมธอด Math.Exp() ใน C#

เมธอด Math.Exp() ใน C# คืนค่า e (Euler's Number ≈ 2.71828) ยกกำลังค่าที่ระบุ

ไวยากรณ์

public static double Exp(double val);

กรณีพิเศษ

  • val = NaN → คืน NaN
  • val = PositiveInfinity → คืน PositiveInfinity
  • val = NegativeInfinity → คืน 0

ตัวอย่างที่ 1: ค่าพิเศษ

using System;

public class Demo
{
    public static void Main()
    {
        Console.WriteLine(Math.Exp(0.0));           // e^0 = 1
        Console.WriteLine(Math.Exp(Double.PositiveInfinity)); // ∞
        Console.WriteLine(Math.Exp(Double.NegativeInfinity)); // 0
    }
}

ผลลัพธ์

1
∞
0

ตัวอย่างที่ 2: ค่าทั่วไปและ NaN

using System;

public class Demo
{
    public static void Main()
    {
        Console.WriteLine(Math.Exp(10.0));   // e^10
        Console.WriteLine(Math.Exp(Double.NaN)); // NaN
    }
}

ผลลัพธ์

22026.4657948067
NaN

การประยุกต์: ใช้ในการคำนวณดอกเบี้ยทบต้น, การเติบโตแบบ Exponential, Probability Distributions (Normal Distribution), Machine Learning (Softmax)