NO

Author Topic: complex cpow()  (Read 2322 times)

Offline TimoVJL

  • Global Moderator
  • Member
  • *****
  • Posts: 2115
complex cpow()
« on: September 21, 2019, 02:42:34 PM »
It's time to check complex functions, like cpow():
Code: [Select]
#include <stdio.h>
#include <complex.h>
// https://en.cppreference.com/w/c/numeric/complex/cpow
int main(void)
{   
    double complex z = cpow(1.0+2.0*I, 2);
    printf("(1+2i)^2 = %.1f%+.1fi\n", creal(z), cimag(z));
 
    double complex z2 = cpow(-1, 0.5);
    printf("(-1+0i)^0.5 = %.1f%+.1fi\n", creal(z2), cimag(z2));
 
    double complex z3 = cpow(conj(-1), 0.5); // other side of the cut
    printf("(-1-0i)^0.5 = %.1f%+.1fi\n", creal(z3), cimag(z3));
 
    double complex z4 = cpow(I, I); // i^i = exp(-pi/2)
    printf("i^i = %f%+fi\n", creal(z4), cimag(z4));
}
expected result:
Code: [Select]
(1+2i)^2 = -3.0+4.0i
(-1+0i)^0.5 = 0.0+1.0i
(-1-0i)^0.5 = 0.0-1.0i
i^i = 0.207880+0.000000i
Code: [Select]
#include <stdio.h>
#include <complex.h>

int main(void)
{
double complex base = -1.23;
for (double f = 1.7; f < 2.31; f += 0.1)
{
double complex exponent = f;
double complex result = cpow(base, exponent);
printf("-1.23^%.2f = %f %fi\n", f, creal(result), cimag(result));
}
return 0;
}
May the source be with you

Offline Pelle

  • Administrator
  • Member
  • *****
  • Posts: 2266
    • http://www.smorgasbordet.com
Re: complex cpow()
« Reply #1 on: September 22, 2019, 07:52:20 PM »
It's time to check complex functions, like cpow():
Not really.

With C2X coming up in a few years, I much rather raise the minimum standard from C99, where complex is mandatory, to C11 where complex is optional - and then remove the support for complex. Few people need it, and it's extra work in the compiler (will often affect new optimizations and warnings, f.e.)
/Pelle