I just tried three pieces of code:
#include<cstdio>
#include<cmath>
#include<cstring>
int main()
{
int a = 3;
int b = pow(10,a);
printf("%d",b);
return 0;
}
//Output:1000
#include<cstdio>
#include<cmath>
#include<cstring>
int main()
{
int a = 3;
int b = pow(10,a-1);
printf("%d",b);
return 0;
}
//Output:99
#include<cstdio>
#include<cmath>
#include<cstring>
int main()
{
int a = 3;
int b = pow(10,a-2);
printf("%d",b);
return 0;
}
//Output:10
I would like to know why the second block of code will output 99, is it because of floating point precision? Or is it because I should use float numbers in the pow function?(Such as 10.0) I'm usually confused about the accuracy of C++, I will be grateful for your help.