How do you implement complex numbers into programming?

Click For Summary

Discussion Overview

The discussion focuses on implementing complex numbers in programming, exploring various programming languages and their support for complex arithmetic. Participants share methods for defining and manipulating complex numbers in languages such as C++, Fortran, and Python.

Discussion Character

  • Technical explanation
  • Exploratory

Main Points Raised

  • One participant inquires about evaluating functions of a complex variable in programming.
  • Another participant suggests representing a complex number using two variables for the real and imaginary parts and mentions the convenience of using a class in C++, specifically the standard std::complex class.
  • A participant notes that Fortran supports complex numbers natively, while C++ provides support through the header and overloaded operators.
  • Further elaboration on C++ includes an example demonstrating the use of the header for arithmetic operations on complex numbers.
  • A participant mentions that Python has a built-in "complex" type that simplifies the use of complex numbers.

Areas of Agreement / Disagreement

Participants generally agree on the existence of support for complex numbers in various programming languages, but there is no consensus on the best approach or implementation details across different languages.

Contextual Notes

Some limitations include potential differences in how complex numbers are handled across programming languages, as well as the need for custom functions in languages like C that do not have built-in support.

Who May Find This Useful

Programmers and developers interested in numerical computing, particularly those working with complex numbers in languages such as C++, Fortran, and Python.

epkid08
Messages
264
Reaction score
1
For instance, if I had a function of a complex variable z, how can I evaluate that function using a program?
 
Technology news on Phys.org
The complex number "5 + 4i" can be represented using 2 variables,

float real_part = 5, imag_part = 4;

of course it's easier if you put them into a class, C++ has the standard std::complex class.

Then you just define arithmetic operators on the class using operator overloading.
 
Fortan supports complex numbers, so it's not an issue. C++ supports complex numbers via the class complex <complex.h>, and the associated overloaded operators and math functions. For a language like C, you'd need to create a set of fuctions to do this.
 
Jeff Reid said:
C++ supports complex numbers via the class complex <complex.h>

In standard C++, the header file is <complex>, i.e. simply use

#include <complex>

This allows you to use the usual arithmetic operations on complex numbers, as well as providing various functions specific to complex numbers. For example:

Code:
#include <iostream>
#include <complex>

using namespace std;

int main ()
{
    complex<double> z1, z2, z3;
    z1 = complex<double> (1.0, 2.0);
    z2 = complex<double> (3.0, 4.0);
    cout << "z1 = " << z1 << endl;
    cout << "z2 = " << z2 << endl;
    z3 = z1 + z2;
    cout << "Sum = " << z3 << endl;
    z3 = z1 * z2;
    cout << "Product = " << z3 << endl;
    return 0;
}

which produces the output

Code:
z1 = (1,2)
z2 = (3,4)
Sum = (4,6)
Product = (-5,10)
 
Last edited:

Similar threads

Replies
65
Views
5K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 16 ·
Replies
16
Views
2K
Replies
65
Views
5K
  • · Replies 102 ·
4
Replies
102
Views
4K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 3 ·
Replies
3
Views
7K
Replies
10
Views
5K
  • · Replies 7 ·
Replies
7
Views
4K
  • · Replies 14 ·
Replies
14
Views
2K