C language pow() function: find x to the power of y (power)
Header file:
#include <>
The pow() function is used to find the y power (to the power) of x, and its prototype is:
double pow(double x, double y);
pow() is used to calculate the y power value with x and then return the result. Suppose the return value is ret, then ret = xy.
Situations that may lead to errors:
- If the base x is negative and the exponent y is not an integer, a domain error error will be caused.
- If the base x and the exponent y are both 0, it may cause a domain error error or may not; this is related to the implementation of the library.
- If the base x is 0 and the exponent y is negative, it may cause domain error or pole error error, or it may not be; this is related to the implementation of the library.
- If the return value ret is too large or too small, it will cause a range error error.
Error code:
- If a domain error error occurs, the global variable errno will be set to EDOM;
- If a pole error or range error error occurs, the global variable errno will be set to ERANGE.
Note that when compiling using GCC, please add -lm.
[Example] Please see the code below.
#include <> #include <> int main () { printf ("7 ^ 3 = %f\n", pow (7.0, 3.0) ); printf ("4.73 ^ 12 = %f\n", pow (4.73, 12.0) ); printf ("32.01 ^ 1.54 = %f\n", pow (32.01, 1.54) ); return 0; }
Output result:
7 ^ 3 = 343.000000 4.73 ^ 12 = 125410439.217423 32.01 ^ 1.54 = 208.036691
C language sqrt() function: find the square root of a given value
Header file:
#include <>
sqrt() is used to find the square root of a given value, and its prototype is:
double sqrt(double x);
[Parameter] x is the value to calculate the square root.
If x < 0, it will cause a domain error error and set the value of the global variable errno to EDOM.
【Return value】Returns x square root.
Note that when compiling using GCC, please add -lm.
[The example calculates the square root value of 200. 】
#include <> main(){ double root; root = sqrt(200); printf("answer is %f\n", root); }
Output result:
answer is 14.142136