Beruflich Dokumente
Kultur Dokumente
function
double long double float double long double pow pow pow pow pow ( double base, double exponent ); ( long double base, long double exponent ); ( float base, float exponent ); ( double base, int exponent ); ( long double base, int exponent );
Raise to power Returns base raised to the power exponent: baseexponent In C++, this function is overloaded in <complex> and <valarray> (see complex pow and valarray pow).
Parameters
base Floating point value. exponent Floating point value.
Return Value
The result of raising base to the power exponent. If the magnitude of the result is so large that it cannot be represented in an object of the return type, a range error occurs, returning HUGE_VAL with the appropiate sign and setting the value of the global variable errno to the ERANGE value. If base is negative and exponent is not an integral value, or if base is zero and exponent is negative, a domain error occurs, setting the global variable errno to the value EDOM.
Portability
In C, only the version taking two double parameters exists with this name. The other overloads are only available in C++.
Example
1 2 3 4 5 6 7 8 9 10 11 /* pow example */ #include <stdio.h> #include <math.h> int main { printf printf printf return } () ("7 ^ 3 = %lf\n", pow (7,3)); ("4.73 ^ 12 = %lf\n", pow (4.73,12)); ("32.01 ^ 1.54 = %lf\n", pow (32.01,1.54)); 0;
sqrt
function
<cmath> double sqrt ( double x ); float sqrt ( float x ); long double sqrt ( long double x );
Compute square root Returns the square root of x. In C++, this function is overloaded in <complex> and <valarray> (see complex sqrt and valarray sqrt).
Parameters
x Floating point value. If the argument is negative, a domain error occurs, setting the global variable errno to the value EDOM.
Return Value
Square root of x.
Portability
In C, only the
double
Example
1 2 3 4 5 6 7 8 9 10 11 12
/* sqrt example */ #include <stdio.h> #include <math.h> int main () { double param, result; param = 1024.0; result = sqrt (param); printf ("sqrt(%lf) = %lf\n", param, result ); return 0; }
Output:
sqrt(1024.000000) = 32.000000
#include <math.h>
double long float pow (double x, double y) powl (long double x, long double y) powf (float x, float y)
#include <complex.h>
double complex float complex long double complex cpow (double complex x, complex double y) cpowf (float complex x, complex float y) cpowl (long double complex x, complex long double y)
Description
The pow functions compute x raised to the power of y, i.e.
(1)
Example 1
#include <math.h> #include <stdio.h>
int main(void) { for(int i = 1; i < 5; i++) printf("pow(3.2, %d) = %lf\n", i, pow(3.2, i)); return 0; }
Output:
pow(3.2, 1) = 3.200000 pow(3.2, 2) = 10.240000 pow(3.2, 3) = 32.768000 pow(3.2, 4) = 104.857600
Special Values
pow ( 0, y ) returns and raises the divide-by-zero floating-point exception for y an odd integer. pow ( 0, y ) returns + and raises the divide-by-zero floating-point exception for y < 0 and not an odd integer. \n pow ( \a 0, \a y ) returns 0 for \a y an odd integer > 0. pow ( 0, y ) returns +0 for y > 0 and not an odd integer. pow ( -1, ) returns 1. pow ( 1, y ) returns 1 for any y, even a NaN.
pow ( x, 0 ) returns 1 for any x, even a NaN. pow ( x, y ) returns a NaN and raises the invalid floating-point exception for finite x < 0 and finite non-integer y. pow ( x, - ) returns + for |x| < 1. \n pow ( \a x, \a - ) returns +0 for |x| > 1. pow ( x, + ) returns +0 for |x| < 1. \n pow ( \a x, \a + ) returns + for |x| > 1. pow ( -, y ) returns -0 for y an odd integer < 0. \n pow ( \a -, \a y ) returns +0 for \a y < 0 and not an odd integer. \n pow ( \a -, \a y ) returns - for \a y an odd integer > 0. \n pow ( \a -, \a y ) returns + for \a y > 0 and not an odd integer. pow ( +, y ) returns +0 for y < 0. \n pow ( \a +, \a y ) returns + for \a y > 0. A domain error occurs if x is finite and negative and y is finite and not an integer. A domain error can occur if x is 0 and y less than or equal to 0. #include <math.h>
double long float sqrt (double x) sqrtl (long double x) sqrtf (float x)
#include <complex.h>
complex double complex float complex sqrt (complex x) csqrt (double complex x) csqrtf (float complex x)
Description
The sqrt function computes the non-negative square root of x, i.e sqrt(x) For complex numbers x, sqrt returns the complex root of x, using the following formula
Example 1
#include <stdio.h> #include <math.h>
int main() { double x = 1234321; double result = sqrt(x); printf("The square root of %.2lf is %.2lf\n", x, result); return 0; }
Ouput:
The square root of 1234321.00 is 1111.00
Special Values
sqrt ( -0 ) returns -0. sqrt ( x ) returns a NaN and generates a domain error for x < 0.
Standards
The sqrt function conforms to ISO/IEC 9899:1999(E).
Compatibility
DOS UNIX Windows ANSI C C++ only sqrtl Real sqrt Complex sqrt Complex csqrt Last Modified: 2009-11-01 08:42:53 Page Rendered:
2.7 math.h
The math header defines several mathematic functions. Macros:
HUGE_VAL
Functions:
acos(); asin(); atan(); atan2(); ceil(); cos(); cosh(); exp(); fabs(); floor(); fmod(); frexp(); ldexp(); log(); log10(); modf(); pow(); sin(); sinh(); sqrt(); tan(); tanh();
All math.h functions handle errors similarly. In the case that the argument passed to the function exceeds the range of that function, then the variable errno is set to EDOM. The value that the function returns is implementation specific. In the case that the value being returned is too large to be represented in a double, then the function returns the macro HUGE_VAL, and sets the variable errno toERANGE to represent an overflow. If the value is too small to be represented in a double, then the function returns zero. In this case whether or not errno is set toERANGE is implementation specific.
errno, EDOM,
Note that in all cases when it is stated that there is no range limit, it is implied that the value is limited by the minimum and maximum values of type double.
Declaration:
double acos(double x);
Returns the arc cosine of x in radians. Range: The value x must be within the range of -1 to +1 (inclusive). The returned value is in the range of 0 to pi (inclusive).
2.7.2.2 asin
Declaration:
double asin(double x);
Returns the arc sine of x in radians. Range: The value of x must be within the range of -1 to +1 (inclusive). The returned value is in the range of -p/2 to +p/2 (inclusive).
2.7.2.3 atan Declaration:
double atan(double x);
Returns the arc tangent of x in radians. Range: The value of x has no range. The returned value is in the range of -p/2 to +p/2 (inclusive).
Returns the arc tangent in radians of y/x based on the signs of both values to determine the correct quadrant. Range: Both y and x cannot be zero. The returned value is in the range of -p/2 to +p/2 (inclusive).
2.7.2.5 cos Declaration:
double cos(double x);
Returns the cosine of a radian angle x. Range: The value of x has no range. The returned value is in the range of -1 to +1 (inclusive).
2.7.2.6 cosh Declaration:
double cosh(double x);
Returns the hyperbolic cosine of x. Range: There is no range limit on the argument or return value.
2.7.2.7 sin Declaration:
double sin(double x);
Range: The value of x has no range. The returned value is in the range of -1 to +1 (inclusive).
2.7.2.8 sinh Declaration:
double sinh(double x);
Returns the hyperbolic sine of x. Range: There is no range limit on the argument or return value.
2.7.2.9 tan Declaration:
double tan(double x);
Returns the tangent of a radian angle x. Range: There is no range limit on the argument or return value.
2.7.2.10 tanh Declaration:
double tanh(double x);
Returns the hyperbolic tangent of x. Range: The value of x has no range. The returned value is in the range of -1 to +1 (inclusive).
2.7.3 Exponential, Logarithmic, and Power Functions 2.7.3.1 exp Declaration:
Returns the value of e raised to the xth power. Range: There is no range limit on the argument or return value.
2.7.3.2 frexp Declaration:
double frexp(double x, int *exponent);
The floating-point number x is broken up into a mantissa and exponent. The returned value is the mantissa and the integer pointed to by exponent is the exponent. The resultant value is x=mantissa * 2^exponent. Range: The mantissa is in the range of .5 (inclusive) to 1 (exclusive).
2.7.3.3 ldexp Declaration:
double ldexp(double x, int
exponent);
Returns the common logarithm (base-10 logarithm) of x. Range: There is no range limit on the argument or return value.
2.7.3.6 modf Declaration:
double modf(double x, double *integer);
Breaks the floating-point number x into integer and fraction components. The returned value is the fraction component (part after the decimal), and sets integer to the integer component. Range: There is no range limit on the argument or return value.
2.7.3.7 pow Declaration:
double pow(double x, double y);
Returns x raised to the power of y. Range: x cannot be negative if y is a fractional value. x cannot be zero if y is less than or equal to zero.
2.7.3.8 sqrt Declaration:
double sqrt(double x);
Returns the square root of x. Range: The argument cannot be negative. The returned value is always positive.
2.7.4 Other Math Functions 2.7.4.1 ceil Declaration:
double ceil(double x);
Returns the smallest integer value greater than or equal to x. Range: There is no range limit on the argument or return value.
2.7.4.2 fabs Declaration:
double fabs(double x);
Returns the absolute value of x (a negative value becomes positive, positive value is unchanged). Range: There is no range limit on the argument. The return value is always positive.
2.7.4.3 floor Declaration:
double floor(double x);
Returns the largest integer value less than or equal to x. Range: There is no range limit on the argument or return value.
Returns the remainder of x divided by y. Range: There is no range limit on the return value. If y is zero, then either a range error will occur or the function will return zero (implementation-defined).
19.66.1.sqrt
Item Value Header file math.h float sqrtf(float num); Declaration double sqrt(double num); long double sqrtl(long double num); Return returns the square root of num.
#include <math.h> #include <stdio.h> int main(void) { printf("%f", sqrt(16.0)); }
19.56.1.pow
Item Value Header file math.h float powf(float base, float exp); Declaration double pow(double base, double exp); long double powl(long double base, long double exp); Return returns base raised to the exp power (base^exp).
#include <math.h> #include <stdio.h> int main(void)
#include <math.h>, syntax at the beginning of our code, means we automatically included these (pre-defined) functions in our program: int abs (int x); long labs (long x); double double double double double double double double double double fabs (double x); sin (double x); cos (double x); tan (double x); asin (double x); acos (double x); atan (double x); sinh (double x); cosh (double x); tanh (double x);
ex ln x log x xy IxI
double exp (double x); double log (double x); double log10 (double x); double pow (double x,double y); double sqrt(double x); double fmod(double x, double y); double ceil (double x); double floor(double x);
sqare root of x
x mod y