I have the following c program, and some of the times I run it, the output differs, based on the compiler and the platform. I understand that double to int conversion could cause problems.
Here is the code:
//Compiler version gcc 6.3.0
#include <stdio.h>
#include <math.h>
int main(void){
double d = 2;
printf("%.20lf\n", pow(10, d));
printf("%d\n", (int)pow(10, d));
printf("%d\n", (int)pow(10, 2));
}
100 is the expected value, but the statement
printf("%d\n", (int)pow(10, d));
has 99 as output when I use both gcc 6.3.0 and Windows 10 x64, but not in other cases.
Here are some results:
//gcc 6.3.0 (Sublime Text 3) in Windows 10 x64
100.00000000000000000000
99 ->this is the problem
100
//gcc 6.3.0 in Android (using Dcoder app)
100.00000000000000000000
100
100
//MSVC(VS 2017 x86) in Windows 10 x64
100.00000000000000000000
100
100
I also tested some online gcc(6.3.0) compilers but all the outputs were 100.
Thanks for the help.