Summary

FFI-compatible and calling-convention-compatible complex types are to be introduced into core to ensure synchronity with C primitives.

Motivation

The C standard defines the memory layout of a complex number, but not their calling convention. This means crates like num-complex require workarounds to interface with FFI using _Complex, and cannot pass values directly. The addition of complex numbers to Rust as a lang-item ensures a correct calling convention consistent with C on all platforms, thus better allowing C interop.

In essence, this RFC makes code like this:

extern double _Complex computes_function(double _Complex x);

callable in Rust without indirection:

extern "C" {
  fn computes_function(x: Complex<f64>) -> Complex<f64>;
}
fn main() {
  let returned_value = computes_function(Complex::<f64>::new(3.0, 4.0))
}

using the standard library’s FFI-compatible complex numbers.

Guide-level explanation

let x = Complex::new(3.0, 4.0);

Simple arithmetic is supported:

let first = Complex::new(1.0, 2.0);
let second = Complex::new(3.0, 4.0);
let a = first + second; // 4 + 6i
let b = first - second; // -2 - 2i
let c = first * second; // -5 + 10i
let d = float_second / float_first; // 0.44 - 0.8i

Reference-level explanation

The core crate will provide implementations for operator traits for possible component types. They will have an internal representation similar to this (with public fields for real and imaginary parts):

// in core::num::complex, which would be a private module holding complex types
#[lang = "complex"] // for calling convention.
#[repr(C)]
#[derive(Copy, Clone, PartialEq, Debug)]
pub struct Complex<T> {pub re: T, pub im: T};

have a constructor

impl Complex<T> {
  fn new(re: T, im: T) -> Self;
}

and have simple arithmetic implementations supported:

impl<T: Add> Add for Complex<T> { type Output = Self; /* ... */ }
impl<T: Sub> Sub for Complex<T> { type Output = Self; /* ... */ }

impl Mul for Complex<T> where T: Add + Sub + Mul{ type Output = Self; /* ... */ }
impl Div for Complex<T> where Complex<T>: Div<T> { type Output = Self; /* ... */ }

Drawbacks

The multiple emitted calls to libgcc.so (__mulsc3 and the like) via compiler-builtins may cause a bit of overhead and may not be what the Rust lang team and compiler team want.

Rationale and alternatives

The rationale for this type is mostly FFI: C libraries that may be linked from Rust code currently cannot provide functions with direct struct implementations of Complex - they must be hidden under at least a layer of indirection. This is because of the undefined calling convention of complex numbers in C. For example: on powerpc64-linux-gnu, returning double _Complex doesn’t do the same thing as returning a struct with a field of type double[2]. However, it is not always possible to write a C complex-valued function that wraps the first function in a pointer. Thus, FFI becomes a problem if such complex-valued functions are passed by value and not by reference.

Additionally, this provides a unified API for complex numbers. Right now, many crates define their own complex types, making interoperability complicated, even though num-complex already exports its own type. (rug::Complex being an example.) You could theoretically do something like this:

double _Complex function(double _Complex value);
void wrapper_function(double _Complex* value, double _Complex* out) {
    *out = function(*value);
}

for all functions you wish for. But this still needs to happen in C.

Alternatives:

$\left(r_1\angle\theta_1\right) + \left(r_2\angle\theta_2\right) = \left(\sqrt{r_1^2+r_2^2+2r_1r_2\cos(\theta_1-\theta_2)}\right)\angle(atan2({r_1\sin\theta_1+r_2\sin\theta_2},{r_1\cos\theta_1+r_2\cos\theta_2}))$

which offsets any benefits that multiplication may bring.

Prior art

FORTRAN, C, C++, Go, Perl and Python all have complex types implemented in the standard library or as a primitive. This clearly appears to be an important feature many languages have. For example, in Python:

complex_num = 1 + 2j
complex_second = 3 + 4j
print(complex_num * complex_second)

or in C:

float _Complex cmplx = 1 + 2*I;
float _Complex two_cmplx = 3 + 4*I;
printf("%.1f%+.1fi\n", creal(cmplx * two_cmplx), cimag(cmplx * two_cmplx));

Even in Rust, it has been discussed two times in IRLO:

Many crates, like num-complex also provide this feature, though it is not FFI-safe.

Unresolved questions

Future possibilities

impl<T: Float> Complex<T: Float> {
  const I: T = Complex::new(T::zero(), T::one());
}

where zero and one is implemented on the Float trait similar to num_traits? Or maybe we could have a method on normal numbers:

// for example
impl f32 {
  fn i(self) -> Complex<f32> {
    Complex::new(0, self)
  }
}

that could help simplify the life of people who otherwise would have to keep writing Complex::new()?