Continuing the discussion from Standard complex number in std library
Proposal
Problem statement
Currently, there is no stable counterpart to the C _Complex types, and a myriad of implementations exist on crates.io. Popular crates like num-complex exist, but more often there are implementations defined by the crates themselves, which, while compatible with the other types in having the exact same implementation, require a myriad of conversion functions to convert with each other. Additionally, there is no syntax supported like 1+2i or 1+2j for complex numbers. (This ACP doesn't cover this, but it does lay ground for future work in this area)
Motivating examples or use cases
A scientific computing library's functions (can be written in C):
// in comp
extern double _Complex computes_function(x: double _Complex);
can be represented in Rust without the need of complex structs and the like:
extern "C" {
fn computes_function(x: c64) -> c64;
}
fn main() {
let returned_value = computes_function(c32::new(3, 4))
}
with the special repr that only the standard library can provide. (This would, however, require that the complex numbers become lang items.)
Solution sketch
Complex numbers will have a implementation similar to this:
// c16, c32, c64, c128 to be defined this way using f16, f32, f64 and f128 respectively
#[lang = "complex"] // defines a special C-compatible repr which matches the ABI
struct c32 {
re: f32,
im: f32
}
impl c32 {
fn conjugate() { ... }
// more function impls as desired
}
// All the trait impls as per LLVM intrinsics
impl Add for c32 {}
impl Sub for c32 {}
impl Mul for c32 {}
impl Div for c32 {}
// syntax can be changed to use j
// in main.rs
fn main() {
println!("{}", Complex(1, 2);
}
The complex numbers will serve as structs and all operations will be defined as a struct operation.
Alternatives
Don't put it into the std (status quo)
We could just not put complex numbers in the std and let the popular libraries work. This would be acceptable if not for improving FFI with C, as it already implements the _Complex types and adding them to Rust would significantly improve the FFI experience with libraries already in C. I do not recommend this yet.
Links and related work
Standard complex number in std library
What happens now?
This issue contains an API change proposal (or ACP) and is part of the libs-api team feature lifecycle. Once this issue is filed, the libs-api team will review open proposals as capability becomes available. Current response times do not have a clear estimate, but may be up to several months.
Possible responses
The libs team may respond in various different ways. First, the team will consider the problem (this doesn't require any concrete solution or alternatives to have been proposed):
- We think this problem seems worth solving, and the standard library might be the right place to solve it.
- We think that this probably doesn't belong in the standard library.
Second, if there's a concrete solution:
- We think this specific solution looks roughly right, approved, you or someone else should implement this. (Further review will still happen on the subsequent implementation PR.)
- We're not sure this is the right solution, and the alternatives or other materials don't give us enough information to be sure about that. Here are some questions we have that aren't answered, or rough ideas about alternatives we'd want to see discussed.
Edit: The ACP transitioned into an RFC which is uploaded on GitHub here