-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Complex numbers #16278
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
Comments
why bikeshed? I am not native speaker but it seems strange to ask for bikeshed:
https://en.m.wiktionary.org/wiki/bikeshedding Edit: Is my comment real bikeshedding? |
Its a joke about that syntax discussions are often long with many drive-by comments |
I currently don't expect to use this feature, so not much weight behind my words, but anyway: My only strong opinions reading this:
|
My take on a few things:
|
For complex number literals / initializing complex numbers: My suggestions would be to have The tuple syntax and/or The existence of a |
Another reason why I'm clearly not in favor of |
Thanks for the work on this. This looks quite nice. Some reflections: *) Regarding construction: *) Regarding The
If we eventually choose to go with construction using (The tuple-based construction *) Coercion rule: *) Regarding relational operators: Mathematically, complex numbers are NOT an ordered field. Related to this, the *) Regarding support for complex numbers:
|
Quick note that we have precedence in slices |
Thanks all for your comments
Ok, we should leave these away citing unclear mathematical definition.
Yep, that was the intention. In hindsight it seems obvious that these things interfere. I've adapted the proposal to highlight the intended semantics.
Out of scope of this proposal. We are specifically interested in C99-compatible complex numbers. I think for anything that's more complicated, like mixed precision or SoA, the user should create a custom struct and convert that to a built-in complex number before processing (or write out the operation manually all together).
That is a nice addition that is also a relatively minor change and fits well with the other constants that are already defined in
Yes, I'm aware. I think it would be unexpected for the user if the main way to write sqrt in Zig does not work with complex numbers though, so implementation here feels appropriate. I think anything like hypotSqr should be implemented in library code, though.
That's a good argument. |
Is there any reason why quaternions are out of scope, since I do feel like if you're going to have logic for one type of extended number system like that then generalizing it so it would work for others like quaternions might be useful. Though I mostly just say that because quaternions are used a good bit in games and would be nice to have native support for like other kinds of vector math, I don't think anyone uses stuff like octonions for anything. |
Yes, the reason is that we are currently mainly focussing on C99 compatibility and sneaking in native complex operations as an added bonus. It does not rule out quaternions in Zig in general, just that they are not part of this proposal. |
Small comment: I noticed that issue #16026, is now accepted for release 0.12, so perhaps our above use of |
A builtin type function should return a type, not a typed value. I feel that |
|
I have made a proof of concept here #19218 |
I’d like to propose a new language construct "builtin methods" for primitive types, which kills many birds with this one stone, including:
Rationale explained in the issue referenced above, quoted below:
Sample of what it might look like: var c = @Complex(f32){0,0};
c.@re += 7;
c.@im += 8;
var cs = @Vector(@Complex(f32),3){
.{1,2},
.{3,4},
.{5,6},
};
var ds = cs.@scale(c);
cs *= ds;
const es = cs.@swiz(1,1,2); |
@expikr Is there some rationale for why complex number fields |
Hmm, true. I forgot about slice. In that case then I agree it's nicer to drop the |
Semantically |
As a long time lurker, I just wanted to add that a typical operation on complex numbers is conjugation that is |
This is a concrete proposal for adding complex numbers to Zig. The basic idea has already been greenlit informally, so this issue is here to describe the concrete roadmap that is required for implementing these.
The main idea is to add complex numbers as a first-class citizen instead of as library types. There are 2 main reasons for this:
This proposal seeks to deal with both of the above, though the primary argument for having built-in complex number is the former.
Scope
This proposal seeks to implement the following things:
_Complex float
,_Complex double
, and_Complex long double
. We can extend the C family with to the usual Zig types; complex versions off16
,f32
,f64
,f80
,f128
,comptime_float
.[2]T
, whereT
is the backing float type. Layout is real first, then imaginary.std.math
where required. In time,std.math.Complex
would be deprecated, and uses are replaced by the new type.std.math.Complex
. Note that the implementations here in particular suffer from ABI issues: the complex type is here passed as a struct but as stated before, this is not always correct.Out of scope
_Imaginary
(see C99 spec) type specifier. This is an optional component of C99, and is in practice not really used [citation needed]. Besides, these types have the same ABI requirements as their backing type, and so these could be dealt with in the C translator without requiring additional changes in Zig.Language Changes
In order to support complex types, the following changes would need to be made:
builtin.Type
.@Complex(T)
, that creates a complex number type where both fields are backed byT
.@real
and@imag
(similar tocreal
andcimag
. This would also support the following syntax:@I()
, that returns0+1i
. This could be used to create more complex complex types, for example1 + 1 * @I()
. It looks a bit weird imo.im
:1+1im
would return the equivalent complex value. Since Zig does not have any other numeric suffixes, I think this option is not the best.@complex(1, 1)
. The result would have the same type as the peer type of the arguments. For comptime values, this would be@Complex(comptime_float)
.var z: @Complex(f32) = .{ 1, 1 };
.Complex(T) -> Complex(U)
. This has the same rules asT -> U
.T -> Complex(T)
. This would initialize a complex value with the valuex + 0i
. The main motivation for this feature is to simplify syntax when dealing with real and complex values, and allow the user to writex + z
. These operations are well-defined in mathematics.@TypeOf(@Complex(T), U)
would yield@Complex(@TypeOf(T, U))
.+
,-
,*
,/
.Is%
necessary? C does not define this one.==
,!=
.-
.Relational operators,<=
,<
,>
,>=
?Relational operators are not supported in C. This usually compares the modulus, and would require the implementation ofabs
to be supported in the compiler. It can be implemented by comparing the hypot without computing the square root.@mulAdd
.@floatCast
..@max
,@min
@sqrt
.@sin
,@cos
,@tan
.@exp
,@exp2
.@log
,@log2
,@log10
.@fabs
. This would implement the hypot. GingerBill shared some notes on this:expand
Open Questions
%
?Related Issues
Please bikeshed on syntax below
The text was updated successfully, but these errors were encountered: