Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

C Library Function - pow()

C Library Function - pow()

C Standard Library - <math.h> C Standard Library -

Description

C Library Function double pow(double x, double y) Return x 's y power, i.e., xy.

pow() is a function in the C standard library a function in the C standard library used to compute the power of a number. Specifically, it returns the first argument raised to the power of the second argument, i.e., x^y.

Declaration

The following is the declaration of the pow() function.



#include 



double pow(double x, double y);

float powf(float x, float y);

long double powl(long double x, long double y);

Parameters

Return Value

Error handling

Example

The following example demonstrates the use of the pow() function.

Example

#include

#include

int

main

(

)

{

   

printf

(

"value 8.0 ^ 3 = %lf\n"

,

pow

(

8.0

,

3

)

)

;

   

printf

(

"value 3.05 ^ 1.98 = %lf"

,

pow

(

3.05

,

1.98

)

)

;

   

   

return

(

0

)

;

}

Let's compile and run the above program, which will produce the following result:



值 8.0 ^ 3 = 512.000000

值 3.05 ^ 1.98 = 9.097324

Handling multiple power calculations

The following example shows how to handle power calculations for multiple bases and exponents:

Example

#include

#include

#include

int

main

(

)

{

   

double

bases

[

]

=

{

2.0

,

2.0

,

0.0

,

2.0

}

;

   

double

exponents

[

]

=

{

3.0

,

3.0

,

0.0

,

2.0

}

;

   

int

num_values

=

sizeof

(

bases

)

/

sizeof

(

bases

[

0

]

)

;

   

for

(

int

i

=

0

;

i

<

num_values

;

i

++

)

{

       

double

x

=

bases

[

i

]

;

       

double

y

=

exponents

[

i

]

;

        errno

=

0

;

 

// Reset errno

       

double

result

=

pow

(

x

,

y

)

;

       

if

(

errno

==

EDOM

)

{

           

printf

(

"Domain error: pow(%f, %f) is not defined\n"

,

x

,

y

)

;

       

}

else

if

(

errno

==

ERANGE

)

{

           

if

(

result

==

HUGE_VAL

||

result

==

HUGE_VAL

)

{

               

printf

(

"Range error: pow(%f, %f) results in overflow\n"

,

x

,

y

)

;

           

}

else

{

               

printf

(

"Range error: pow(%f, %f) results in underflow\n"

,

x

,

y

)

;

           

}

       

}

else

{

           

printf

(

"pow(%f, %f) = %f\n"

,

x

,

y

,

result

)

;

       

}

   

}

   

return

0

;

}

Let's compile and run the above program, which will produce the following result:

pow(2.000000, 3.000000) = 8.000000

pow(-2.000000, 3.000000) = -8.000000

pow(0.000000, 0.000000) = 1.000000

pow(2.000000, -2.000000) = 0.250000

Code Analysis

Use Cases

pow() The function has a wide range of uses in many applications, including but not limited to:

Summary

pow() The function is used to compute the power of a floating-point number and is an important tool for handling exponentiation. Through proper use of pow(), exponentiation can be achieved in scientific computing, engineering applications, and financial analysis, and possible error conditions can be handled properly.

C Standard Library - <math.h> C Standard Library -