cabs, cabsl
Calculates the absolute value of complex number.
Defined in header <math.h>
Syntax
#include <math.h>
double cabs(struct complex z);
long double cabsKstruct complexl z);
Portability
| DOS | UNIX | Windows | ANSI C | C++ only | |
|---|---|---|---|---|---|
| cabs | ■ | ■ | ■ | ■ | |
| cabsl | ■ | ■ |
Remarks
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 a structure with type joomplexl as an argument, and returns a long double result. The structure is defined in math.h as
struct _complexl { long double x, y; }; If you are using C++, use the complex type defined in complex.h, and the function abs.
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.
See also
Example
#include <stdio.h>
#include <math.h>
lifdef cplusplus
#include <complex.h>
lendif
#ifdef cplusplus /* if C++, use class complex */
void print_abs (void)
{
complex z (1.0, 2.0);
double absval;
absval = abs (z);
printfC'The absolute value of %.21fi %.21f] is %.21f",
real(z), imag(z), absval);
}
telse /* Function below 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);
printfC'The absolute value of %.21fi l.21fj is %.21f",
z.x, z.y, absval);
}
#endif
int main (void)
{
print_abs ();
return 0;
Differences from modern implementations
Nothing recorded yet.
Modern references
These are search links, not yet checked by hand.
Stable link: /3.1/math.h/cabs/
· short form /3.1/cabs/