Home  >  Article  >  Backend Development  >  C program to write x raised to the yth power

C program to write x raised to the yth power

藏色散人
藏色散人Original
2021-07-16 14:25:3222119browse

In the C program, you can write the y-th power of x through the pow() function. The function of pow() is to calculate the y-th power value with x as the base. Its syntax is "double pow(double x, double y);".

C program to write x raised to the yth power

The operating environment of this tutorial: Windows 7 system, version C11, Dell G3 computer.

How to write x raised to the yth power in C program?

C language pow() function: find x raised to the yth power (power)

Header file: #include 15dfdae10d7c0c52b8c9b87fff3f31d0

pow() function is used to find the y power (power) of x. Its prototype is:

double pow(double x, double y);

pow() to calculate the value raised to the yth power with x as the base, and then return the result. Assume the return value is ret, then ret = xy.

Potential error situations:

If the base x is a negative number and the exponent y is not an integer, a domain error will occur.

If the base x and the exponent y are both 0, a domain error may or may not occur; this is related to the implementation of the library.

If the base x is 0 and the exponent y is a negative number, it may cause domain error or pole error, or it may not; this depends on the implementation of the library.

If the return value ret is too large or too small, a range error will occur.

Error code:

If a domain error occurs, then the global variable errno will be set to EDOM;

If a pole error or range error occurs, then the global variable errno will be set to ERANGE.

Note, please add -lm when compiling with GCC.

[Example] Please see the code below.

#include <stdio.h>
#include <math.h>
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

Recommendation: "c language video tutorial"

The above is the detailed content of C program to write x raised to the yth power. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn