# 5.3.4f1 comparison confusion

I apologize if this is a known issue or if I’m just being stupid but while doing some algebra to simplify some of the equations I’m using, I came across some odd inconsistencies which are shown below

``````int r = 10;
Debug.Log(r * 0.5f - r * 0.04f == r * 0.46f); // false
Debug.Log((r * 1 / 2) - (r * 4 / 100) == (r * 46 / 100)); // false
Debug.Log(r * (0.5f - 0.04f) == r * 0.46f); // true
Debug.Log(10 * 0.5f - 10 * 0.04f == 10 * 0.46f); // true
``````

Hopefully I’m just overlooking something simple but to me, all 4 of these are equivalent unless I’m not accounting for rounding errors somewhere. The biggest issue and most confusing this is that when each part is broken into variables and their types compared, they are both System.Single’s and all result in 4.6.

So my question is why do the first 2 comparisons result in false while the second two return true? Especially the first and last. I’m using 5.3.4f1

In the first line, you should nerver use == to compare floats, you will have precision problems, use Mathf.Approximately()

``````Debug.Log(Mathf.Approximately(r * 0.5f - r * 0.04f , r * 0.46f)); // true
``````

In the second line you are using int arithmetic, there are no floats.

``````(10 * 1 / 2) - (10 * 4 / 100)  = 5 - 0 = 5
(10 * 46 / 100) = 4
``````

And 5 != 4

The third and fourth lines are correct, but you should better use Mathf.Approximately()