Computer >> कंप्यूटर ट्यूटोरियल >  >> प्रोग्रामिंग >> C#

C# में कॉम्प्लेक्स नंबर्स (Complex Numbers): स्ट्रक्चर और ऑपरेटर ओवरलोडिंग


कॉम्प्लेक्स नंबर: a + bi जहां a वास्तविक भाग, b काल्पनिक भाग, i = √-1। C# में System.Numerics.Complex स्ट्रक्चर बिल्ट-इन है (`.NET Framework 4.0+`, `.NET Core`), लेकिन कस्टम स्ट्रक्चर भी बना सकते हैं।

कस्टम Complex स्ट्रक्चर उदाहरण

using System;

public struct Complex
{
    public int Real;
    public int Imaginary;
    
    public Complex(int real, int imaginary)
    {
        Real = real;
        Imaginary = imaginary;
    }
    
    // ऑपरेटर ओवरलोडिंग: +
    public static Complex operator +(Complex one, Complex two)
    {
        return new Complex(one.Real + two.Real, one.Imaginary + two.Imaginary);
    }
    
    // ऑपरेटर ओवरलोडिंग: -
    public static Complex operator -(Complex one, Complex two)
    {
        return new Complex(one.Real - two.Real, one.Imaginary - two.Imaginary);
    }
    
    public override string ToString()
    {
        return $"{Real} + {Imaginary}i";
    }
}

class Demo
{
    static void Main()
    {
        Complex val1 = new Complex(7, 1);
        Complex val2 = new Complex(2, 6);
        
        Complex res = val1 + val2;
        
        Console.WriteLine("पहला: {0}", val1);
        Console.WriteLine("दूसरा: {0}", val2);
        Console.WriteLine("परिणाम (योग): {0}", res);
        Console.ReadLine();
    }
}

आउटपुट

पहला: 7 + 1i
दूसरा: 2 + 6i
परिणाम (योग): 9 + 7i

बिल्ट-इन System.Numerics.Complex

using System.Numerics;

Complex c1 = new Complex(3, 4);
Complex c2 = new Complex(1, 2);
Complex sum = c1 + c2;           // (4, 6)
Complex product = c1 * c2;       // (-5, 10)
double magnitude = c1.Magnitude; // 5
double phase = c1.Phase;         // ~0.927 रेडियन

मुख्य बिंदु

  • struct उपयोग करें (वैल्यू टाइप, हीप आवंटन नहीं)।
  • ऑपरेटर ओवरलोडिंग: +, -, *, /, ==, !=।
  • बिल्ट-इन Complex में ट्रिग्नोमेट्रिक, लॉगरिदमिक, पावर फंक्शन्स शामिल।
  1. JavaScript नंबर्स उदाहरण JavaScript नंबर्स उदाहरण

    JavaScript में नंबर्स के लिए एक उदाहरण निम्न है -उदाहरण<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title>Document</title> &

  1. - Python में मापांक (Modulus) जटिल संख्याओं के साथ कैसे काम करता है?

    Python में जटिल संख्याओं पर मापांक ऑपरेटर फ्लोर और मापांक ऑपरेटर्स (// और % क्रमशः) Python 3.x में जटिल संख्या पर उपयोग करने की अनुमति नहीं है। हालांकि, ये ऑपरेशन Python 2.7.x में जटिल संख्याओं के लिए परिभाषित हैं। Python 3 >>> x = 9+2j >>> y = 2+1j >>> x % y Traceback (

  1. - पायथन में कॉम्प्लेक्स नंबर्स का उपयोग कैसे करें?

    एक कॉम्प्लेक्स नंबर वास्तविक संख्याओं a और b का एक जोड़ा होता है, जिसे आमतौर पर a+bi या a+ib के रूप में लिखा जाता है, जहां i को काल्पनिक इकाई कहा जाता है और यह दूसरे पद के लिए लेबल के रूप में कार्य करता है। गणितीय रूप से, i² = -1। कभी-कभी i के बजाय j का उपयोग किया जाता है। यहां बताया गया है कि कॉम्