Float precision error in C++

Hello,
I have a problem when doing math with floats - it seems like brackets change the output, even when they shouldn’t.
For example:

MaxHealth = 100.0, percentToAdd = 10.0

This equation will result in 10.0, as it should be.

271119-53526712-258148695088270-2244298142201675776-n.png

While this one will result in 9.9998

271120-53613622-255249155357813-6070785369220055040-n.png

I have no idea how this is happening but I can only reproduce it in UE4 and not in other C++ programs.

My guess is that it’s because it’s performing a behind the scenes truncating operation at different points in time. In the first example it would do it on the entire equation while in the second example it happens in the parentheses then again on the result. UE4 does some stuff behind the scenes which has thrown me for a loop from time to time as well.

Personally I would just use an integer to represent health and then use FloorToInt() or RoundToFloat() on all operations if the floating point isn’t important which in most cases it isn’t.

FMath stuff here: