Pow function does not work as expected in Code :: Blocks IDE
I have a question from my junior and I cannot fix it. Below is the code it uses in the Code :: Blocks IDE, just downloaded from the official Code :: Blocks site .
This is the hello world console console project, which he only modified slightly using a header file math.h
and using a pow()
function.
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main()
{
printf("Hello world! %d\n",pow(2,2));
return 0;
}
Should the output of this code be Hello world! 4
right? But voila, itβs always Hello world! 0
, if I donβt use printf("Hello world! %f\n", pow(2,2));
, which syntactically is, yes, the perfect and correct thing to do. But then that's a completely different story.
The Pow function should return 4 double
, of course. So what's going on? printf()
does not work correctly, or there is a problem with pow()
.
source to share
printf("Hello world! %d\n",pow(2,2));
"The result of this code should be Hello world! 4 correct?"
Not. Since this behavior is undefined, anything can happen. Since @Mureinik posted what is likely to happen in this itinerant situation, you could understand why you saw 0. But in the end, C shouldn't be doing that way.
... If any argument is not the correct type for the corresponding conversion specification, the behavior is undefined .. C11 Β§7.21.6.1 9
Casting to is (int)
also a problem.
1) the int
range is much less than double
, and for pow(2,100)
that fails.
2) Casting does not int
truncate the fractional part by the result double
, and the accuracy is pow()
not defined. This will give an amazing result when pow()
sort of 7.99999999999999
and 7
printed rather than hoping for 8.000000
.
If your code needs a cardinality integer function, consider unsigned long long int pow or fooobar.com/questions/141070 / ... or search around.
source to share
The return value pow()
is double
, as you can see here:
http://www.tutorialspoint.com/c_standard_library/c_function_pow.htm
So, you have to cast the return value to int like this:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main()
{
printf("Hello world! %d\n",(int)pow(2,2));
return 0;
}
Otherwise, as you've seen, it's output 0!
Another example to show this can be tried:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main()
{
printf("Hello world! %d\n",4.000000);
return 0;
}
As you will see, the output is also 0 because it is a double value!
source to share
As you noted, it pow
returns double. printf
however is unaware of the types passed to it on the stack, and if you pass double
where expected int
, it will just take the first sizeof(int)
bytes from the stack and interpret them as int
. For example, on my machine (gcc (GCC) 4.4.7 20120313 (Red Hat 4.4.7-4) on x86_64), this program printed Hello world! -1954378952
.
If you want to treat the result as int
using %d
, you must explicitly specify it as such:
printf("Hello world! %d\n", (int)pow(2,2));
source to share
pow(2,2)
will always return a double character.
The printf()
% d% and I both used to print int.Esli you use% d or% i to double, it would create problems. To display the double use of% f or% lf you will get the correct answer
printf("Hello world! %f\n",pow(2,2));
or
printf("Hello world! %lf\n",pow(2,2));
source to share