multiplying a complex number

The C++ standard library has some support for complex numbers. I've never used it before, but here is a quick example. You'll have to experiment from here, or perhaps others that have used it more will reply. You can also use <float> rather than <double>

Code:
#include <iostream>     
#include <complex> // for std::complex, std::real, std::imag     
using namespace std;

void setup() {
  Serial.begin(9600);
  while (!Serial && millis() < 3000) {}
  
  std::complex<double> a(10.0, 2.0);  // 10 + 2i
  std::complex<double> b(12.0, 4.0);  // 12 + 4i

  std::complex<double> c = a + b;
  std::complex<double> d = a * b;

  cout << "real(a): " << real(a) << endl;
  cout << "imag(a): " << imag(a) << endl;

  cout << "real(b): " << real(b) << endl;
  cout << "imag(b): " << imag(b) << endl;

  cout << "real(c): " << real(c) << endl;
  cout << "imag(c): " << imag(c) << endl;

  cout << "real(d): " << real(d) << endl;
  cout << "imag(d): " << imag(d) << endl;
}

void loop() {
}
 
Back
Top