Given two numbers base and exponent, pow() function finds x raised to the power of y i.e. xy. Basically in C exponent value is calculated using the pow() function.
Input: 2.0, 5.0 Output: 32 Explanation: pow(2.0, 5.0) executes 2.0 raised to the power 5.0, which equals 32 Input: 5.0, 2.0 Output: 25 Explanation: pow(5.0, 2.0) executes 5.0 raised to the power 2.0, which equals 25
double pow(double x, double y);
Parameters: The method takes two arguments:
- x : floating point base value
- y : floating point power value
Working of pow() function with integers
The pow() function takes ‘double’ as the arguments and returns a ‘double’ value. This function does not always work for integers. One such example is pow(5, 2). When assigned to an integer, it outputs 24 on some compilers and works fine for some other compilers. But pow(5, 2) without any assignment to an integer outputs 25.
- This is because 52 (i.e. 25) might be stored as 24.9999999 or 25.0000000001 because the return type is double. When assigned to int, 25.0000000001 becomes 25 but 24.9999999 will give output 24.
- To overcome this and output the accurate answer in integer format, we can add 0.5 to the result and typecast it to int e.g (int)(pow(5, 2)+0.5) will give the correct answer(25, in above example), irrespective of the compiler.
This article is contributed by Arushi Dhamija and Jatin Goyal. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Function Overloading vs Function Overriding in C++
- How to call function within function in C or C++
- Difference between Virtual function and Pure virtual function in C++
- What happens when a virtual function is called inside a non-virtual function in C++
- div() function in C++
- exp() function C++
- log() function in C++
- arc function in C
- fma() function in C++
- wcsrchr() function in C/C++
- putchar() function in C
- mbrtowc() function in C/C++
- c16rtomb() function in C/C++
- c32rtomb() function in C/C++
- mbsinit() Function in C/C++
- bit_and function in C++
- wcspbrk() function in C/C++
- strcmpi() function in C
- mbrlen() function in C/C++
- fegetexceptflag() function in C/C++