Sometimes if I do:
float num1 = 0.1f;
float num2 = 0.1f;
float result = num1 + num2;
Debug.Log(result);
The result is 0.2000001
Why?
Sometimes if I do:
float num1 = 0.1f;
float num2 = 0.1f;
float result = num1 + num2;
Debug.Log(result);
The result is 0.2000001
Why?
because it’s a floating point number, it’s never gonna be entirely accurate.
The way they are encoded means that not all numbers may be perfectly represented, and that will get you small, incremental errors, just as you’ve noticed.
I advise taking the time to read through Floating-point arithmetic - Wikipedia for a detailled explanation, but the takeaway is this:
You can use Mathf.Approximately to compare equality on floating point numbers.