-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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
Comments
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, |
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? |
I'd wait on this until we see what happens on the typeclass work. Things like complex are prime candidates for overloaded arithmetic ops. |
See #1227 |
Once we have interfaces/type classes, we should have a look at Haskell's hierarchy and do some cherrypicking. |
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). |
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. |
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. |
Tagging RFC since there's a question of whether to do this in libraries or in the RTS. |
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).
This has been implemented. |
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
I'm tending to favor (1) though (3) and (4) might be desirable to have for other reasons.
The text was updated successfully, but these errors were encountered: