C Programming/C Reference/complex.h/carg

From Wikibooks, open books for an open world
Jump to: navigation, search

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 [π,-π].

Description[edit]

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:

  • Z = A + Bi (in rectangular co-ordinates)

where 'A' is real part and 'B' is an imaginary part.

  • z = re^{ia} (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.

Synopsis[edit]

#include<complex.h>
double carg (double complex z);
float cargf (float complex z);
long double cargl (long double complex z);[1]

SYNTAX

Syntax of this carg function is as follow:

SYNTAX
carg (item)

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.

Result[edit]

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 1 signed char
Integer 2 signed short int
Integer 4 signed long int
Real 4 float
Complex 4 should not be passed by value; if passed by reference (without CARG) then it is pointer to a strcture
logical 1 unsigned char
logical 4 must not be passed by value or by reference
char 1 char*


Example 1[edit]

 /*conversion of a real number from its Cartesian to polar form*/

#include<stdio.h>
#include<complex.h>
int main(){
double complex z = -4.4 + 3.3 * I;
double radius = cabs(z);
double argument = carg(z);

double x = creal(z);
double y = cimag(z);

printf("cartesian(x,y):(%4.1f,%4.1f)\n",x,y); printf("polar(r,theta):(%4.1f,%4.1f)\n",radius,argument);
return 0;
}


Output[edit]

Cartesian(x,y):(-4.4,3.3)
polar(r,theta):(5.5,2.5)[2]

Example 2[edit]

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)

Output[edit]

CARG (1.00,1.00) = 0.785

References[edit]