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

เมธอด Double.IsPositiveInfinity() ใน C#

เมธอด Double.IsPositiveInfinity() ใน C# ใช้ตรวจสอบว่าค่า double ที่ระบุมีค่าเป็นอนันต์บวก (positive infinity) หรือไม่

ไวยากรณ์ (Syntax)

public static bool IsPositiveInfinity(double value);
  • value — ค่า double-precision floating-point ที่ต้องการตรวจสอบ
  • คืนค่า true หากเป็น positive infinity, มิฉะนั้น false

ตัวอย่างที่ 1: 1.0 / 0.0 = Positive Infinity

using System;

public class Demo
{
    public static void Main()
    {
        double d = 1.0 / 0.0;
        Console.WriteLine("Double Value = " + d);
        Console.WriteLine("HashCode of Double Value = " + d.GetHashCode());
        TypeCode type = d.GetTypeCode();
        Console.WriteLine("TypeCode of Double Value = " + type);
        Console.WriteLine("Positive Infinity? = " + Double.IsInfinity(d));
        Console.WriteLine("Check whether the specified value is NaN? = " + Double.IsNaN(d));
        Console.WriteLine("Does the value evaluate to negative infinity? = " + Double.IsNegativeInfinity(d));
        Console.WriteLine("Does the value evaluate to positive infinity? = " + Double.IsPositiveInfinity(d));
    }
}

ผลลัพธ์

Double Value = ∞
HashCode of Double Value = 2146435072
TypeCode of Double Value = Double
Positive Infinity? = True
Check whether the specified value is NaN? = False
Does the value evaluate to negative infinity? = False
Does the value evaluate to positive infinity? = True

ตัวอย่างที่ 2: -50.0 / 0.0 = Negative Infinity

using System;

public class Demo
{
    public static void Main()
    {
        double d = -50.0 / 0.0;
        Console.WriteLine("Double Value = " + d);
        Console.WriteLine("HashCode of Double Value = " + d.GetHashCode());
        TypeCode type = d.GetTypeCode();
        Console.WriteLine("TypeCode of Double Value = " + type);
        Console.WriteLine("Positive Infinity? = " + Double.IsInfinity(d));
        Console.WriteLine("Check whether the specified value is NaN? = " + Double.IsNaN(d));
        Console.WriteLine("Does the value evaluate to negative infinity? = " + Double.IsNegativeInfinity(d));
        Console.WriteLine("Does the value evaluate to positive infinity? = " + Double.IsPositiveInfinity(d));
    }
}

ผลลัพธ์

Double Value = -∞
HashCode of Double Value = -1048576
TypeCode of Double Value = Double
Positive Infinity? = True
Check whether the specified value is NaN? = False
Does the value evaluate to negative infinity? = True
Does the value evaluate to positive infinity? = False

สรุป

  • Double.IsPositiveInfinity(d) — true ก็ต่อเมื่อ d = +∞
  • Double.IsNegativeInfinity(d) — true ก็ต่อเมื่อ d = -∞
  • Double.IsInfinity(d) — true เมื่อ d = +∞ หรือ -∞
  • Double.IsNaN(d) — true เมื่อ d = NaN (Not a Number) เช่น 0.0/0.0, Math.Sqrt(-1)
  • ค่าคงที่: Double.PositiveInfinity, Double.NegativeInfinity, Double.NaN, Double.MaxValue, Double.MinValue, Double.Epsilon
  • การหารด้วยศูนย์ใน floating-point ไม่โยน exception — ให้ infinity หรือ NaN ตามเคส
  • ใช้ double.IsInfinity() (instance method) ได้เหมือนกันใน .NET ใหม่

ตารางสรุปค่าพิเศษของ Double

นิพจน์ผลลัพธ์IsPositiveInfinityIsNegativeInfinityIsNaN
1.0 / 0.0+∞truefalsefalse
-1.0 / 0.0-∞falsetruefalse
0.0 / 0.0NaNfalsefalsetrue
Double.PositiveInfinity+∞truefalsefalse
Double.NegativeInfinity-∞falsetruefalse
Double.NaNNaNfalsefalsetrue