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. LLVM has a intrinsic for this, but it is not used in Rust as yet. 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.
Motivating examples or use cases
(I'm looking to develop this a bit further with code, but my basic idea is scientific computing FFI with C will have a target FFI type on this end)
Solution sketch
Complex numbers will have a implementation similar to this:
// llvm-intrinsic primitive c32, c64, c128
// impls for all the types
impl c32 {
fn re() { ... }
fn im() { ... }
}
// 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 to be used like this: 1 + 2j
// in main.rs
fn main() {
println!("{}", 1+2j); // prints 1 + 2j
}
The complex numbers will serve as primitives and all intrinsics will be defined as per MLIR intrinsics.
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. Additionally, with complex numbers being LLVM intrinsics, I suggest that we support it as part of the underlying backend. 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.
Request: Can anyone provide an example piece of code that they would like for this?