Author Topic: Complex numbers  (Read 1379 times)

shiv

  • Newbie
  • *
  • Posts: 9
    • 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: 9
    • 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--

lerno

  • Newbie
  • *
  • Posts: 19
    • View Profile
Re: Complex numbers
« Reply #3 on: June 23, 2018, 11:49:34 PM »
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

  • Full Member
  • ***
  • Posts: 106
    • View Profile
Re: Complex numbers
« Reply #4 on: June 25, 2018, 09:00:29 AM »
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: [Select]
type Color enum u8 { Red, Green, Blue }

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

lerno

  • Newbie
  • *
  • Posts: 19
    • View Profile
Re: Complex numbers
« Reply #5 on: June 25, 2018, 11:59:39 AM »
I don't know if there is a simple solution, but consider the difference between overloading and normal function calling:

Code: [Select]
// Operator overloading
result1 = matrix1 + (matrix2 - matrix3) / factor;

// Straight functions
result2 = matrix_add(matrix1, matrix_division(matrix_sub(matrix2, matrix3), factor);

Personally I dislike function overloading, but this is a special case.

Alternatively, allow free postfix function calls:

Code: [Select]
result2 = matrix1 matrix_add ((matrix2 matrix_sub matrix3) matrix_division factor);

There is one other solution though, in three steps:
  • Add built-in types "matrix", "complex", "vector", "quaternion"
  • Create default implementations of each as "matrix_add", "matrix_mult", "complex_add" etc
  • Support monkey patching functions – basically allow overwriting the default implementations with a new function replacement within a module.