Skip to content

Conversation

@scimind2460
Copy link

@scimind2460 scimind2460 commented Dec 2, 2025

This RFC proposes FFI-compatible complex numbers to help scientific computing library authors use non-indirected complexes.

I apologise in advance to num-complex

Rendered

@ehuss ehuss added the T-libs-api Relevant to the library API team, which will review and decide on the RFC. label Dec 2, 2025
@joshtriplett joshtriplett added the T-lang Relevant to the language team, which will review and decide on the RFC. label Dec 2, 2025
@joshtriplett
Copy link
Member

Labeling this T-lang because the desire to make this FFI-compatible is a lang matter.

@joshtriplett joshtriplett added the I-libs-api-nominated Indicates that an issue has been nominated for prioritizing at the next libs-api team meeting. label Dec 2, 2025
@clarfonthey
Copy link

clarfonthey commented Dec 2, 2025

It's worth pointing out another big issue with this is that the canonical a+bi is not actually the best representation of complex numbers in all cases, and so deciding on this is making a decision that might make life harder for external complex-numeric libraries out there.

In particular, while a+bi (orthogonal) representation is efficient for addition, r*(iθ).exp() is more efficient for multiplication, and depending on the equation you're using, it may be advantageous to switch between the two to reduce the number of arithmetic operations needed.

I'm not super compelled by the argument that C supports this, therefore the standard library needs to support this. I think that guaranteeing a std::ffi::Complex representation would be desirable, but there's no saying that we need to make this a canonical type in, say, std::num.

@tgross35
Copy link
Contributor

tgross35 commented Dec 2, 2025

It's worth pointing out another big issue with this is that the canonical a+bi is not actually the best representation of complex numbers in all cases, and so deciding on this is making a decision that might make life harder for external complex-numeric libraries out there.

In particular, while a+bi (orthogonal) representation is efficient for addition, r*(iθ).exp() is more efficient for multiplication, and depending on the equation you're using, it may be advantageous to switch between the two to reduce the number of arithmetic operations needed.

I think that polar form almost always is the more optimal form, at least in my experience. But the ABIs do use rectangular, e.g. from x86:

Arguments of complex T where T is one of the types float or double are treated as if they are implemented as:

struct complexT {
  T real;
  T imag;
};

so it makes sense that an interchange type matches that, and users can translate to/from a polar repr at the FFI boundary if needed. But this reasoning is definitely something to have in the RFC's rationale & alternatives.

@clarfonthey
Copy link

clarfonthey commented Dec 2, 2025

Right: I guess my main clarification here was that due to the polar-orthogonal discrepancy, it shouldn't be a canonical Rust type (e.g. std::num::Complex shouldn't be making a decision on which is more-canonical), but I do think that having extra FFI-compatibility types is reasonable and this shouldn't prevent us from adding std::ffi::Complex which is orthogonal.

@scimind2460
Copy link
Author

scimind2460 commented Dec 3, 2025

Thanks everyone for the feedback! I have incorporated as much as I can into the RFC.
@clarfonthey I do think that the orthogonal representation is more "canonical", especially as it is the most commonly used one in crates.io and across languages. So I'm not sure if we can consider this an issue, especially as there are polar conversion methods in the RFC.

scimind2460 and others added 4 commits January 7, 2026 16:18
Co-authored-by: Trevor Gross <tg@trevorgross.com>
Co-authored-by: Trevor Gross <tg@trevorgross.com>
Removed redundant statement about overhead from libgcc calls and clarified the purpose of the Complex type.
@scimind2460
Copy link
Author

Hopefully I've cleared up everything to satisfaction. I suppose it's broadly reasonable to FCP?

@joshtriplett
Copy link
Member

The current state of the RFC looks good, thank you!

This continues to be a lang/libs-api RFC, lang because of the FFI interoperability with C _Complex, and libs-api for everything else about the type.

@rfcbot merge libs-api,lang

@rust-rfcbot
Copy link
Collaborator

rust-rfcbot commented Jan 13, 2026

Team member @joshtriplett has proposed to merge this. The next step is review by the rest of the tagged team members:

No concerns currently listed.

Once a majority of reviewers approve (and at most 2 approvals are outstanding), this will enter its final comment period. If you spot a major issue that hasn't been raised at any point in this process, please speak up!

cc @rust-lang/lang-advisors: FCP proposed for lang, please feel free to register concerns.
See this document for info about what commands tagged team members can give me.

@rust-rfcbot rust-rfcbot added proposed-final-comment-period Currently awaiting signoff of all team members in order to enter the final comment period. disposition-merge This RFC is in PFCP or FCP with a disposition to merge it. labels Jan 13, 2026
@joshtriplett joshtriplett added I-lang-nominated Indicates that an issue has been nominated for prioritizing at the next lang team meeting. and removed I-libs-api-nominated Indicates that an issue has been nominated for prioritizing at the next libs-api team meeting. labels Jan 13, 2026
@traviscross traviscross added the P-lang-drag-2 Lang team prioritization drag level 2. label Jan 14, 2026
Co-authored-by: kennytm <kennytm@gmail.com>
Clarify the rationale behind complex number implementation alternatives.
Expanded on the limitations of polar complex numbers and the need for a standard type for FFI with C. Discussed alternatives to complex number representation and their implications.
Added a section discussing alternatives to complex number implementations, including polar layout and non-generic primitive types, while emphasizing the importance of FFI compatibility.
Add considerations for complex number features and compatibility.
@scimind2460
Copy link
Author

Commenting on #793 as prior art (it wishes to achieve the same objective, but is largely dead)

@scottmcm
Copy link
Member

Having something to match C99 _Complex seems entirely lang-reasonable to me. I'm happy to see this not proposing a new repr; if things outside core want something with that layout/ABI they can repr(transparent) the canonical one.

@rfcbot reviewed

(I didn't pay much attention to the API. I trust libs-api will handle that appropriately.)

@cuviper
Copy link
Member

cuviper commented Feb 1, 2026

if things outside core want something with that layout/ABI they can repr(transparent) the canonical one.

I'm ok with this, but I do want to note that this is not seamless. For example, num_complex::Complex would not be able to keep its pub fields, only to pub expose that it's wrapping the core type that has pub fields itself.

(I probably won't bother with that at all -- just recommending the core type directly if you want C-ABI compatibility.)

@comex
Copy link

comex commented Feb 1, 2026

Will there be impls for fN: Mul<Complex<fN>>, fN: Add<Complex<fN>>, and the reverses of those? That would make it possible to write e.g. 1.0 + 2.0 * f32::i(). Seems like it should be either part of the RFC or a future possibility, or explicitly rejected.

Clarify the potential benefits of simplifying Complex number creation and discuss future support for Imaginary and Gaussian integers.
Clarify suggestions for complex number operations and future support.
@scimind2460
Copy link
Author

scimind2460 commented Feb 1, 2026

Will there be impls for fN: Mul<Complex<fN>>, fN: Add<Complex<fN>>, and the reverses of those? That would make it possible to write e.g. 1.0 + 2.0 * f32::i(). Seems like it should be either part of the RFC or a future possibility, or explicitly rejected.

It was there in an earlier draft, but I decided to relegate it to a future possibility. However, you can see that a related possibility such as the fn i(self) was discussed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

disposition-merge This RFC is in PFCP or FCP with a disposition to merge it. I-lang-nominated Indicates that an issue has been nominated for prioritizing at the next lang team meeting. P-lang-drag-2 Lang team prioritization drag level 2. proposed-final-comment-period Currently awaiting signoff of all team members in order to enter the final comment period. T-lang Relevant to the language team, which will review and decide on the RFC. T-libs-api Relevant to the library API team, which will review and decide on the RFC.

Projects

None yet

Development

Successfully merging this pull request may close these issues.