-2073741714.0
is not typically exactly representable as a float
. Use wider types or accept imprecision. float
as a 32-bit variable can not exactly encode every number, just about 232 of them. The typical closest one is -2073741696
float b4 = -2073741714.0;
printf("b4 = %f\n", b4);
// typical output
// b4 = -2073741696.000000
Instead consider either wider FP types.
int main(void) {
double b1 = 23.0;
double b2 = 42.0;
double b3 = 2073741824.0;
double b4 = -2073741714.0;
double b5 = -64.0;
double b6 = -111.0;
double be;
printf("b4 = %f\n", b4);
be = b1 + b2 + (b3 + b4) + b5 + b6;
printf("Das Float Ergebnis ist: %f\n", be); // Das Float Ergebnis ist: 0.000000
return (EXIT_SUCCESS);
}
Or use wider math
int main(void) {
float b1 = 23.0;
float b2 = 42.0;
float b3 = 2073741824.0;
double b4 = -2073741714.0;
float b5 = -64.0;
float b6 = -111.0;
float be;
printf("b4 = %f\n", b4);
be = b1 + b2 + (b3 + b4) + b5 + b6;
printf("Das Float Ergebnis ist: %f\n", be); // Das Float Ergebnis ist: 0.000000
return (EXIT_SUCCESS);
}
A 3rd choice is to re-arrange the evaluation order. This will help, yet not overcome limitations of float
.
int main(void) {
float b1 = 23.0;
float b2 = 42.0;
float b3 = 2073741824.0;
float b4 = -2073741714.0;
float b5 = -64.0;
float b6 = -111.0;
float be;
be = b1 + b2 + (b3 + b4) + b5 + b6;
printf("Das Float Ergebnis ist: %f\n", be); // Das Float Ergebnis ist: 18.000000
return (EXIT_SUCCESS);
}