General Category > General Discussion

Complex numbers

(1/2) > >>

shiv:
What would it take to support complex numbers in c2? I'm assuming c2 does not automatically overload the arithmetic operators so wrappers will have to be written? Thanks. --shiv--

admin:
I haven't really thought about that yet. I almost never (read: never) write code with Complex numbers and such.
Are there specific painpoints using them in C?

shiv:
In the standard (assuming we include complex.h) only "float/double complex" are allowed. GNU also allows "int complex".
All the arithmetic operators *,+,- are overloaded and ~ means complex conjugation.
If we include tgmath.h then we also get "generic" math functions like sin(), cos(), etc. So, it is actually quite nice!

I wonder if in c2 you should follow Rust's example and have separate modules named float32, float64, complex32, complex64, etc.,
and hide the functions inside each module. That way we could avoid names like sin, sinf, sinc, sinz, etc..

Not sure how to call C macros from C2; otherwise there might be another route though that might not work if you target llvm-ir (?) directly.

--shiv--

lerno:
Simple operations on complex numbers, quaternions and matrices is pretty much all that a sane programmer uses to operator overloading for.

Support for this out of the box would be very nice, but it's important to understand that people will want to write their own optimized libraries to plug in into the system.

bas:
Since C2 is a descendant of C and has no symbol-mangling, function overloading (multiple functions with same name bit with different arguments)
is not supported. I recently looked at Zig (ziglang.org) a bit and they allow putting functions on many constructs, like types, enums etc. This could
also be implemented in C2..


--- Code: ---type Color enum u8 { Red, Green, Blue }

func bool Color.isRed(const Color* color) {
  return *color == Red
}

--- End code ---

Navigation

[0] Message Index

[#] Next page

Go to full version