www.digitalmars.com [Home] [Search] [CTG] [RTL] [IDDE]

Last update Feb 15, 2003


math.h

fpclassify classify floating point type
isfinite test for finite
isinf test for infinity
isnan test for NaN
isnormal test for normal
signbit test sign bit of floating point value
acos arc cosine
asin arc sine
atan arc tangent
atan2 arc tangent in 4 quadrants
ceil round upwards
cos cosine
cosh hyperbolic cosine
exp ex
expm1 ex - 1
fabs absolute value
floor round downwards
fmod remainder after division
frexp extract exponent and mantissa
hypot hypotenuse
ldexp n*2exp
log natural logarithm
logb logarithm base 2
log10 logarithm base 10
log1p natural logarithm of (1 + x)
matherr, _matherrl handle errors generated by math functions
modf separate value into integral and fractional parts
nextafter next representable value
poly compute polynomial
pow xy
sin sine
sinh hyperbolic sine
sqrt square root
tan tangent
tanh hyperbolic tangent
_cabs absolute value of complex number

fpclassify

Header

#include <math.h>

int fpclassify(x); 

Description

This function, implemented as a macro, determines if a floating point value belongs to a special class. x's type is either float, double or long double. The following values can be compared to the result from fpclassify. They classify floating point values and expand to a unique constant expression of type int:

Value Meaning
FP_NANS Signalling NaN
FP_NANQ Quiet NaN
FP_NAN Quiet NaN (same as FP_NANQ)
FP_INFINITE Infinity
FP_NORMAL A number not covered by other classifications
FP_SUBNORMAL Subnormal or denormal
FP_ZERO Zero
FP_EMPTY No value
FP_UNSUPPORTED Unsupported bit pattern

Return Value

Returns a constant that reflects the class of the floating-point expression x.

Compatibility

DOS Windows 3.x Phar Lap DOSX Win32

isfinite
isinf
isnan
isnormal


isfinite, isinf, isnan, isnormal

Header

#include <math.h>

int isfinite(x);
int isinf(x);
int isnan(x);
int isnormal(x);
Description

Each of these macros take an argument of type float, double, or long double, and determine if its classified type matches the name of the macro.

Macro Test
isfinite is finite, non-zero
isinf is infinity
isnan is a NaN
isnormal is not zero, infinity, subnormal, or NaN

Return Value

Returns non-zero if a match, 0 if not.

Compatibility

C99 7.12.3

See Also

fpclassify

signbit

Header

math.h

Prototype

int signbit(x);

Description

Examines the sign bit of a floating-point value (including NaNs, infinities, and zero). signbit is implemented as a macro, and its argument can be either a double or a float.

Return Value

Non-zero if the sign bit is 1; zero if the sign bit is 0.

Compatibility

C99 7.12.3.6


acos

Header

math.h
fltenv.h (Required for exception values)

Prototype

double acos(double x);
float acosf(float x);
long double acosl(long double x);

Description

The acos functions calculate the principal value of the arc cosine of x. The acos function calculates the arc cosine of a double value; acosf calculates the arc cosine of a floating-point value; acosl calculates the arc cosine of a long double value.

Return Value

The arc cosine of x.

Special Results

	value of x 	return value 	invalid? 
	>1.0 		NAN 		yes 
	<-1.0 		NAN 		yes
Compatibility

DOS Windows 3.x Phar Lap DOSX Win32

See Also

asin
atan
atan2
cos
cosh
hypot
sin

tan
tanh

Example

	/* 	Example of acos 	*/ 
	
	#include <math.h>
	#include <stdio.h>
	#include <stdlib.h>
 
	void main() 
	{ 
	   double d; 
	   double result; 

	   printf("Enter a double:"); 
	   scanf("%lg", &d); 
	   result = acos(d); 
	   printf("\nacos(%lg) = %lg\n", d, result); 
	}
Output

Enter a double:. 45 acos(0.45) = 1.10403


asin

Header

math.h
errno.h
fltenv.h (Required for exception values)

Prototype

double asin(double x);
float asinf(float x);
long double asinl(long double x);

Description

The asin functions calculate the principal value of the arcsine of x. asin calculates the arcsine of a double value; asinf calculates the arcsine of a floating-point value; and asinl calculates the arcsine of a long double value. The value of x must be between -1 and 1.

Synonym

Function: _asinl

Return Value

The arcsine of x. Each function returns a value ranging from -π/2 to π/2. For asinl, if the x argument is outside the range of -1 to 1, the function returns NAN (not a number) and errno is set to EDOM, for domain error.

Special Results

value of x	return value	invalid?		 
 ±0.0		±0.0		no	 
>1.0		NAN		yes 
<-1.0		NAN		yes 
Compatibility

DOS Windows 3.x Phar Lap DOSX Win32

See Also

acos
atan
atan2
cos
cosh
hypot
sin
sinh
tan
tanh Functions

Example

	/* 	Example of asin 	*/

	#include <math.h> 
	#include <stdio.h> 
	#include <stdlib.h> 

	void main() 
	{ 
	   double d;

	   printf("Enter a double : "); 
	   scanf("%lg", &d); 
	   printf("\nasin(%g) = %g\n", d, asin(d));
 
	} 
Output

Enter a double :. 45
asin(0.45) = 0.466765


atan

Header

math.h
fltenv.h (Required for exception values)

Prototype

double atan(double x);
float atanf(float x);
long double atanl(long double x);

Description

The atan functions calculate the arc tangent of x. The atan function calculates the arc tangent of a double value; atanf calculates the arc tangent of a floating-point value; and _atanl and atanl calculate the arc tangent of a long double value.

Synonym

Function: _atanl

Return Value

The arc tangent of x. This is a value in the range of -π/2 to π/2.

Special Results

value of x	return value	invalid? 
±0.0 		±0.0 		no 
±INFINITY 	NAN 		yes 
Compatibility

DOS Windows 3.x Phar Lap DOSX Win32

See Also

acos
asin
atan2
cos
cosh
hypot
sin
sinh
tan

Example

	/* 	Example of atan 	*/

	#include <math.h> 
	#include <stdio.h> 
	#include <stdlib.h>
 
	void main() 
	{ 
	   double d;
 
	   printf("Enter a double : "); 
	   scanf("%lg", &d); 
	   printf("\natan(%g) = %g\n", d, atan(d)); 
	} 
Output

Enter a double :. 45
atan(0.45) = 0.422854


atan2

Header

math.h
fltenv.h (Required for exception values)

Prototype

double atan2(double y, double x);
float atan2f(float y, float x);
long double atan2l(long double y, long double x);

Description

The atan2 functions calculate the arc tangent of y/x. The atan2 function calculates the arc tangent of double values; atan2f calculates the arc tangent of floating-point values; and atan2l calculates the arc tangent of long double values.

Synonym

Function: _atan2l

Return Value

The arc tangent of y/x. This is a value in the range -p to p. If one argument is a NaN, than NaN is returned; if both arguments are NaNs, either one may be returned.

If both arguments are 0, these functions set errno to EDOM, print a _DOMAIN error message to stderr, and return 0.

Special Results

value of y		value of x	return value
±0.0 			> 0.0 		±0.0 
±0.0 			±0.0 		±0.0 
±0.0 			< 0.0 		±π 
±0.0 			-0.0 		±π
> 0.0 			±0.0 		π/2 
< 0.0 			±0.0 		π/2 
> 0.0 			INFINITY 	±0.0 
±INFINITY 		anything 	±π/2 
> 0.0 			-INFINITY 	±π 
±INFINITY 		INFINITY 	±π/4 	
±INFINITY 		-INFINITY	±3π/4
Compatibility

DOS Windows 3.x Phar Lap DOSX Win32

See Also

acos
asin
atan
atan2
cos
cosh
hypot
sin
sinh
tan
tanh

Example

	/* 	Example of atan2 	*/

	#include <math.h> 
	#include <stdio.h> 
	#include <stdlib.h>
 
	void main() 
	{ 
	   double d1, d2; 

	   printf("Enter two doubles : "); 
	   scanf("%lg %lg", &d1, &d2); 
	   printf("\natan2(%g, %g) = %g\n", d1, d2, atan2(d1,d2)); 
	}
Output

Enter two doubles :. 3 .4
atan2(0.3, 0.4) = 0.643501


ceil

Header

math.h

Prototype

double ceil(double x);
float ceilf(float x);
long double ceill(long double x);

Description

Returns the value of x rounded upward to the next integer (toward positive infinity.) The ceil function returns the rounded value as a double; ceilf returns the value as a floating-point number; _ceill and ceill return the value as a long double.

Synonym

Function: _ceill

Return Value

x rounded up to the next integer.

Compatibility

DOS Windows 3.x Phar Lap DOSX Win32

See Also

floor
nearbyint
rint
rndtol
rndtonl
round
trunc

Example

	/* 	Example for ceil 	*/

	#include <math.h> 
	#include <stdio.h> 
	#include <stdlib.h> 

	void main() 
	{ 
	  double d;
 
	  printf("Enter a double: "); 
	  scanf("%lg",& d); 
	  printf("\nCeil (%lg) = %lg\n", d, ceil(d)); 
	} 
Output

Enter a double: 3.34
Ceil (3.34) = 4


cos

Header

math.h
fltenv.h (required for exception values)

Prototype

double cos(double x);
float cosf(float x);
double cosl(double x);

Description

The cos functions calculate the cosine of x (measured in radians). The cos function operates on double values; cosf operates on floating-point values; _cosl and cosl operate on long double values.

Synonym

Function: _cos1

Return Value

The cosine of x. If an error occurs because x is too large, errno is set to ERANGE.

Special Results

value of x 	return value 		invalid?
±INFINITY 	NAN 			yes 
Compatibility

DOS Windows 3.x Phar Lap DOSX Win32

See Also

acos
asin
sin
cosh


cosh

Header

math.h
fltenv.h

Prototype

double cosh(double x);
float coshf(float x);
long double coshl(long double x);

Description

The cosh functions calculate the hyperbolic cosine of x. cosh operates on double values; coshf operates on float values; _coshl and coshl operate on long double values.

Synonym

Function: _coshl

Return Value

The hyperbolic sine of x. If the value of x is too large, errno is set to ERANGE.

Special Results

value of x 		return value 		invalid?
±INFINITY 		±0.0	 		no 

Compatibility

DOS Windows 3.x Phar Lap DOSX Win32

See Also

sinh
cos

exp

Header

math.h

Prototype

double exp(double x);
float expf(float x);
long double expl(long double x);

Description

The exp functions calculate the value of the natural logarithm base (e) raised to the power of x. The exp function operates on double values; expf operates on floating-point values; and expl operate on long double values.

Synonym

Function: _expl

Return Value

The value of e x . Otherwise, on overflow, returns HUGE_VAL and sets errno to ERANGE. On underflow, returns 0.

Special Results

value of x/return value

+INFINITY
+INFINITY
-INFINITY
+0.0

Compatibility

DOS Windows 3.x Phar Lap DOSX Win32

See Also

expm1
log
log1p
log10
pow
sqrt

Example

	/* 	Example of exp */

	#include <math.h>
	#include <stdio.h>
	#include <stdlib.h>

	void main()
	{ 
	   double d;
	   double result; 


	   printf("Enter a double:");
	   scanf("%lg", &d); 
	   result = exp(d);
	   printf("\nexp(%lg) = %lg\n", d, result); 
	} 

Output

Enter a double: 2.34
exp(2.34) = 10.3812


expm1

Header

math.h

Prototype

double expm1(double x);
float expm1f(float x);
long double expm1l(long double x);

Description

Calculates the value of the natural logarithm base (e) raised to the power of x, minus 1. For very small x, expm1(x) is more accurate than exp(x) -1.

Return Value

The value of ex-1

Special Results

x ex-1
±0.0 ±0.0
+INFINITY +INFINITY
-INFINITY -1.0

Compatibility

DOS Windows 3.x Phar Lap DOSX Win32

See Also

exp
log
log1p
log10
pow
sqrt


fabs

Header

math.h

Prototype

double fabs(double x);
float fabsf(float x);
long double _fabsl(long double x);

Description

The fabs, fabsf, and fabsl functions calculate the absolute value of a floating-point number (x). For the fabs function, x is a double value; for fabsf , x is a float, and for fabsl, x is a long double.

Synonym

Function: fabsl

Return Value

The absolute value of x.

Special Results

value of x/return value
±0.0
+0.0
±INFINITY
+INFINITY
Compatibility

DOS Windows 3.x Phar Lap DOSX Win32

See Also

abs
copysign
signbit

Example

	/* 	Example of fabs */

	#include <math.h>
	#include <stdio.h>
	#include <stdlib.h>

	void main()
	{ 
	   double d;
	   double result; 

	   printf("Enter a double:");
	   scanf("%lg", &d); 
	   result = fabs(d);
	   printf("\nfabs(%lg) = %lg\n", d, result); 

	} 

Output

Enter a double:-2.34
fabs(-2.34) = 2.34


floor

Header

math.h

Prototype

double floor(double x);
float floorf(float x);
long double floorl(long double x);

Description

Returns the value of x rounded down to the nearest integer.

Synonym

Function: _floor1

Return Value

A floating-point value that represents x rounded down to the nearest integer.

Compatibility

DOS Windows 3.x Phar Lap DOSX Win32

See Also

ceil
nearbyint
rint
rndtol
rndtonl
round
trunc

Example

	/* 	Example of floor
		Also demonstrates floorl, floorf 
	*/
	
	#include <stdlib.h>
	#include <stdio.h>
	#include <math.h>

	void main()
	{ 
	   double d; 
	   printf("Enter a double: ");
	   scanf("%lg", &d); 
	   printf("\nfloor(%g)=%g\n", d, floor (d));
	} 
Output

	Enter a double: 3.14 

	floor(3.14)= 3 

fmod

Header

math.h
fltenv.h (Required for exception values)

Prototype

double fmod(double x, double y);
float fmodf(float x, float y);
long double fmodl(long double x, long double y);

Description

These functions calculate the floating-point remainder of x/y. The fmod function operates on double values; fmodf operates on float values; and fmodl operates on long double values.

Synonym

Function: _fmodl

Return Value

The value of x -i * y, where i is the number of times that y can be completely subtracted from x. The result has the same sign as x.

Special Results

value of x 	value of y 	return value 	invalid?
±0.0 		not 0.0 	±0.0 		no 
±INFINITY 	anything 	NAN 		yes 
anything 	±0.0 		NAN 		yes 
!=± INFINITY 	±INFINITY 	x 		no 

Compatibility

DOS Windows 3.x Phar Lap DOSX Win32

See Also

div
ldiv
remainder
remquo

Example

	/* 	Example for fmod 
		Also demonstrates fmodf, fmodl 
	*/

	#include <stdio.h>
	#include <stdlib.h>
	#include <math.h>

	void main() 
	{ 
	   double d1, d2, r; 
	   printf("Enter two doubles: "); 
	   scanf("%lg %lg", &d1, &d2); 

	   r = fmod(d1, d2); 
	   printf("fmod(%g, %g)=%g\n", d1, d2, r); 
	} 
Output

Enter two doubles: 16 7
fmod (16, 7)=2


frexp

Header

math.h

Prototype

double frexp(double x, int *exp);
float frexpf(float x, int *exp);
long double frexpl(long double x, int *exp);

Description

The frexp, frexpf, _frexpl, and frexpl functions break a floating-point number (x) into a mantisa (m) and an exponent (n), such that 0.5 <= |m| < 1.0 and x = m * 2n. The integer exponent n is stored at the location pointed to by exp.

Return Value

These functions return the mantissa. If x is 0, the functions return 0 for both the mantissa and the exponent. There is no error return.

Special Results

value of x 	return value 	*exp after?
±0.0 ±0.0 +0.0 ±INFINITY ±INFINITY ? NAN NAN ?

Compatibility

DOS Windows 3.x Phar Lap DOSX Win32

Synonym

Function: _frexpl

See Also

ldexp

Example

	/* 	Example for frexp */

	#include <stdio.h>
	#include <stdio.h>
	#include <math.h>

	void main() 
	{ 
	   int e; 
	   double d, r; 

	   printf("Enter a double: "); 
	   scanf("%lg", &d); 
	   r = frexp (d, &e); 
	   printf("%g, = %g * 2^%d.\n", d, r, e); 
	} 
Output

Enter a double: 102
102, = 0.796875 * 2^7.


ldexp

Header

math.h

Prototype

double ldexp(double n, int exp);
float ldexpf(float n, int exp);
long double ldexpl(long double n, int exp);

Description

Each function uses the following form to multiply a number by an integral power of two:

n * 2exp

Synonym

Function: _ldexpl

Return Value

The value of n * 2exp.

Compatibility

DOS Windows 3.x Phar Lap DOSX Win32

See Also

frexp

Example

	/* 	Example for ldexp */

	#include <stdlib.h> 
	#include <stdio.h> 
	#include <math.h>

	void main() 
	{ 
	   int e; 
	   double d, r; 

	   printf("Enter a double and an int: "); 
	   scanf("%lg %d", &d, &e); 
	   r = ldexp (d, e); 
	   printf("%g = %g * 2^%d.\n\n", r, d, e); 
	} 
Output

Enter a double and an int: 125 5
4000 = 125 * 2^ 5.


log

Header

math.h
fltenv.h (Required for exception values)

Prototype

double log(double x);
float logf(float x);
long double logl(long double x);

Description

The log functions calculate the natural logarithm of x. The log function operates on a double value; logf operates on a floating-point value; the logl function operates on long double values.

Synonym

Function: _logl

Return Value

The natural logarithm of x. If x < 0, a domain error is returned. If x == 0, a range error is returned.

Special Results

value of x return value divide by 0? invalid?
±0.0 -INFINITY yes no
< 0.0 NAN no yes
+INFINITY +INFINITY no no

Compatibility

DOS Windows 3.x Phar Lap DOSX Win32

See Also

loglp
logpf
log10

Example

	/* 	Example for log 
		Also demonstrates logl 
	*/

	#include <stdlib.h> 
	#include <stdio.h> 
	#include <math.h>

	void main() 
	{ 
	   double d; 

	   printf("Enter a double: "); 
	   scanf("%lg", &d); 
	   printf("\nlog(%g)=%g\n\n", d, log(d)); 
	} 
Output

Enter a double: 234

log(234)= 5.45532


logb, logbf

Header

math.h

Prototype

double logb(double x);
float logbf(float x);

Description

Extracts the exponent of x, as a signed integral value. If x is subnormal, it is treated as if it were normalized. For a positive, finite x:
	1 <= x * FLT_RADIX ^ -logb(x) < FLT_RADIX 
	
Return Value

The signed exponent of the argument.

Special Results

Value of x Return value Divide by 0?
±INFINITY ±INFINITYno
±0.0 -INFINITY yes

log10

Header

math.h
fltenv.h (Required for exception values)

Prototype

double log10(double x);
float log10f(float x);
long double log10l(long double x);

Description

The log10 functions calculate the base-10 logarithm of x. The log10 function operates on a double value; log10f operates on a floating-point value; the _log10l and log10l functions operate on long double values.

Synonym

Function: _log10l

Return Value

The base-10 logarithm of x. If x < 0, a domain error is returned. If x == 0, a range error is returned.

Special Results

value of x return value divide by 0? invalid?
±0.0 -INFINITY yes no
< 0.0 NAN no yes
+INFINITY +INFINITY no no

Compatibility

DOS Windows 3.x Phar Lap DOSX Win32

See Also

log
log1p

Example

	/* Example for log10 */

	#include <stdlib.h>
	#include <stdio.h> 
	#include <math.h> 

	int main()
	{ 
	   double d; 

	   printf("Enter a double: "); 
	   scanf("%lg", &d); 
	   printf("\nlog10(%g)=%g\n\n", d, log10(d));
	   return 0; 
	} 
Output

Enter a double: 234

log10(234)= 2.36922


log1p, log1pf

Header

math.h
fltenv.h (Required for exception values)

Prototype

double log1p(double x);
float log1pf(float x);

Description

The log1p and log1pf functions calculate the natural logarithm of 1 + x. For very small x, log1p(x) will be more accurate than log(1 + x).

Return Value

The natural logarithm of 1 + x.

Special Results

x log1p(x) divide by 0? invalid?
±0.0 ±0.0 no no
-1.0 -INFINITY yes no
<-1.0 NAN no yes
+INFINITY -INFINITY no no

Compatibility

DOS Windows 3.x Phar Lap DOSX Win32

See Also

log
log10

Example

	/* 	Example for log1p 
		Also demonstrates loglpf 
	*/

	#include <stdlib.h> 
	#include <stdio.h> 
	#include <math.h>

	void main() 
	{ 
	   double d; 

	   printf("Enter a double: "); 
	   scanf("%lg", &d); 
	   printf("\nlog1p(%g)=%g\n\n", d, log1p(d)); 
	} 
Output

Enter a double: 1.234

log1p(1.234)= 0.803794


matherr, _matherrl

Header

math.h

Prototype

int matherr(struct exception *except);
int _matherrl(struct _exceptionl *except);

Description

The matherr and _matherrl functions process errors that have been generated by math functions. The math functions call a math error function whenever an error occurs. Users can also provide their own math error functions to implement special error handling. The _matherrl is called for errors in long double math.

A pointer to the exception structure will be passed to a user-supplied math error function when an error occurs. The exception structures, _exception and _exceptionl are defined in math.h. They have the following elements:

Element/Description
int type
Exception type (see the following table)
char *name
Function name where error occurred
double arg1, arg2
Function arguments
double retval
Value returned by function
The values for type are:

Value/Description
_DOMAIN
Argument domain error
_SING
Argument singularity
_OVERFLOW
Overflow range error
_PLOSS
Partial loss of significance
_TLOSS
Total loss of significance
_UNDERFLOW
Underflow range error

Return Value

These functions return 0 to indicate an error, non-zero for success.

Compatibility

DOS Windows 3.x Phar Lap DOSX Win32

See Also

The other math functions


modf

Header

math.h

Prototype

double modf(double x, double *iptr);
float modff(float x, float *iptr);
long double _modfl(long double x, long double *iptr);

Description

The modf functions break x into its integral and fraction parts. each of which has the same sign as x. The integral portion is stored in the double pointed to by iptr. The modf function operates on double values; modff operates on floating-point values; _modfl operates on long double values.

Synonym

Functions: modfl

Return Value

The signed fractional portion of x.

Special Results

value of x 	return value 	*iptr after
±INFINITY 	±0.0 		±INFINITY 

Compatibility

DOS Windows 3.x Phar Lap DOSX Win32

Example

	/* 	Example for modf
		Also demonstrates modfl 
	*/

	#include <stdlib.h>
	#include <stdio.h>
	#include <math.h> 

	void main()
	{ 
	   double d, frac, intr; 

	   printf("Enter a double: ");
	   scanf("%lg", &d); 
	   frac = modf (d, &intr);
	   printf("\nFor %g, the fractional part is 
		  %g,\nand the integral part is " 
		  "%g.\n", d, frac, intr); 
	} 
Output

Enter a double: 123.456

For 123.456, the fractional part is 0.456,
and the integral part is 123.


nextafter

Header

fltpnt.h

Prototype

double nextafter(double x, double y);
float nextafterf(float x, float y)
long double nextafterl(long double x, long double y);

Description

Calculates the next representable value after x in the direction of y.

Return Value

If y is greater than x, the result will be the next largest floating-point value; if y is less than x, the result will be the next smallest value. If x and y are equal, the result is x. The FE_INEXACT and FE_OVERFLOW exceptions will be raised if x is finite and the function result is infinite. The FE_INEXACT and FE_UNDERFLOW exceptions will be raised if the function value is subnormal, and x is not equal to y.

Compatibility

DOS Windows 3.x Phar Lap DOSX Win32

Example

	/* 	Example for nextafter
		Also demonstrates nextafterf 
	*/

	#include <stdio.h>
	#include <stdlib.h> 
	#include <math.h>
	#include <fltpnt.h>

	void main()
	{ 
	   double d1, d2, r; 
	   printf("Enter two doubles: ");
	   scanf("%lg %lg", &d1, &d2); 

	   r = nextafter(d1, d2);
	   printf("nextafter(%g, %g)=%. 16f\n", d1, 
	   d2, r);
	} 
Output

Enter two doubles: 1 2
nextafter(1, 2)= 1.0000000000000002


poly, polyl

Header

math.h

Prototype

double poly(double x, int deg, double coeff[]);
long double polyl(long double x, int deg, long double coeff[]);

Description

poly and polyl evaluate a polynomial of the form:

(coeff[deg] * x + coeff [deg-1]) * x +...+ coeff[0].

Argument x is the base value; deg is the maximum degree, and coeff is an array containing the coefficients of the values.

The polyl function is the long double version of poly.

Return Value

Both functions return the calculated value of the polynomial.

Compatibility

DOS Windows 3.x Phar Lap DOSX Win32

Example

	/* 	Example for poly
		Also demonstrates polyl 
	*/

	#include <stdio.h>
	#include <stdlib.h>
	#include <math.h>

	void main()
	{ 
	   double coeff[4], x = 1.2, y; 
	   
	   coeff[0] = 0.0;
	   coeff[1] = 1.0; 
	   coeff[2] = 2.0;
	   coeff[3] = 3.0; 

	   y = poly(x, 3, coeff); 

	   printf("poly(%g, 3, {%g, %g, %g, %g}) = 
		     %g\n", x, coeff[0], coeff[1], 
		     coeff[2], coeff[3], y);
	} 
Output

poly(1.2, 3, {0, 1, 2, 3}) = 9.264


pow

Header

math.h

Prototype

double pow(double x, double y);
float powf(float x, float y);
long double powl(long double x, long double y);

Description

The pow functions calculate x raised to the power of y. The pow function operates on double values; powf operates on floating-point values; _powl and powl operate on long double values.

Synonym

Function: _pow1

Return Value

x raised to the power of y. If an overflow occurs, the pow functions set errno to ERANGE and return _HUGE_VAL or _LHUGE_VAL (if the return type is long).

Special Results


value of x	value of y	return value	div 0	inv

anything 	±0.0 		1.0 		no 	no 
|x| > 1. 0 	+INFINITY 	+INFINITY 	no 	no 
|x| < 1. 0 	+INFINITY 	+0.0 		no 	no 
|x| > 1. 0 	-INFINITY 	+0.0 		no 	no 
|x| < 1. 0 	-INFINITY 	+INFINITY 	no 	no 
+INFINITY 	y > 0.0 	+INFINITY 	no 	no 
+INFINITY 	y < 0.0 	+0.0 		no 	no 
-INFINITY 	odd integer>0.0	-INFINITY 	no 	no 
-INFINITY  	>0.0,not odd	+INFINITY 	no 	no
		integer 
-INFINITY 	odd integer<0.0 -0.0 		no 	no 
-INFINITY 	<0.0, not odd 	+0.0 		no 	no 
		integer 
±1.0 		±INFINITY 	NAN 		no 	yes 
<0.0 		finite, 	NAN 		no 	yes
		nonintegral  
±0.0 		odd integer <0.0±INFINITY 	yes 	no 
±0.0 		<0.0, not odd 	+INFINITY 	yes 	no
		integer 
±0.0 		odd integer>0.0 ±0.0 		no 	no 
±0.0 		>0.0, not odd 	+0.0 		no 	no 
		integer 

Compatibility

DOS Windows 3.x Phar Lap DOSX Win32

See Also

exp
log
log1p
log10
sqrt

Example

	/* 	Example for pow
		Also demonstrates powl 
	*/

	#include <stdlib.h>
	#include <stdio.h>
	#include <math.h>

	void main()
	{ 

	   double d1, d2; 

	   printf("Enter two doubles: ");
	   scanf("%lg %lg", &d1, &d2); 
	   printf("\npow(%g, %g)=%g\n", d1, d2,
		    pow (d1, d2)); 
	} 
Output

Enter two doubles: 2 16

pow(2, 16)= 65536


sin

Header

math.h
fltenv.h (required for exception values)

Prototype

double sin(double x);
float sinf(float x);
double sinl(double x);

Description

The sin functions calculate the sine of x (measured in radians). The sin function operates on double values; sinf operates on floating-point values; _sinl and sinl operate on long double values.

Synonym

Function: _sin1

Return Value

The sine of x. If an error occurs because x is too large, errno is set to ERANGE.

Special Results

value of x return value invalid? ±0.0 ±0.0 no ±INFINITY NAN yes

Compatibility

DOS Windows 3.x Phar Lap DOSX Win32

See Also

acos
asin
cos
sinh

Example

	/* 	Example of sin 
		Also demonstrates sinl 
	*/

	#include <stdlib.h>
	#include <stdio.h> 
	#include <math.h> 

	void main()
	{ 
	   double d; 

	   printf("Enter a double: "); 
	   scanf("%lg", &d); 
	   printf("\nsin(%g)=%g\n", d, sin (d)); 
	} 

Output

Enter a double: 100

sin(100)=-0.506366


sinh

Header

math.h
fltenv.h

Prototype

double sinh(double x);
float sinhf(float x);
long double sinhl(long double x);

Description

The sinh functions calculate the hyperbolic sine of x. The sinh function operates on double values; sinhf operates on floating-point values; _sinhl and sinhl operate on long double values.

Synonym

Function: _sinhl

Return Value

The hyperbolic sine of x. If the value of x is too large, errno is set to ERANGE.

Special Results

value of x 		return value 		invalid?
±0.0 			±0.0 			no 
±INFINITY 		±INFINITY 		no 

Compatibility

DOS Windows 3.x Phar Lap DOSX Win32

See Also

cosh
sin

Example

	/* 	Example of sinh 
		Also demonstrates sinh 
	*/

	#include <stdlib.h>
	#include <stdio.h>
	#include <math.h>

	void main() 
	{ 
	   double d; 

	   printf("Enter a double: "); 
	   scanf("%lg", &d); 
	   printf("\nsinh(%g)=%g\n", d, sinh (d)); 
	} 
Output

Enter a double: 12

sinh(12)= 81377.4


sqrt

Header

math.h
fltenv.h (required for exception values)

Prototype

double sqrt(double x);
float sqrtf(float x);
long double sqrtl(long double x);

Description

The sqrt functions calculate the square root of x. The sqrt function operates on double values; sqrtf operates on floating-point values; _sqrtl operates on long double values.

Return Value

Function: _sqrtl

Special Results


value of x 		return value 	invalid?
-0.0 			-0.0 		no 
<0.0			NAN 		yes 
+INFINITY 		+INFINITY 	no 

Compatibility

DOS Windows 3.x Phar Lap DOSX Win32

See Also

exp
expml
pow Functionsow

Example

	/* 	Example of sqrt 
		Also demonstrates sqrtl 
	*/

	#include <stdlib.h>
	#include <stdio.h>
	#include <math.h>

	void main() 
	{ 
	   double d; 

	   printf("Enter a double: "); 
	   scanf("%lg", &d); 
	   printf("\nsqrt(%g)=%g\n", d, sqrt (d)); 
	} 
Output

Enter a double: 81

sqrt(81)= 9


tan

Header

math.h fltenv.h (required for exception values)

Prototype

double tan(double x);
float tanf(float x);
long double tanl(long double x);

Description

The tan functions calculate the tangent of x (measured in radians). The tan function operates on double values; tanf operates on floating-point values; tanl operates on long double values.

Synonym

Function: _tanl

Return Value

The tangent of x.

If x is too large, a partial loss of significance may occur. In this case, the tan functions set global variable errno to ERANGE and generate a _PLOSS error. If x is so large that significance is totally lost, tan functions print a _TLOSS error message to stderr, set errno to ERANGE, and return 0.

Special Results

value of x	return value	invalid?
±0.0 		±0.0 		no 
±INFINITY 	NAN 		yes 
Compatibility

DOS Windows 3.x Phar Lap DOSX Win32

See Also

acos
tanh

Example

	/* 	Example of tan 
		Also demonstrates tanl 
	
	*/
	#include <stdlib.h>
	#include <stdio.h>
	#include <math.h>

	void main() 
	{ 
	   double d; 

	   printf("Enter a double: "); 
	   scanf("%lg", &d); 
	   printf("\ntan(%g)=%g\n", d, tan (d)); 
	} 
Output

Enter a double: 100

tan(100)=-0.587214


tanh

Header

Prototype

double tanh(double x);
float tanhf(float x);
long double tanhl(long double x);

Description

The tanh functions calculate the hyperbolic tangent of x. The tanh function operates on double values; tanhf operates on floating-point values; tanhl operates on long double values.

Synonym

Function: _tanhl

Return Value

The hyperbolic tangent of x.

Special Results

value of x 	return value 	invalid?
±0.0 		±0.0 		no 
±INFINITY 	±1. 0 		no

Compatibility

DOS Windows 3.x Phar Lap DOSX Win32

See Also

acos
tan

Example

	/* 	Example of tanh 	*/

	#include <stdlib.h>
	#include <stdio.h>
	#include <math.h>

	void main() 
	{ 
	   double d; 

	   printf("Enter a double: "); 
	   scanf("%lg", &d); 
	   printf("\ntanh(%g)=%g\n", d, tanh(d)); 
	} 

Output

Enter a double: 3

tanh(3)= 0.995055 

_cabs, _cabsl

Header

math.h

Prototype

double _cabs(struct _complex z);
long double _cabsl(struct _complexl z);

Description

The _cabs and _cabsl functions calculate the absolute value of the complex number stored in the z structure. This structure is composed of a real component x and an imaginary component y. In the _cabs function, x and y are double values; in the _cabsl function, x and y are long double values.

Synonym

Function: cabs, cabl
Argument: complex, complexl

Return Value

The absolute value of z. If an overflow occurs, these functions return HUGE_VAL, and set variable errno to ERANGE.

Compatibility

DOS Windows 3.x Phar Lap DOSX Win32

See Also

abs
fabs

Example

	/* 	Example for cabs 	*/

	#include <math.h> 
	#include <stdio.h> 
	#include <stdlib.h>
 
	void main() 
	{
 
	   struct _complex z; 
	   double retval; 

	   z. x = 3.0; 
	   z. y = 1.0; 
	   retval = _cabs(z); 
	   printf("cabs of (%g,%gi) = %g", z. x, z. y, 
		     retval); 
	}
Output

cabs of (3,1i) = 3.16228


hypot

Header

math.h

Prototype

float hypotf(float x, float y);
double hypot(double x, double y);
long double hypotl(long double x, long double y);

Description

The hypot functions calculate the length of the hypotenuse of a right-angled triangle with sides of length x and y. The hypotenuse is the value of the square root of the sums of the squares of x and y:

	sqrt(x2 + y2)

Return Value

The hypotenuse of a right-angled triangle with sides x and y.

Special Results

x y return value invalid?
x +-0.0 fabs(x) no
+-INFINITY y +INFINITY no
+-INFINITY NAN +INFINITY no

Note that hypot(x,y), hypot(y,x) and hypot(x,-y) are equivalent.

Compatibility

ANSI C99, DOS, Windows 3.x, Phar Lap, DOSX, Win32

See Also

cosh
sinh
tanh
ANSI C99 7.12.7.3, F.9.4.3

Example

	/* 	Example for hypot */

	#include <stdio.h> 
	#include <math.h>

	void main() 
	{ 
	   double d1, d2; 

	   printf("Enter two doubles: "); 
	   scanf("%Lg %Lg", &d1, &d2); 
	   printf("\nhypot (%g, %g) = %g\n", d1, d2, 
		    hypot (d1, d2)); 
	} 
Output

Enter two doubles: 5 5
hypot (5, 5) = 7.07107


Copyright © 1995-2002 Digital Mars. All Rights Reserved.