Because precision for a single-precision (32 bit) floating-point value is around 7 digits after the decimal point. Which means the value you are adding is essentially zero, at least when added to 1
. The value itself, however, can effortlessly stored in a float since the exponent is small in that case. But to successfully add it to 1
you have to use the exponent of the larger number ... and then the digits after the zeroes disappear in rounding.
You can use double
if you need more precision. Performance-wise this shouldn't make a difference on today's hardware and memory is often also not as constrained that you have to think about every single variable.
EDIT: As you stated that using double
is not an option you could try to sort the values you are adding beforehand (assuming you are trying to calculate a sum). Starting to sum up the small values ensures that you won't get that large rounding errors when you're done in the end. But still, it can happen.
Another option may be to perform intermediary calculations in double-precision and afterwards cast to float
again. This will only help, however, when there are a few more operations than just adding a very small number to a larger one.