Here is my program:
#include <stdio.h>
int main()
{
printf("%g\n", 100000.5);
printf("%g\n", 100001.5);
printf("%g\n", 100002.5);
printf("%g\n", 100003.5);
printf("%g\n", 100004.5);
printf("%g\n", 100005.5);
return 0;
}
I am getting this output using gcc:
100000
100002
100002
100004
100004
100006
In VS2019, I get different output which looks more consistent:
100001
100002
100003
100004
100005
100006
Why does the output differ? Is it about compiler?
Yes, IEEE 754 defines four possible rounding modes, quoted from the gcc man pages:
Therefore gcc uses "Round to nearest" while Visual Studio uses "Round toward plus Infinity"
The good news is that you can change the mode:
In your case: