========
MATH.H
========
Functions
=========
abs ...
acos, acosl
asin, asinl
atan, atanl
atan2, atan2l
atof, _atold
cabs, cabsl
ceil, ceill
cos, cosl
cosh, coshl
exp, expl
fabs, fabsl
floor, floorl
fmod, fmodl
frexp, frexpl
hypot, hypotl
labs ...
ldexp, ldexpl
log, logl
log10, log101
matherr, _matherrl
modf, modfl
poly, polyl
pow, powl
pow10, pow10l
sin, sinl
sinh, sinhl
sqrt, sqrtl
tan, tanl
tanh, tanhl
Constants, data types, and global variables
===========================================
complex (struct)
_complexl (struct)
EDOM
ERANGE
exception (struct)
_exceptionl (struct)
HUGE_VAL
M_E
M_LOG2E
M_LOG10E
M_LN2
M_LN10
M_PI
M_PI_2
M_PI_4
M_1_PI
M_2_PI
M_1_SQRTPI
M_2_SQRTPI
M_SQRT2
M_SQRT_2
_mexcep
See Also
========
List of all Header files
===========
COMPLEX.H
===========
Declares the C++ complex math functions.
All function names, member names, and
operators have been borrowed from AT&T C++,
except for the addition of acos, asin, atan,
log10, tan, and tanh.
Includes
=======
IOSTREAM.H
MATH.H
Functions
=========
abs
acos
arg
asin
atan
conj
cos
cosh
exp
imag
log
log10
norm
polar
pow
pow10
real
sin
sinh
sqrt
tan
tanh
Constants, data types, and global variables
===========================================
_COMPLEX_H
__cplusplus
Overloaded operators
====================
+ += - -=
* *= / /=
== != << >>
See Also
========
List of all Header files
=========
FLOAT.H
=========
Functions
=========
_clear87
_fpreset
_control87
_status87
Constants, data types, and global variables
===========================================
CW_DEFAULT
FPE_EXPLICITGEN
FPE_INEXACT
FPE_INTDIV0
FPE_INTOVFLOW
FPE_INVALID
FPE_OVERFLOW
FPE_UNDERFLOW
FPE_ZERODIVIDE
ILL_EXECUTION
ILL_EXPLICITGEN
SEGV_BOUND
SEGV_EXPLICITGEN
See Also
========
List of all Header files
=======
BCD.H
=======
Declares the C++ class bcd, plus the
overloaded operators for class bcd and
for bcd math functions.
Functions
=========
abs
acos
asin
atan
cos
cosh
exp
log
log10
pow
pow10
real
sin
sinh
sqrt
tan
tanh
Constants, data types, and global variables
===========================================
_BCD_H
_BcdMaxDecimals
bcdexpo (enum)
__cplusplus
Overloaded operators
====================
!= += +
-= - *=
* == <
<= > >=
/= /
See Also
========
List of all Header files
M_E, M_LOGxxx, M_LNxx <MATH.H>
=======================
The constant values for logarithm functions.
Name | Meaning
----------+------------------------
M_E | The value of e
M_LOG2E | The value of log(e)
M_LOG10E | The value of log10(e)
M_LN2 | The value of ln(2)
M_LN10 | The value of ln(10)
PI constants <MATH.H>
===============
Common constants of ã (pi)
Name | Meaning
------------+--------------------------------------------
M_PI | ã
M_PI_2 | One-half ã (ã/2)
M_PI_4 | One-fourth ã (ã/4)
M_1_PI | One divided by ã (1/ã)
M_2_PI | Two divided by ã (2/ã)
M_1_SQRTPI | One divided by square root of ã (1/ûã)
M_2_SQRTPI | Two divided by square root of ã (2/ûã)
M_SQRTxx <MATH.H>
==========
Constant values for square roots of 2.
Name | Meaning
----------+------------------------------------
M_SQRT2 | Square root of 2 (û2)
M_SQRT_2 | 1/2 the square root of 2 (û2)/2
exception and _exceptionl <MATH.H>
===========================
The format of error information for math
routines.
Struct exception is used by matherr:
struct exception {
int type;
char *name;
double arg1, arg2, retval;
};
Struct _exceptionl is used by _matherrl:
struct _exceptionl {
int type;
char *name;
long double arg1, arg2, retval;
};
Member | What It Is (Or Represents)
--------+--------------------------------------------------------------
type | The type of mathematical error that occurred; an enum type
| defined in the typedef _mexcep.
name | A pointer to a null-terminated string holding the name of
| the math library function that resulted in an error.
arg1, | The arguments (passed to the function *name) that caused
arg2 | the error. If only one argument was passed to the function,
| it is stored in arg1.
retval | The default return value for matherr; you can modify this
| value.
complex and _complexl <MATH.H>
=======================
Complex number representation.
Struct complex is used by the complex
function cabs.
struct complex {
double x, y;
};
Struct _complexl is used by the long double
complex function cabsl.
struct _complexl {
long double x, y;
};
x is the real part, and y is the imaginary
part.
_mexcep <MATH.H>
================
The typedef _mexcep enumerates these
constants that represent possible
mathematical errors.
Constant | Mathematical error | Example
----------+------------------------------------------+----------------
DOMAIN | Argument was not in domain of function | log(-1)
SING | Argument would result in a singularity | pow(0, -2)
OVERFLOW | Argument would produce a function | exp(1000)
| result > MAXDOUBLE |
UNDERFLOW| Argument would produce a function | exp(-1000)
| result < MINDOUBLE |
TLOSS | Argument would produce function result | sin(10e70)
| with total loss of significant digits |
The symbolic constants MAXDOUBLE and
MINDOUBLE are defined in VALUES.H.
See Also:
matherr
==============
abs, <MATH.H, STDLIB.H, COMPLEX.H>
cabs, cabsl, <MATH.H>
fabs, fabsl, <MATH.H>
labs <MATH.H, STDLIB.H>
==============
* abs (a macro) gets the absolute value of an integer
* cabs and cabsl (macros) calculate the absolute value of a complex number
* fabs and fabsl calculate the absolute value of a floating-point number
* labs calculates the absolute value of a long number
Declaration:
* abs
real: int abs(int x);
complex: double abs(complex x);
* double cabs(struct complex z);
* long double cabsl(struct _complexl (z));
* double fabs(double x);
* long double fabsl(long double @E(x));
* long int labs(long int x);
Remarks:
All of these routines return the absolute
value of their argument. abs, cabs, and cabsl
are macros; fabs and labs are functions.
| Returns absolute
Routine | value of...
---------+-----------------------------------
abs | x, an integer
cabs | z, a complex number
cabsl | z, a complex number
fabs | x, a double
labs | x, a long
abs
===
If abs is called when STDLIB.H has been
included, it is treated as a macro that
expands to inline code.
If you want to use the abs function instead
of the macro, include #undef abs in your
program, after the #include <STDLIB.H>.
cabs and cabsl
==============
Calling cabs or cabsl is equivalent to
calling sqrt with the real and imaginary
components of z:
sqrt(z.x * z.x + z.y * z.y)
In C++, use abs with the complex type defined
in COMPLEX.H.
Return Value:
* abs:
* (real): An integer in the range 0 to 32,767;
an argument of -32,768 is returned as -32,768
* (complex): A double
* cabs and cabsl:
* On success, the absolute value of z, a double
* On overflow, they return HUGE_VAL and set errno to
ERANGE (result out of range)
* fabs: A double
* labs: A long
Error handling for cabs and cabsl can be
modified via matherr and _matherrl.
Portability + DOS + UNIX + Windows + ANSI C + C++ Only +
Real abs | Yes | Yes | Yes | Yes | |
Complex abs | Yes | | Yes | | Yes |
cabs | Yes | Yes | Yes | Yes | |
cabsl | Yes | | Yes | | |
fabs | Yes | Yes | Yes | Yes | |
fabsl | Yes | | Yes | | |
labs | Yes | Yes | Yes | Yes | |
+-----+------+---------+--------+----------+
Examples:
abs example
cabs example
fabs example
labs example
abs example
=============
#include <stdio.h>
#include <math.h>
int main(void)
{
int number = -1234;
printf("number: %d absolute value: %d\n", number, abs(number));
return 0;
}
cabs 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 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;
}
fabs example
==============
#include <stdio.h>
#include <math.h>
int main(void)
{
float number = -1234.0;
printf("number: %f absolute value: %f\n", number, fabs(number));
return 0;
}
labs example
==============
#include <stdio.h>
#include <math.h>
int main(void)
{
long result;
long x = -12345678L;
result= labs(x);
printf("number: %ld abs value: %ld\n", x, result);
return 0;
}
=============================
acos, asin, atan, atan2, (real) <MATH.H>
acosl, asinl, atanl, atan2l (complex) <COMPLEX.H>
=============================
Arc cosine, arc sine, and arc tangent functions
Declaration:
* Real: double acos(double x);
double asin(double x);
double atan(double x);
double atan2(double y, double x);
long double acosl(long double (x));
long double asinl(long double (x));
long double atanl(long double (x));
long double atan2l(long double (y), long double (x));
* Complex: complex acos(complex z);
complex asin(complex z);
complex atan(complex x);
Remarks:
Real versions
=============
* acos and acosl of a real value compute the arc cosine of that value
* asin and asinl of a real value compute the arc sine of that value
* atan and atanl calculate the arc tangent of the input value
* atan2 and atan2l also calculate the arc tangent of the input value
Real arguments to acos, acosl, asin, and
asinl must be in the range -1 to 1.
Complex versions
================
* Complex arc cosine: acos(z) = -i log(z + i sqrt(1 - z**2))
* Complex arc sine: asin(z) = -i * log(i * z + sqrt(1 - z**2))
* Complex arc tangent: atan(z) = -0.5 i log((1 + i*z)/(1 - i*z))
Return Value:
* On success,
* acos and acosl return the arc cosine of the
input value (in the range 0 to pi)
* asin and asinl return the arc sine of the
input value (in the range -pi/2 to pi/2)
* atan and atanl return the arc tangent of the
input value (in the range -pi/2 to pi/2)
* atan2 and atan2l return the arc tangent of y/x
(in the range -pi to pi).
atan2 and atan2l produce correct results even
when the resulting angle is near pi/2 or
-pi/2 (x near 0).
* On error (if the arguments are not in the range -1 to 1),
the acos, acosl, asin, and asinl functions return NAN and
set errno to EDOM (domain error).
* If both x and y are set to 0, atan2 sets errno to EDOM.
Error handling for these routines can be
modified via matherr and _matherrl.
Portability + DOS + UNIX + Windows + ANSI C + C++ Only +
Real acos | Yes | Yes | Yes | Yes | |
Real asin | Yes | Yes | Yes | Yes | |
Real atan | Yes | Yes | Yes | Yes | |
Complex acos | Yes | | Yes | | Yes |
Complex asin | Yes | | Yes | | Yes |
Complex atan | Yes | | Yes | | Yes |
acosl | Yes | | Yes | | |
asinl | Yes | | Yes | | |
atanl | Yes | | Yes | | |
atan2 | Yes | Yes | Yes | Yes | |
atan2l | Yes | | Yes | | |
+-----+------+---------+--------+----------+
See Also:
cos
complex
cosh
sin
tan
Examples:
acos example
asin example
atan example
atan2 example
acos example
==============
#include <stdio.h>
#include <math.h>
int main(void)
{
double result;
double x = 0.5;
result = acos(x);
printf("The arc cosine of %lf is %lf\n", x, result);
return 0;
}
asin example
==============
#include <stdio.h>
#include <math.h>
int main(void)
{
double result;
double x = 0.5;
result = asin(x);
printf("The arc sin of %lf is %lf\n", x, result);
return(0);
}
atan example
==============
#include <stdio.h>
#include <math.h>
int main(void)
{
double result;
double x = 0.5;
result = atan(x);
printf("The arc tangent of %lf is %lf\n", x, result);
return(0);
}
atan2 example
===============
#include <stdio.h>
#include <math.h>
int main(void)
{
double result;
double x = 90.0, y = 45.0;
result = atan2(y, x);
printf("The arc tangent ratio of %lf is %lf\n", (y / x), result);
return 0;
}
==============
atof, _atold <MATH.H, STDLIB.H>
==============
* atof converts a string to a floating point
* _atold converts a string to a long double
Declaration:
* double atof(const char *s);
* long double _atold(const char *(s));
Remarks:
* atof converts the string *s to double.
* _atold converts the string *s to a long double.
atof recognizes the character representation
of a floating-point number made up of the
following:
* an optional string of tabs and spaces
* an optional sign
* a string of digits and an optional decimal point (the digits can be on
both sides of the decimal point)
* an optional e or E followed by an optional signed integer
The characters must match this format:
[whitespace] [sign] [ddd] [.] [ddd] [e|E[sign]ddd]
atof also recognizes
* +INF and -INF (plus and minus infinity)
* +NAN and -NAN (Not-a-Number)
In atof, the first unrecognized character
ends the conversion.
strtod and strtoul are similar to atof and
_atold; they provide better error detection
than the ato... functions, and are preferred
in some applications.
Return Value:
* On success, these functions return the
converted value of the input string.
* If the input string can't be converted,
these functions return 0.
* If there is an overflow, atof and _atold:
* return plus or minus HUGE_VAL
* set errno to ERANGE
* do NOT call matherr or _matherrl
Portability:
+ DOS + UNIX + Windows + ANSI C + C++ Only +
atof | Yes | Yes | Yes | Yes | |
_atold | Yes | | Yes | | |
+-----+------+---------+--------+----------+
See Also:
atoi
atol
ecvt
fcvt
gcvt
scanf
strtod
Example:
#include <stdlib.h>
#include <stdio.h>
int main(void)
{
float f;
char *str = "12345.67";
f = atof(str);
printf("string = %s float = %f\n", str, f);
return 0;
}
============================
ceil, ceill, floor, floorl <MATH.H>
============================
* ceil and ceill round up
* floor and floorl round down
Declaration:
* double ceil(double x);
* double floor(double x);
* long double ceill(long double (x));
* long double floorl(long double (x));
Remarks:
* ceil finds the smallest integer not < x.
* ceill finds the smallest (long double) integer not < x.
* floor finds the largest integer not > x.
* floorl finds the largest (long double) integer not > x.
Return Value:
Both ceil and floor return the integer found
as a double; ceill and floorl return the
integer found as a long double.
Portability:
+ DOS + UNIX + Windows + ANSI C + C++ Only +
ceil | Yes | Yes | Yes | Yes | |
ceill | Yes | | Yes | | |
floor | Yes | Yes | Yes | Yes | |
floorl | Yes | | Yes | | |
+-----+------+---------+--------+----------+
See Also:
fmod
Example (for both ceil and floor):
#include <math.h>
#include <stdio.h>
int main(void)
{
double number = 123.54;
double down, up;
down = floor(number);
up = ceil(number);
printf("original number %5.2lf\n", number);
printf("number rounded down %5.2lf\n", down);
printf("number rounded up %5.2lf\n", up);
return 0;
}
==================
cos, sin, tan, (real) <MATH.H>
cosl, sinl, tanl (complex) <COMPLEX.H>
==================
Cosine, sine, and tangent functions
Declaration:
* Real: double cos(double x);
double sin(double x);
double tan(double x);
long double cosl(long double x);
long double sinl(long double x);
long double tanl(long double x);
* Complex: complex cos(complex z);
complex sin(complex z);
complex tan(complex x);
Remarks:
Real versions
=============
* cos and cosl compute the cosine of the input value
* sin and sinl compute the sine of the input value
* tan and tanl calculate the tangent of the input value
Angles are specified in radians.
Complex versions
================
* Complex cosine: cos(z) = (exp(i * z) + exp(-i * z)) / 2
* Complex sine: sin(z) = (exp(i * z) - exp(-i * z)) / (2i)
* Complex tangent: tan(z) = sin(z) / cos(z)
Return Value:
* On success,
* cos and cosl return the cosine of the
input value (in the range -1 to 1)
* sin and sinl return the sine of the
input value (in the range -1 to 1)
* tan returns the tangent of x,
sin(x)/cos(x).
Error handling for these routines can be
modified via matherr and _matherrl.
Portability:
+ DOS + UNIX + Windows + ANSI C + C++ Only +
Real cos | Yes | Yes | Yes | Yes | |
Real sin | Yes | Yes | Yes | Yes | |
Real tan | Yes | Yes | Yes | Yes | |
Complex cos | Yes | | Yes | | Yes |
Complex cos | Yes | | Yes | | Yes |
Complex tan | Yes | | Yes | | Yes |
cosl | Yes | | Yes | | |
sinl | Yes | | Yes | | |
tanl | Yes | | Yes | | |
+-----+------+---------+--------+----------+
See Also:
acos
asin
atan
atan2
complex
cosh
Examples:
cos example
sin example
tan example
cos example
=============
#include <stdio.h>
#include <math.h>
int main(void)
{
double result;
double x = 0.5;
result = cos(x);
printf("The cosine of %lf is %lf\n", x, result);
return 0;
}
sin example
=============
#include <stdio.h>
#include <math.h>
int main(void)
{
double result, x = 0.5;
result = sin(x);
printf("The sin() of %lf is %lf\n", x, result);
return 0;
}
tan example
=============
#include <stdio.h>
#include <math.h>
int main(void)
{
double result, x;
x = 0.5;
result = tan(x);
printf("The tan of %lf is %lf\n", x, result);
return 0;
}
=====================
cosh, sinh, tanh, (real) <MATH.H>
coshl, sinhl, tanhl (complex) <COMPLEX.H>
=====================
Hyperbolic cosine, sine, and tangent functions
Declaration:
* Real: double cosh(double x);
double sinh(double x);
double tanh(double x);
long double coshl(long double (x));
long double sinhl(long double (x));
long double tanhl(long double (x));
* Complex: complex cosh(complex z);
complex sinh(complex z);
complex tanh(complex x);
Remarks:
Real versions
=============
* cosh and coshl compute the hyperbolic cosine: (e**x + e**-x) / 2
* sinh and sinhl compute the hyperbolic sine: (e**x - e**-x) / 2
* tanh and tanhl calculate the hyperbolic tangent: sinh(x) / cosh(x)
Complex versions
================
* Complex cosine: cosh(z) = (exp(z) + exp(-z)) / 2
* Complex sine: sinh(z) = (exp(z) - exp(-z)) / 2
* Complex tangent: tanh(z) = sinh(z) / cosh(z)
Return Value:
* On success,
* cosh and coshl: hyperbolic cosine of x
* sinh and sinhl: hyperbolic sine of x
* tanh and tanhl: hyperbolic tangent of x
* On failure (when the correct value would
create an overflow), cosh and sinh return
HUGE_VAL with the appropriate sign (coshl
and sinhl return _LHUGE_VAL with the
appropriate sign) and set errno to ERANGE.
Error handling for these hyperbolic functions
can be modified via matherr and _matherrl.
Portability:
+ DOS + UNIX + Windows + ANSI C + C++ Only +
Real cosh | Yes | Yes | Yes | Yes | |
Real sinh | Yes | Yes | Yes | Yes | |
Real tanh | Yes | Yes | Yes | Yes | |
Complex cosh | Yes | | Yes | | Yes |
Complex sinh | Yes | Yes | Yes | | Yes |
Complex tanh | Yes | | Yes | | Yes |
coshl | Yes | | Yes | | |
sinhl | Yes | | Yes | | |
tanhl | Yes | | Yes | | |
+-----+------+---------+--------+----------+
See Also:
acos
asin
atan
atan2
complex
cos
sin
tan
Examples:
cosh example
sinh example
tanh example
cosh example
==============
#include <stdio.h>
#include <math.h>
int main(void)
{
double result;
double x = 0.5;
result = cosh(x);
printf("The hyperbolic cosine of %lf is %lf\n", x, result);
return 0;
}
sinh example
==============
#include <stdio.h>
#include <math.h>
int main(void)
{
double result, x = 0.5;
result = sinh(x);
printf("The hyperbolic sin() of %lf is %lf\n", x, result);
return 0;
}
tanh example
==============
#include <stdio.h>
#include <math.h>
int main(void)
{
double result, x;
x = 0.5;
result = tanh(x);
printf("The hyperbolic tangent of %lf is %lf\n", x, result);
return 0;
}
===========
exp, expl (real) <MATH.H>
=========== (complex) <COMPLEX.H>
* Real exp calculates e to the xth power
* Complex exp calculates e to the zth power (z is a complex number)
Declaration:
* Real: double exp(double x);
long double exp(long double (x));
* Complex: complex exp(complex z);
Remarks:
exp calculates the exponential function e**x.
The complex exponential function is defined
by
exp(x + iy) = exp(x) * (cos(y) + i sin(y))
Return Value:
* On success, exp returns e**x
Sometimes the arguments passed to exp produce
results that overflow or are incalculable.
* On overflow,
* exp returns HUGE_VAL
* expl returns _LHUGE_VAL
Results of excessively large magnitude
cause errno to be set to
ERANGE (Result out of range).
* On underflow, exp and expl return 0.0,
and errno is not changed.
Error handling for these functions can be
modified via matherr and _matherrl.
Portability:
+ DOS + UNIX + Windows + ANSI C + C++ Only +
expl | Yes | | Yes | | |
Real exp | Yes | Yes | Yes | Yes | |
Complex exp | Yes | | Yes | | Yes |
+-----+------+---------+--------+----------+
See Also:
frexp
ldexp
log
log10
pow
pow10
sqrt
Example:
#include <stdio.h>
#include <math.h>
int main(void)
{
double result;
double x = 4.0;
result = exp(x);
printf("'e' raised to the power \
of %lf (e ^ %lf) = %lf\n",
x, x, result);
return 0;
}
=============
fmod, fmodl <MATH.H>
=============
Calculates x modulo y, the remainder of x/y
Declaration:
* double fmod(double x, double y);
* long double fmod(long double (x), long double (y));
Remarks:
fmod and fmodl calculate x modulo y. This is
defined as the remainder f, where
x = (ay + f) for some integer a
and
0 < f < y.
Return Value:
* Where x = ay + f and 0 < f < y,
fmod and fmodl return the remainder f.
* Where y = 0, fmod and fmodl return 0.
Portability:
+ DOS + UNIX + Windows + ANSI C + C++ Only +
fmod | Yes | Yes | Yes | Yes | |
fmodl | Yes | | Yes | | |
+-----+------+---------+--------+----------+
See Also:
ceil
floor
modf
Example:
#include <stdio.h>
#include <math.h>
int main(void)
{
double x = 5.0, y = 2.0;
double result;
result = fmod(x,y);
printf("The remainder of (%lf / %lf) is %lf\n", x, y, result);
return 0;
}
===============
frexp, frexpl <MATH.H>
===============
* frexp splits a double number into mantissa and exponent
* frexpl splits a long double number into mantissa and exponent
Declaration:
* double frexp(double x, int *exponent);
* long double frexp(long double (x), int *(exponent));
Remarks:
Given x (a double), frexp calculates the
mantissa m (a double) and n (an integer),
such that
x = m * (2**n)
where
0.5 =< m < 1
frexpl calculates the mantissa and integer
for a long double x.
frexp and frexpl store n in the integer
*exponent.
Return Value:
frexp and frexpl return the mantissa m.
You can use the function matherr to modify
error handling for frexp, or _matherrl for
frexpl.
Portability:
+ DOS + UNIX + Windows + ANSI C + C++ Only +
fexp | Yes | Yes | Yes | Yes | |
fexpl | Yes | | Yes | | |
+-----+------+---------+--------+----------+
See Also:
exp
ldexp
Example:
#include <math.h>
#include <stdio.h>
int main(void)
{
double mantissa, number;
int exponent;
number = 8.0;
mantissa = frexp(number, &exponent);
printf("The number %lf is ", number);
printf("%lf times two to the ", mantissa);
printf("power of %d\n", exponent);
return 0;
}
===============
hypot, hypotl <MATH.H>
===============
Calculates hypotenuse of right triangle
Declaration:
* double hypot(double x, double y);
* long double hypotl(long double (x), long double (y));
Remarks:
hypot and hypotl calculate the value z where
+-----------------+
z**2 = x**2 + y**2 | | |
| y | |
and | | |
| +----------- |
z >= 0 | x |
+-----------------+
This is equivalent to the length of the
hypotenuse of a right triangle, if the
lengths of the two sides are x and y.
Return Value:
* On success,
* hypot returns a double
* hypotl returns a long double
* On error (such as an overflow), both
functions set errno to ERANGE (result
out of range) and:
* hypot returns the value HUGE_VAL.
* hypotl returns the value _LHUGE_VAL.
Error handling for hypot can be modified
via matherr; for hypotl, via _matherrl.
Portability:
+ DOS + UNIX + Windows + ANSI C + C++ Only +
hypot | Yes | Yes | Yes | | |
hypotl | Yes | | Yes | | |
+-----+------+---------+--------+----------+
Example:
#include <stdio.h>
#include <math.h>
int main(void)
{
double result;
double x = 3.0;
double y = 4.0;
result = hypot(x, y);
printf("The hypotenuse is: %lf\n", result);
return 0;
}
===============
ldexp, ldexpl <MATH.H>
===============
Calculates x times (2 raised to exp)
Declaration:
* double ldexp(double x, int expon);
* long double ldexpl(long double (x), int (expon));
Remarks:
ldexp calculates the double value x * 2**expon.
Return Value:
Both functions return the value they
calculated, x * 2**expon.
For ldexp, this value is the same as
(x * pow(2, expon))
For ldexpl, this value is the same as
(x * powl(2, expon))
Error handling for ldexp can be modified
via matherr; for ldexpl, via _matherrl.
Portability:
+ DOS + UNIX + Windows + ANSI C + C++ Only +
ldexp | Yes | Yes | Yes | Yes | |
ldexpl | Yes | | Yes | | |
+-----+------+---------+--------+----------+
See Also:
exp
frexp
modf
Example:
#include <stdio.h>
#include <math.h>
int main(void)
{
double value;
double x = 2;
/* ldexp raises 2 by a power of 3
then multiplies the result by 2 */
value = ldexp(x,3);
printf("The ldexp value is: %lf\n", value);
return 0;
}
==========================
log, logl, log10, log10l (real) <MATH.H>
========================== (complex) <COMPLEX.H>
* log and logl = natural logarithm function
* log10 and log10l = common logarithm function
Declaration:
* Real: double log(double x);
double log10(double x);
long double logl(long double (x));
long double log10l(long double (x));
* Complex: complex log(complex x);
complex log10(complex x);
Remarks:
Real versions:
* log and logl calculate the natural
logarithm of x.
* log10 and log10l calculate the base 10
logarithm of x.
Complex versions:
The complex natural logarithm is defined by
log(z) = log(abs(z)) + i arg(z)
The complex common logarithm is defined by
log10(z) = log(z) / log(10)
Return Value:
* On success,
* log and logl return the natural log of x
* log10 and log10l the return log (base 10) of x
* On error, if x = 0, these functions set errno to ERANGE
* log and log10 return negative HUGE_VAL
* logl and log10l return negative _LHUGE_VAL
* On error, if x is real and < 0, all four functions set
errno to EDOM (domain error)
Error handling for log and log10 can be
modified via matherr; for logl, via
_matherrl.
Portability:
+ DOS + UNIX + Windows + ANSI C + C++ Only +
Real log | Yes | Yes | Yes | Yes | |
Real log10 | Yes | Yes | Yes | Yes | |
Complex log | Yes | | Yes | | Yes |
Complex log10 | Yes | | Yes | | Yes |
log10l | Yes | | Yes | | |
logl | Yes | | Yes | | |
+-----+------+---------+--------+----------+
See Also:
complex
exp
sqrt
Example:
log example
log10 example
log example
=============
#include <math.h>
#include <stdio.h>
int main(void)
{
double result;
double x = 8.6872;
result = log(x);
printf("The natural log of %lf is %lf\n", x, result);
return 0;
}
log10 example
#include <math.h>
#include <stdio.h>
int main(void)
{
double result;
double x = 800.6872;
result = log10(x);
printf("The common log of %lf is %lf\n", x, result);
return 0;
}
=============
_matherrl, <MATH.H>
matherr
=============
User-modifiable math error handler
Declaration:
* int matherr(struct exception *e);
* int _matherrl(struct _exceptionl *(e));
Remarks:
* matherr is called when an error is
generated by the math library.
* _matherrl is called when an error is
generated by the long double math functions.
matherr and _matherrl each serve as a user
hook (a function that you can customize) that
you can replace by writing your own math
error-handling routine.
matherr and _matherrl are useful for trapping
domain and range errors caused by the math
functions.
They do not trap floating-point exceptions,
such as division by 0. See signal for
trapping such errors.
The source code to the default matherr (and
_matherrl) is on the Borland C++ distribution
disks.
The UNIX-style matherr (and _matherrl)
default behavior (printing a message and
terminating) is not ANSI compatible.
User-defined matherr (and _matherrl)
====================================
You can define your own matherr or _matherrl
routine to be a custom error handler (such as
one that catches and resolves certain types
of errors).
This customized function overrides the
default version in the C library.
The customized matherr or _matherrl should
return 0 if it fails to resolve the error, or
non-zero if the error is resolved.
Return Value:
* If the error is UNDERFLOW or TLOSS, the
default return value is 1.
* Otherwise, the default return value is 0.
* When matherr (or _matherrl) returns 0,
(indicating that it was not able to resolve
the error), it sets errno to 0 and an error
message is printed.
* When matherr (or _matherrl) returns
non-zero (indicating that it was able to
resolve the error), errno is not set and no
messages are printed.
matherr (and _matherrl) can also modify
e -> retval, which propagates back to the
original caller.
Portability:
+ DOS + UNIX + Windows + ANSI C + C++ Only +
| Yes | | Yes | | |
+-----+------+---------+--------+----------+
Example:
#include <math.h>
#include <string.h>
#include <stdio.h>
int matherr (struct exception *a)
{
if (a->type == DOMAIN)
if (!strcmp(a->name,"sqrt")) {
a->retval = sqrt (-(a->arg1));
return 1;
}
return 0;
}
int main(void)
{
double x = -2.0, y;
y = sqrt(x);
printf("Matherr corrected value: %lf\n",y);
return 0;
}
=============
modf, modfl <MATH.H>
=============
* modf splits double into integer and fraction parts
* modfl splits long double into integer and fraction parts
Declaration:
* double modf(double x, double *ipart);
* long double modfl(long double (x), long double *(ipart));
Remarks:
* modf breaks the double x into two parts:
the integer and the fraction. It stores the
integer in ipart and returns the fraction.
* modfl is the long double version of modf.
Return Value:
Both functions return the fractional part of x.
Portability:
+ DOS + UNIX + Windows + ANSI C + C++ Only +
modf | Yes | Yes | Yes | Yes | |
modfl | Yes | | Yes | | |
+-----+------+---------+--------+----------+
See Also:
fmod
Example:
#include <math.h>
#include <stdio.h>
int main(void)
{
double fraction, integer;
double number = 100000.567;
fraction = modf(number, &integer);
printf("The whole and fractional parts of %lf are %lf and %lf\n",
number, integer, fraction);
return 0;
}
=============
poly, polyl <MATH.H>
=============
Generates a polynomial from arguments
Declaration:
* double poly(double x, int degree, double coeffs[]);
* long double poly(long double (x), int (degree), long double (coeffs[]));
Remarks:
poly generates a polynomial in x, of degree
degree, with coefficients coeffs[0],
coeffs[1], ..., coeffs[degree].
For example, if n = 4, the generated
polynomial is
coeffs[4] * (x**4) +
coeffs[3] * (x**3) +
coeffs[2] * (x**2) +
coeffs[1] * x +
coeffs[0]
Return Value:
poly returns the value of the polynomial as
evaluated for the given x.
Portability:
+ DOS + UNIX + Windows + ANSI C + C++ Only +
poly | Yes | Yes | Yes | | |
polyl | Yes | | Yes | | |
+-----+------+---------+--------+----------+
Example:
#include <stdio.h>
#include <math.h>
/* polynomial: x**3 - 2x**2 + 5x - 1 */
int main(void)
{
double array[] = { -1.0, 5.0, -2.0, 1.0
};
double result;
result = poly(2.0, 3, array);
printf("The polynomial: x**3 - 2.0x**2 + 5x - 1 at 2.0 is %lf\n",
result);
return 0;
}
===========
pow, powl (real) <MATH.H>
=========== (complex) <COMPLEX.H>
Power function, x to the y (x**y)
Declaration:
* Real: double pow(double x, double y);
long double pow(long double (x), long double (y));
* Complex: complex pow(complex x, complex y);
complex pow(complex x, double y);
complex pow(double x, double y);
Remarks:
pow and powl calculate x**y.
The complex pow is defined by
pow(base, expon) = exp(expon log(base))
Return Value:
* On success, pow and powl return the value
calculated, x**y.
* If x and y are both 0, they return 1.
* If x is real and < 0, and y is not a
whole number, these functions set errno
to EDOM (domain error).
Sometimes the arguments passed to these
functions produce results that overflow or
are incalculable. When the correct value
would overflow, pow returns HUGE_VAL and powl
returns _LHUGE_VAL.
Results of excessively large magnitude can
cause pow or powl to set errno to ERANGE
(result out of range).
Error handling for pow can be modified via
matherr; for powl, via _matherrl.
Portability:
+ DOS + UNIX + Windows + ANSI C + C++ Only +
Real pow | Yes | Yes | Yes | Yes | |
Complex pow | Yes | | Yes | | Yes |
powl | Yes | | Yes | | |
+-----+------+---------+--------+----------+
The complex version of this function requires
C++ and probably is not portable.
powl is unique to DOS.
See Also:
complex
exp
log
pow10
sqrt
Example:
#include <math.h>
#include <stdio.h>
int main(void)
{
double x = 2.0, y = 3.0;
printf("%lf raised to %lf is %lf\n", x, y, pow(x, y));
return 0;
}
===============
pow10, pow10l <MATH.H>
===============
Power function, 10 to the p (10**p)
Declaration:
* double pow10(int p);
* long double pow10l(int (p));
Remarks:
pow10 and pow10l compute 10**p.
The result is actually calculated to long
double accuracy. All arguments are valid,
though some can cause an underflow or
overflow.
Return Value:
On success, pow10 returns the value
calculated, 10**p.
Portability:
+ DOS + UNIX + Windows + ANSI C + C++ Only +
pow10 | Yes | Yes | Yes | | |
pow10l | Yes | | Yes | | |
+-----+------+---------+--------+----------+
See Also:
exp
pow
Example:
#include <math.h>
#include <stdio.h>
int main(void)
{
double p = 3.0;
printf("Ten raised to %lf is %lf\n", p, pow10(p));
return 0;
}
=============
sqrt, sqrtl (real) <MATH.H>
============= (complex) <COMPLEX.H>
Calculates square root
Declaration:
* Real: double sqrt(double x);
long double sqrtl(long double @E(x));
* Complex: complex sqrt(complex x);
Remarks:
sqrt calculates the positive square root of
the input value.
For complex numbers x, sqrt(x) gives the
complex root whose arg is arg(x) / 2.
The complex square root is defined by
sqrt(z) = sqrt(abs(z)) * ( cos(arg(z)/2) + i * sin(arg(z)/2) )
Return Value:
Real sqrt and sqrtl return the square root of x.
* If x is real and positive, the result is positive.
* If x is real and negative, sqrt sets errno to EDOM (domain error).
Error handling for real sqrt can be
modified via matherr; for sqrtl, via _matherrl.
Portability:
+ DOS + UNIX + Windows + ANSI C + C++ Only +
Real sqrt | Yes | Yes | Yes | Yes | |
Complex sqrt | Yes | | Yes | | Yes |
sqrtl | Yes | | Yes | | |
+-----+------+---------+--------+----------+
See Also:
abs
arg
complex
cos
exp
log
pow
sin
Example:
#include <math.h>
#include <stdio.h>
int main(void)
{
double x = 4.0, result;
result = sqrt(x);
printf("The square root of %lf is %lf\n", x, result);
return 0;
}
=====
bcd <BCD.H>
=====
Converts number to binary-coded decimal
Declaration:
bcd bcd(int x);
bcd bcd(double x);
bcd bcd(double x, int decimals);
Remarks:
All of the usual arithmetic operators have
been overloaded to work with BCD numbers.
The argument decimals is optional. You can
use it to specify how many decimal digits
after the decimal point are to be carried in
the conversion.
BCD numbers have about 17 decimal digits of
precision, and a range of about 1 x 10E(-125)
to 1 x 10E(125).
Use the function real to convert a BCD number
back to a float, double, or long double.
The number is rounded to the nearest whole
number, with ties being rounded to an even
digit ("banker's rounding").
Return Value:
The BCD equivalent of the given number.
Portability:
+ DOS + UNIX + Windows + ANSI C + C++ Only +
| Yes | | Yes | | Yes |
+-----+------+---------+--------+----------+
See Also:
real
Example:
#include <iostream.h>
#include <bcd.h>
double x = 10000.0; // ten thousand dollars
bcd a = bcd(x/3,2); // a third, rounded to nearest penny
int main(void)
{
cout << "share of fortune = $" << a << "\n";
return 0;
}
======
real <BCD.H, COMPLEX.H>
======
* (Complex version) Returns real part of complex number
* (BCD version) Converts BCD number back to float, double, or long double
Declaration:
* double real(complex x);
* double real(bcd x);
Remarks:
The data associated to a complex number
consists of two floating-point numbers. real
returns the one considered to be the real
part.
You can also use real to convert a binary
coded decimal (BCD) number back to a float,
double, or long double.
Return Value:
The real part of part of the complex number.
Portability:
+ DOS + UNIX + Windows + ANSI C + C++ Only +
| Yes | | Yes | | Yes |
+-----+------+---------+--------+----------+
See Also:
bcd
complex
imag
Example:
#include <iostream.h>
#include <complex.h>
int main(void)
{
double x = 3.1, y = 4.2;
complex z = complex(x,y);
cout << "z = " << z << "\n";
cout << " has real part = " << real(z) << "\n";
cout << " and imaginary real part = " << imag(z) << "\n";
cout << "z has complex conjugate = " << conj(z) << "\n";
return 0;
}
======================
Overloaded operators
======================
Overloaded operators are those that have been
redefined within a C++ class using the
keyword operator followed by an operator
symbol.
When an operator is thus overloaded, the
resulting symbol is called the operator
function name.
All operators can be overloaded except for
these:
. .* :: ?:
and the preprocessor symbols # and ##.
With the exception of the assignment function
operator =(), all overloaded operators are
inherited by any classes derived from the
class they are defined in.
See Also:
operators
punctuators
precedence
=====
arg <COMPLEX.H>
=====
Gives the angle of a number in the complex plane
Declaration: double arg(complex z);
Remarks:
arg gives the angle, in radians, of the
number in the complex plane.
Angles for arg (counter-clockwise from 0):
(imaginary axis)
pi/2
|
|
pi ------+------ 0, 2*pi (real axis)
|
|
3*pi/2
If the argument passed to arg is complex 0
(zero), arg returns 0.
Return Value:
arg(x) returns atan2(imag(x), real(x)).
Portability:
+ DOS + UNIX + Windows + ANSI C + C++ Only +
| Yes | | Yes | | Yes |
+-----+------+---------+--------+----------+
See Also:
complex
norm
polar
Example:
#include <iostream.h>
#include <complex.h>
int main(void)
{
double x = 3.1, y = 4.2;
complex z = complex(x,y);
cout << "z = " << z << "\n";
cout << " has real part = " << real(z) << "\n";
cout << " and imaginary part = " << imag(z) << "\n";
cout << "z has complex conjugate = " << conj(z) << "\n";
double mag = sqrt(norm(z));
double ang = arg(z);
cout << "The polar form of z is:\n";
cout << " magnitude = " << mag << "\n";
cout << " angle (in radians) = " << ang << "\n";
cout << "Reconstructing z from its polar form gives:\n";
cout << " z = " << polar(mag,ang) << "\n";
return 0;
}
=========
complex <COMPLEX.H>
=========
Creates complex numbers
Declaration: complex complex(double real, double imag);
Remarks:
Creates a complex number out of the given
real and imaginary parts.
The imaginary part is assumed to be 0 if imag
is omitted.
complex as constructor
======================
complex is the constructor for the C++ class
complex.
Return Value:
Returns the complex number with the given
real and imaginary parts.
Portability:
+ DOS + UNIX + Windows + ANSI C + C++ Only +
| Yes | | Yes | | Yes |
+-----+------+---------+--------+----------+
See Also:
abs
acos
arg
asin
atan
atan2
conj
cos
cosh
imag
log
log10
norm
polar
pow
real
sin
sinh
sqrt
tan
tanh
More about programming in C++
Example:
#include <iostream.h>
#include <complex.h>
int main(void)
{
double x = 3.1, y = 4.2;
complex z = complex(x,y);
cout << "z = "<< z << "\n";
cout << " and imaginary real part = " << imag(z) << "\n";
cout << "z has complex conjugate = " << conj(z) << " \n";
return 0;
}
======
conj <COMPLEX.H>
======
Complex conjugate of a complex number
Declaration: double conj(complex z);
Remarks:
conj(z) is the same as
complex(real(z), -imag(z)).
Return Value:
Returns the complex conjugate of the complex
number z.
Portability:
+ DOS + UNIX + Windows + ANSI C + C++ Only +
| Yes | | Yes | | Yes |
+-----+------+---------+--------+----------+
See Also:
complex
imag
real
Example:
#include <iostream.h>
#include <complex.h>
int main(void)
{
double x = 3.1, y = 4.2;
complex z = complex(x,y);
cout << "z = "<< z << "\n";
cout << " and imaginary real part = " << imag(z) << "\n";
cout << "z has complex conjugate = " << conj(z) << " \n";
return 0;
}
======
imag <COMPLEX.H>
======
Returns the imaginary part of a complex number
Declaration: double imag(complex x);
Remarks:
The data associated to a complex number
consists of two floating-point (double)
numbers.
imag returns the double considered to be the
imaginary part of the complex number.
Return Value:
Returns the imaginary part of the complex
number.
Portability:
+ DOS + UNIX + Windows + ANSI C + C++ Only +
| Yes | | Yes | | Yes |
+-----+------+---------+--------+----------+
See Also:
complex
conj
real
Example:
#include <iostream.h>
#include <complex.h>
int main(void)
{
double x = 3.1, y = 4.2;
complex z = complex(x,y);
cout << "z = " << z << "\n";
cout << " has real part = " << real(z) << "\n";
cout << " and imaginary real part = " << imag(z) << "\n";
cout << "z has complex conjugate = " << conj(z) << "\n";
return 0;
}
======
norm <COMPLEX.H>
======
Returns the square of the absolute value
Declaration: double norm(complex x);
Remarks:
norm can overflow if either the real or
imaginary part is sufficiently large.
Return Value:
norm(x) returns the magnitude
(real(x) * real(x)) + (imag(x) * imag(x))
Portability:
+ DOS + UNIX + Windows + ANSI C + C++ Only +
| Yes | | Yes | | Yes |
+-----+------+---------+--------+----------+
See Also:
arg
complex
polar
Example:
#include <iostream.h>
#include <complex.h>
int main(void)
{
double x = 3.1, y = 4.2;
complex z = complex(x,y);
cout << "z = " << z << "\n";
cout << " has real part = " << real(z) << "\n";
cout << " and imaginary real part = " << imag(z) << "\n";
cout << "z has complex conjugate = " << conj(z) << "\n";
double mag = sqrt(norm(z));
double ang = arg(z);
cout << "The polar form of z is:\n";
cout << " magnitude = " << mag << "\n";
cout << " angle (in radians) = " << ang << "\n";
cout << "Reconstructing z from its polar form gives:\n";
cout << " z = " << polar(mag,ang) << "\n";
return 0;
}
=======
polar <COMPLEX.H>
=======
Calculates complex number
Declaration: complex polar(double mag, double angle);
Remarks:
polar calculates the complex number with the
given magnitude and angle. The default angle
is 0. These two declarations are the same:
polar(mag, angle);
complex(mag * cos(angle), mag * sin(angle));
* NOTE: The "*" symbol here signifies
multiplication, not some form of indirection.
Return Value:
Returns the complex number with the given
magnitude (absolute value) and angle
(argument).
Portability:
+ DOS + UNIX + Windows + ANSI C + C++ Only +
| Yes | | Yes | | Yes |
+-----+------+---------+--------+----------+
See Also:
arg
complex
norm
Example:
#include <iostream.h>
#include <complex.h>
int main(void)
{
double x = 3.1, y = 4.2;
complex z = complex(x,y);
cout << "z = " << z << "\n";
cout << " has real part = " << real(z) << "\n";
cout << " and imaginary real part = " << imag(z) << "\n";
cout << "z has complex conjugate = " << conj(z) << "\n";
double mag = sqrt(norm(z));
double ang = arg(z);
cout << "The polar form of z is:\n";
cout << " magnitude = " << mag << "\n";
cout << " angle (in radians) = " << ang << "\n";
cout << "Reconstructing z from its polar form gives:\n";
cout << " z = " << polar(mag,ang) << "\n";
return 0;
}
CW_DEFAULT <FLOAT.H>
============
Default control word for 8087/80287 math
coprocessor.
=====================
_clear87, _status87 <FLOAT.H>
=====================
* _clear87 clears floating-point status
* _status87 gets floating-point status
Declaration:
* unsigned int _clear87 (void);
* unsigned int _status87 (void);
Remarks:
_clear87 clears the floating-point status
word.
_status87 gets the floating-point status
word.
This word is a combination of the 80x87
status word and other conditions detected
by the 80x87 exception handler.
Return Value:
* _clear87: the bits in the value returned
indicate the floating-point status before
it was cleared.
* _status87: the bits in the return value
give the floating-point status
For information on the status word, refer to
the constants defined in FLOAT.H.
Portability:
+ DOS + UNIX + Windows + ANSI C + C++ Only +
| Yes | | Yes | | |
+-----+------+---------+--------+----------+
See Also:
_control87
_fpreset
Example (for both functions):
#include <stdio.h>
#include <float.h>
int main(void)
{
float x;
double y = 1.5e-100;
printf("\nStatus 87 before error: %X\n", _status87());
x = y; /* create underflow and precision loss */
printf("Status 87 after error: %X\n", _status87());
_clear87();
printf("Status 87 after clear: %X\n", _status87());
y = x;
return 0;
}
============
_control87 <FLOAT.H>
============
Changes floating-point control word
Declaration:
unsigned int _control87(unsigned int newcw, unsigned int mask);
Remarks:
_control87 retrieves or changes the
floating-point control word.
The floating-point control word is an
unsigned int that specifies the following
modes in the floating-point package:
* precision mode
* infinity mode
* rounding mode
Changing these modes allows you to mask or
unmask floating-point exceptions.
_control87 matches the bits in mask to the
bits in newcw.
If any mask bit = 1, the corresponding bit in
newcw contains the new value for the same bit
in the floating-point control word.
For Example:
+------------------------------------------+
| Original |
| control word: 0100 0011 0110 0011 |
| mask 1000 0001 0100 1111 |
| newcw 1000 1001 0000 0101 |
| Changing bits: 1xxx xxx1 x0xx 0101 |
+------------------------------------------+
If mask = 0000, _control87 returns the
floating-point control word without altering
it.
_control87 does not change the Denormal bit
because Borland C++ uses denormal exceptions.
Return Value:
The bits in the value returned reflect the
new floating-point control word.
For a complete definition of the bits
returned by _control87, see FLOAT.H.
Portability:
+ DOS + UNIX + Windows + ANSI C + C++ Only +
| Yes | | | | |
+-----+------+---------+--------+----------+
See Also:
_clear87
_fpreset
_status87
CW_DEFAULT
signal
Example:
/* mask float on, print exceptions */
_control87 (MCW_EM, MCW_EM);
==========
_fpreset <FLOAT.H>
==========
Reinitializes floating-point math package
Declaration: void _fpreset(void);
Remarks:
_fpreset reinitializes the floating-point
math package.
This function is usually used in conjunction
with system or the exec... or spawn...
functions.
Under DOS, if a program uses an 80x87
coprocessor, a child process (executed by
system or by an exec... or spawn... function)
might alter the parent process's
floating-point state.
If you use an 80x87, take the following precautions:
* Do not call system or an exec... or spawn... function while a
floating-point expression is being evaluated.
* Call _fpreset to reset the floating-point state after using system,
exec..., or spawn... if there is any chance that the child process
performed a floating-point operation with the 80x87.
Return Value: None
Portability:
+ DOS + UNIX + Windows + ANSI C + C++ Only +
| Yes | | | | |
+-----+------+---------+--------+----------+
See Also:
_clear87
_control87
_status87
Example:
#include <stdio.h>
#include <float.h>
#include <setjmp.h>
#include <signal.h>
#include <process.h>
#include <conio.h>
#ifdef __cplusplus
typedef void (*fptr)(int);
#else
typedef void (*fptr)();
#endif
jmp_buf reenter;
/* define a handler for trapping floating
point errors */
void float_trap(int sig)
{
printf("Trapping floating point error,");
printf("signal is %d\n",sig);
printf("Press a key to continue\n");
getch();
/*
reset the 8087 chip or emulator to clear
any extraneous garbage
*/
_fpreset();
/* return to the problem spot */
longjmp(reenter, -1);
}
int main(void)
{
float one = 3.14, two = 0.0;
/*
install signal handler for floating point
exception
*/
signal(SIGFPE, (fptr)float_trap);
printf("Generating a math error,");
printf("press a key\n");
getch();
if (setjmp(reenter) == 0)
one /= two;
printf("Returned from signal trap\n");
return 0;
}
SIGFPE Type Signals
=====================
These SIGFPE-type signals can occur (or be
generated).
They correspond to the exceptions that the
8087 family is capable of detecting, as well
as the
"INTEGER DIVIDE BY ZERO"
and
"INTERRUPT ON OVERFLOW"
exceptions on the main CPU.
The declarations for these signals are in
FLOAT.H.
SIGFPE signal | Meaning
----------------+---------------------------------
FPE_EXPLICITGEN| User program executed raise(SIGFPE)
|
FPE_INEXACT | Precision
FPE_INTDIV0 | Integer divide by zero
FPE_INTOVFLOW | INTO executed with OF flag set
FPE_INVALID | Invalid operation
FPE_OVERFLOW | Numeric overflow
FPE_UNDERFLOW | Numeric underflow
FPE_ZERODIVIDE | Division by zero
The FPE_INTOVFLOW and FPE_INTDIV0 signals
are generated by integer operations, and
the others are generated by floating-point
operations.
Whether the floating-point exceptions are
generated depends on the coprocessor control
word, which can be modified with _control87.
Denormal exceptions are handled by Borland
C++ and not passed to a signal handler.
See Also:
signal
SIGSEGV Type Signals
======================
These SIGSEGV-type signals can occur:
SIGSEGV Signal | Meaning
------------------+------------------------------
SEGV_BOUND | Bound constraint exception
SEGV_EXPLICITGEN | raise(SIGSEGV) was executed
Because the 8088 and 8086 processors don't
have a bound instruction, the SEGV_BOUND
type of SIGSEGV signal won't occur on these
processors.
The 186, 286, 386, and NEC V series
processors do have this instruction.
Borland C++ doesn't generate bound instructions,
but they can be used in inline code and
separately compiled assembler routines that
are linked in.
See Also:
signal
SIGILL Type Signals
=====================
These SIGILL-type signals can occur:
SIGSEGV Signal | Meaning
-----------------+-----------------------------
ILL_EXECUTION | Illegal operation attempted
ILL_EXPLICITGEN | raise(SIGILL) was executed
Because the 8088, 8086, NEC V20, and NEC V30
processors don't have an illegal operation
exception, the ILL_EXECUTION type of SIGILL
won't occur on those processors.
The 186, 286, 386, NEC V40, and NEC V50
processors do have this exception type.
See Also:
signal