If you know beforehand which operands can be small, you can reorder the operations:
-b + 1 + a
This way, the part before a
will be calculated first, and both sides (-b+1
and a
) will be roughly of the same magnitude, causing less floating-point problems.
Here's a relevant section of The Java™ Tutorials:
Operators on the same line have equal precedence. When operators of equal precedence appear in the same expression, a rule must govern which is evaluated first. All binary operators except for the assignment operators are evaluated from left to right;