RAD Studio
ContentsIndex
PreviousUpNext
cabs, cabsl

Header File 

math.h  

Category 

Math Routines 

Prototype 

double cabs(struct complex z); 

long double cabsl(struct _complexl z); 

Description 

cabs calculates the absolute value of a complex number. cabs is a macro that calculates the absolute value of z, a complex number. z is a structure with type complex; the structure is defined in math.h as 

struct complex { 

double x, y; 

}; 

where x is the real part, and y is the imaginary part. 

Calling cabs is equivalent to calling sqrt with the real and imaginary components of z, as shown here: 

sqrt(z.x * z.x + z.y * z.y) 

cabsl is the long double version; it takes a structure with type _complexl as an argument, and returns a long double result. The structure is defined in math.h as 

struct _complexl { 

long double x, y; 

}; 

Return Value 

cabs (or cabsl) returns the absolute value of z, a double. On overflow, cabs (or cabsl) returns HUGE_VAL (or _LHUGE_VAL) and sets the global variable errno to

ERANGE 
Result out of range 

Error handling for these functions can be modified through the functions _matherr and _matherrl. 

Example  

#include <stdio.h>
#include <math.h>
#ifdef __cplusplus
  #include <complex.h>
#endif
#ifdef __cplusplus /* if C++, use class complex */
  void print_abs(void)
  {
    complex<float> z(1.0, 2.0);
    double  absval;
    absval = abs(z);
    printf("The absolute value of %.2lfi %.2lfj is %.2lf",
      real(z), imag(z), absval);
  }
#else  /* below function is for C (and not C++) */
  void print_abs(void)
  {
    struct complex z;
    double absval;
    z.x = 2.0;
    z.y = 1.0;
    absval = cabs(z);
    printf("The absolute value of %.2lfi %.2lfj is %.2lf",
      z.x, z.y, absval);
  }
#endif
int main(void)
{
  print_abs();
  return 0;
}

Portability

 
POSIX 
Win32 
ANSI C 
ANSI C++ 
cabs 
 
 
 
cabsl 
 
 
 
Copyright(C) 2008 CodeGear(TM). All Rights Reserved.
What do you think about this topic? Send feedback!