Answer the question
In order to leave comments, you need to log in
Why doesn't float overflow?
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <conio.h>
#include <float.h>
int main(void)
{
float value = FLT_MAX * 1.0000001f; // FLT_MAX + 1.0f
printf("%e", value);
_getch();
return 0;
}
Answer the question
In order to leave comments, you need to log in
The magic of floating points :)
Numbers should be approximately the same order, otherwise UB (with reservations).
3.4e38 + 1.0 = 3.4e38. This is equivalent to 3.4e38 + 0.0, because 1 compared to 1e38 is zero.
And when multiplying, the difference will be already in the 7th decimal place, which float can detect and report overflow (+INF).
Explain why this is so, multiplication overflows, and addition seems to simply cut off extra bits.
Imagine we have decimal arithmetic with three significant digits. Accordingly, addition works with no more than five digits: three actually significant ones, on the left for carry and on the right for rounding. What happened, in any case, will be rounded up to three.
99900000000000
+ 1,00
-----------------
9990 → 9,99e13
99900000000000
+ 50000000000
-----------------
9995 → 1,00e14 → переполнение
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question