After one hour of trying to find a bug in my code I’ve finally found the reason. I was trying to add a very small float to 1f, but nothing was happening. While trying to figure out why I found that adding that small float to 0f worked perfectly.
Why is this happening? Does this have to do with ‘orders of magnitude’? Is there any workaround to this problem?
Thanks in advance.
Edit:
Changing to double precision or decimal is not an option at the moment.
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 to1you have to use the exponent of the larger number … and then the digits after the zeroes disappear in rounding.You can use
doubleif 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
doubleis not an option you could use Kahan summation, as akuhn pointed out in a comment.Another option may be to perform intermediary calculations in double-precision and afterwards cast to
floatagain. This will only help, however, when there are a few more operations than just adding a very small number to a larger one.