Hacker News new | ask | show | jobs
by ilayn 695 days ago
I hope Zig adds complex numbers before it is too late. It's a pity that they did not steal array syntax and slices from Fortran/Numpy.
4 comments

Why would complex numbers in particular deserve to be a native type?

What about quaternions, or vectors and matrices of sizes 2-4 at least?

Is there any real advantage over just using a struct?

Would MultiArrayList help with performance for complex numbers and vectors?

Zig doesn't (and never will) have operator overloading, so using any user-defined numeric types is awkward. It also already has vector types.
This is IMO the canonical use case of operator overloads. Are you proposing adding complex numbers to the standard lib to circumvent that restriction, vice addressing it?
Why should complex numbers need justification to be native type? If you need to do serious computation, you need them. If you say Zig is not for number crunching which, say, Java explicitly decided then fine otherwise searching for a reason for complex number tells me that you don't do any numerical programming which is fine but asking for justification makes no sense. Why is any other thing in the language justified? float64 is just two float32. Why stop at float64 why not float128 float256?
What do you mean with ” float64 is just two float32.”?
I don’t think it’s more complicated than people want an abstraction for the complex number instructions you find on processors like the TMS320 series or Qualcomm Hexagon.
Odin makes them (and more!) absolutely lovely to work with:

complex32/64/128, quaternion64/128/256

https://odin-lang.org/docs/overview/#array-programming

https://odin-lang.org/docs/overview/#matrix-type

Probably helps it's the language JangaFX uses for their products used by essentially every major video game, movie, and TV show out there (https://jangafx.com/#headline-603-384)

True, but column-major by default and 1-indexed unfortunately. Still quite nice that they are addressing these challenges head-on early stage unlike other new langs. I guess they had their share the pain like the rest of us.
I'm curious, what use cases do you have for complex numbers that couldn't be provided in a 3rd party library or maybe even the stdlib?
You are essentially necromancing the ISO C99 discussion that happened already. Here is an answer that, after 25 years, is still wrong https://learn.microsoft.com/en-us/cpp/c-runtime-library/comp...
That doesn't tell me much about the usage scenario of complex numbers that would justify adding native support in programming languages. As a sibling comment said, builtin vector and matrix types (up to 4 dimensions) would probably be more useful.
I'll paste the same comment here as I did in the sibling; Why should complex numbers need justification? If you need to do serious computation, you need them. If you say Zig is not for number crunching which Java explicitly decided then fine otherwise searching for a reason for complex number tells me that you don't do any numerical programming which is fine but asking for justification makes no sense. Why is any other thing in the language justified?
This is simple to add; struct with two fields, and a handful of methods + operator overloads.
Why did not I think of that, shame on me.