Author Topic: Complex numbers  (Read 773 times)

shiv

  • Newbie
  • *
  • Posts: 6
    • View Profile
Complex numbers
« on: March 06, 2017, 05:41:57 AM »
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

  • Administrator
  • Newbie
  • *****
  • Posts: 11
    • View Profile
Re: Complex numbers
« Reply #1 on: March 06, 2017, 08:02:26 AM »
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

  • Newbie
  • *
  • Posts: 6
    • View Profile
Re: Complex numbers
« Reply #2 on: March 07, 2017, 12:03:09 AM »
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--