Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement complex numbers #1284

Closed
boggle opened this issue Dec 10, 2011 · 10 comments
Closed

Implement complex numbers #1284

boggle opened this issue Dec 10, 2011 · 10 comments
Labels
C-enhancement Category: An issue proposing an enhancement or a PR with one.

Comments

@boggle
Copy link
Contributor

boggle commented Dec 10, 2011

Complex numbers in C are implemented by passing real and imaginary part as two consecutive, unpadded floating point values via the stack (i.e. as length-2 array). This is similar for returned complex values - which is the catch, as tuples in rust are defined to have the same alignment behavior as records (cf spec). This means a (f32, f32) complex might suffer padding and is thus not suitable for implementing complex values. I see different ways to deal with this and would like comments on which is best

  1. Implement native complex types (complex, c32, c64) in rust. Shouldn't be too hard but is a substantial language change,
  2. Define tuple components to be stored consecutively (i.e. without padding/alignment),
  3. Solve via annotation when declaration function prototypes in native mods,
  4. Natively support c_vec (stack and heap allocated), or
  5. Implement tuple-based wrappers in the runtime.

I'm tending to favor (1) though (3) and (4) might be desirable to have for other reasons.

@jckarter
Copy link

I don't know what Rust's abi rules are, but for every platform I know of, f32 is 32-bit aligned in C, so padding shouldn't be an issue. You may still need to have complex types be handled natively in order to deal with ABI differences between complex and structs—for example, on x86-64, _Complex long double return values are returned on the x87 stack, where as a struct of long doubles would be returned in memory. I think there's at least one x86-32 platform that also returns _Complex float and double values using a special rule (though I forget which). Perhaps you could handle the different ABI with a type attribute, though.

@leto
Copy link

leto commented Dec 10, 2011

One issue to think about is how Complex NaN will be handled. Will there be a real NaN and a separate Complex NaN? Or will you go the highlander "there can be only one NaN" route?

@graydon
Copy link
Contributor

graydon commented Dec 12, 2011

I'd wait on this until we see what happens on the typeclass work. Things like complex are prime candidates for overloaded arithmetic ops.

@kud1ing
Copy link

kud1ing commented Dec 13, 2011

See #1227

@kud1ing
Copy link

kud1ing commented Dec 22, 2011

Once we have interfaces/type classes, we should have a look at Haskell's hierarchy and do some cherrypicking.
For example Haskell's "Complex" implements the type class "RealFloat" http://www.haskell.org/onlinereport/basic.html#standard-classes

@boggle
Copy link
Contributor Author

boggle commented Dec 22, 2011

Yes, sure. Although I'd like to keep things a bit more simple than in Haskell. For now, typeclasses for integral and float types should suffice. Perhaps the float operations need to be split between a "full set" and those supported by floats and complex numbers alike. I'm really looking at libmath in that regard. Anyways to implement complex numbers, we may need ABI support for C's Complex type (implementing it ourselves is not a good option as it would hurt compatibility for bindings to C libraries).

@graydon
Copy link
Contributor

graydon commented Feb 15, 2012

I think we have almost enough machinery in the operator overloading space to implement this in the libraries now, using tuples and syntax extensions. Retagging as lib-related.

@boggle
Copy link
Contributor Author

boggle commented Apr 7, 2012

I am still opposed to doing that in the libraries. If we do not support the C complex type that will cause overhead when calling the lib math functions for complex numbers and in integration with numerical C code. I'd actually rather see rust have primitive complex types (c64, c128, similar to "go") that directly map on their C counterparts. This solution would be in line with what we do for floats and should make it easier to handle the ABI requirements of passing complex numbers to C functions.

@catamorphism
Copy link
Contributor

Tagging RFC since there's a question of whether to do this in libraries or in the RTS.

bors added a commit that referenced this issue Apr 6, 2013
This adds two generic data types, `Ratio` and `Cmplx` (and some aliases for useful instances, e.g. `Ratio<int>` and `Cmplx<f64>`), and basic arithmetic support, as well as `.to_str` (for both) and `.from_str` (for rational).

The complex number implementation doesn't solve #1284 other than getting something into the libraries, specifically it doesn't even try to address C interop. If the complex part of this gets merged, maybe it's worth closing that issue and reopening more specific issue(s) about the failings.

The implementations can be fleshed out when the numeric traits stabilise (and trait inheritance works).
@thestinger
Copy link
Contributor

This has been implemented.

bjorn3 added a commit to bjorn3/rust that referenced this issue Oct 23, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-enhancement Category: An issue proposing an enhancement or a PR with one.
Projects
None yet
Development

No branches or pull requests

7 participants