PRINTF doesn't correctly handle +INF or -INF
Code to reproduce:
float val = -1.0f / 0.0f; // Should be -inf, instead -2146435072 is printed
if (isnan(val)) {
PRINTF("Catched NAN %f\n", val);
} else {
if (isinf(val)) {
PRINTF("Catched INF %f\n", val);
} else {
PRINTF("Normal number: %f\n", val);
}
}
The isinf
check catches the expression, but PRINTF doesn't output -inf
as expected. Instead -2146435072 is printed.
According to IEEE-754 a number is infinite if every bit of the exponent is set and the mantissa is zero. As such we expect the byte value to be 0x7f800000 (sign bit: off, exponent: all set, mantissa: none set)
Than, the bytes are interpreted according to float_val = (sign ? -1:1) * 2^(exponent) * 1.(mantissa bits) = - 2^32 * 1.0 = -2146435072
This can cause -a lot- of headaches.
Additionally, these expressions are printed incorrectly:
1/0 = ∞ (printf: -1.11111...)
log (0) = -∞ (printf: -2146435072)
The error is in rodos/src/independent/yprintf.cpp
, line 184: Though nan is catched via an inequality check (if val != val: nan
) there is no mechanism to correctly handle inf.