I was implementing calculate mean and stddev for unsigned char type array (say, gray image). To store the sum and average, I use float and double type and get different result. Now I simplify this issue, with minimal reproducing code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(){
#define BASE 16777306
#if 0
float a = BASE;
unsigned char b = 179;
float c = a + b; // float sum
printf("float type, a=%f, (float)b=%f, c=%f, a+b=%d\n", a, (float)b, c, BASE+b);
#else
double a = BASE;
unsigned char b = 179;
double c = a + b; // double sum
printf("double type, a=%lf, (double)b=%lf, c=%lf, a+b=%d\n", a, (double)b, c, BASE+b);
#endif
return 0;
}
Result:
double type, a=16777306.000000, (double)b=179.000000, c=16777485.000000, a+b=16777485
float type, a=16777306.000000, (float)b=179.000000, c=16777484.000000, a+b=16777485
As shown above, 16777484.000000
generated from float type, is 1 less than 16777485.000000
(the correct one).
Why using float type for sum leading to wrong result? While double type remains correct as int type?