0

I'm trying to use cpow with r and I get a syntax error telling me to put a semi colon after r. After reading the docs I'm still confused on what I'm doing wrong?

#include <math.h>
#include <complex.h>

float area(double r)
{
    r = (1 - r / 1.83) * 1.25;
    double complex z = cpow(r, .2); // i get an error here on the letter r saying missing colon (;)
    return r;

}




    int main()
    {
        float a,r;

        double radius, sum = 0;
        char continu[20];

        // the body of the loop is executed at least once
        do
        {
            float a,r;

            printf("Enter a number: ");
            printf("enter radius of the circle: ");
            scanf("%lf",&r);
            a=profile(r);
            printf("CCOC: %f\n",a);

        }
        while(continu = 'y');


        return 0;
    }

user3629249
  • 16,402
  • 1
  • 16
  • 17
  • What result do you expect to get with `cpow` that you would not get from `pow`? – Eric Postpischil Apr 15 '21 at 19:41
  • pow gives error as it doesnt work well with negetive bases that have non integer eponents. – Theodore Vassilakoglou Apr 16 '21 at 01:58
  • If you are trying to take the fifth root of a possibly negative number, `cpow` will not give you the result you want, due to the branch cut it is specified to use. It will give you a complex (non-real) result. Use `copysign(pow(fabs(r), .2), r)` instead. – Eric Postpischil Apr 16 '21 at 02:04

2 Answers2

5

The variable name r in double complex r collides with the argument name r in double r. Give one of them another name.

MikeCAT
  • 73,922
  • 11
  • 45
  • 70
1

On topo of conflicting r variable name and returnign wrong type the header for cpow is like this:

double complex cpow( double complex x, double complex y );

What you are calling it with is:

double complex z = cpow(double r,double .2);

Complex numbers have 2 components real and imaginary. As your operands are real domain you need to cast them to complex domain by copying them to real part and set imaginary part to zero.

I never used complex.h usually use my own code for stuff like this so I do not know how exactly cast this. According to this I would expect this would work:

double area(double r)
    {
    double complex cr,ce,cz;
    r = (1.0-(r/1.83))*1.25;
    cr=r+0.0*I;
    ce=0.2+0.0*I;
    cz=cpow(cr,cc);
    return  creal(cz);
    }

However as its most likely implemented like a macro its possible you need to write double complex before each operand of cpow ... Also the I I saw to be use as i too in the same linked doc... its really confusing which one or if booth or if they are required or not.

However using cpow from complex.h will not resolve your problem as it most certainly does not compute the correct k for cln and uses most likely just k=0 which will lead to non real results !!! If you use the last version of mine mypow in the linked answer you do not need complex.h nor anything else but math.h ...

Also only now its clear you are computing area. So why is r even negative? If you have some parts to add and some to substract from the total area then its OK however if not then maybe you just have wrong equation and after its correction you could use real domain pow ... such case is called XY problem ... you want to solve problem X by solving problem Y as you think it would lead you closer to solution however if X and Y are not related you just wasting time.

Spektre
  • 49,595
  • 11
  • 110
  • 380