C Programming/C Reference/complex.h/carg
CARG is basically a standard library function(complex function) which calculates the argument of complex number. It passes a numeric or logical argument by value. It returns phase angle of complex number(a+bi) in polar form within the range [π,-π].
This function passes a numeric or logical argument by value instead of using the Fortran standard of passing arguments by reference. When the argument is of type, character at that time the CARG function will convert the argument to a C string. It can only be used as an actual argument when invoking a subroutine or function. A complex number can be represented as:
- (in rectangular co-ordinates)
where 'A' is real part and 'B' is an imaginary part.
- (in polar co-ordinates)
where r = cabs(z) is the radius and a = carg(z) is the phase angle. Hence this function returns phase angle of complex number Z when it is used in polar co-ordinates the return value is ranges from [π, -π] and when we use this function, we required to use <complex.h> header file.
In general, this function calculates argument of a complex number.
double carg (double complex z);
float cargf (float complex z);
long double cargl (long double complex z);
Syntax of this carg function is as follow:
In this, 'item' may be of any intrinsic type except complex & 4 byte logical.It is a data object for which to return a value.
When the argument is numerical or logical, at that time value of item is placed on calling stack, rather than its address. If argument is of the type character, the fortran length descriptor is removed and the character string is null terminated. The C data type of the result is shown in following table:
|fortran type||fortran kind||c type|
|Integer||2||signed short int|
|Integer||4||signed long int|
|Complex||4||should not be passed by value; if passed by reference (without CARG) then it is pointer to a strcture|
|logical||4||must not be passed by value or by reference|
/*conversion of a real number from its Cartesian to polar form*/
double complex z = -4.4 + 3.3 * I;
double radius = cabs(z);
double argument = carg(z);
double x = creal(z);
double y = cimag(z);
This example compute and print argument of (1+i)
USE CARG_INT USE UMACH_INT IMPLICIT NONE ! (declaration of variables) INTEGER NOUT REAL VALUE COMPLEX Z ! (compute) Z = (1.0,1.0) VALUE=CARG(Z) ! (print the result) CALL UMACH(2,NOUT)
CARG (1.00,1.00) = 0.785