I was really interested in the explanations and made some tests...
O_o
Your test is flawed. Your results are irrelevant.
The values `9.995' and `9.985' do not exist in IEEE754 binary, arguably the most common, representation.
Code:
#include <stdio.h>
void Dump
(
float f
)
{
char s[16];
int c = 2;
do {
sprintf(s, "%%.%df\n", c);
printf(s, f);
} while(c++ < 9);
}
int main()
{
float s;
while(1 == scanf("%f", &s))
{
Dump(s);
}
return(0);
}
Code:
10.015
10.005
9.995
9.985
9.975
Code:
10.02
10.015
10.0150
10.01500
10.015000
10.0150003
10.01500034
10.015000343
10.01
10.005
10.0050
10.00500
10.005000
10.0050001
10.00500011
10.005000114
9.99
9.995
9.9950
9.99500
9.995000
9.9949999
9.99499989
9.994999886
9.98
9.985
9.9850
9.98500
9.985000
9.9849997
9.98499966
9.984999657
9.98
9.975
9.9750
9.97500
9.975000
9.9750004
9.97500038
9.975000381
look to me 5 is being rounded down no matter what...
o_O
You do not have a five.
You have a five and a bit, or you have a four and a lot.
If you want to see tie-breaking applied, you must use values which can be exactly represented.
Code:
#include <stdio.h>
int main()
{
printf("%.4f\n", 1.40625f); // down
printf("%.5f\n", 1.421875f); // up
printf("%.5f\n", 1.453125f); // down
printf("%.6f\n", 1.4609375f); // up
return(0);
}
Strangely enough, the example I've provided may also fail to show tie-breaking behavior because not all compilers have enough precision to accurately encode values of the relevant precision.
Soma