- Home /
if(12f > 12f) = true?
Hi! I am making a Character controller. I am almost finished, I just needed to fix a bug using the following if statement:
if(GetColliderLimit(Vector2.up) > yLimitTop) {
Debug.Log(GetColliderLimit(Vector2.up) + " > " + yLimitTop + " = " + (GetColliderLimit(Vector2.up) > yLimitTop));
//DO THINGS TO FIX THE BUG HERE
}
However, there is a case when it enters the conditional when it shouldn't. That is when both numbers are 12.
As far as I know, 12 > 12 is not True. Please help! What am I doing wrong? If this is a floating point error, how can I print the full float number (not rounded)?
I made A LOT of Math like this in my script, and this is the only time I got an error like this.
Thank you in advance.
Answer by CraigGraff · Jul 24, 2017 at 01:09 AM
It is almost certainly a floating point error. 12.000001 will appear as 12, but still register as greater than 12.
To fix this, you could rearrange your if condition to use Mathf.Approximately. Something like this:
if(GetColliderLimit(Vector2.up) > yLimitTop && !Mathf.Approximately(GetColliderLimit(Vector2.up), yLimitTop)){
@DavidRochin Edited to working code.
I didnt realize you edited the code and I made a comment mess haha. It worked! Thank you very much!
Answer by toddisarockstar · Jul 24, 2017 at 12:58 AM
hmmm. i know "Print" works for accurate councel output with floats. Floats print with the decimals. Vector2 and 3 do NOT. hope this helps
Vector2 v22 = new Vector2 (5.45433f, 6.985495f);
print (v22.x+"/"+v22.y);
Your answer
Follow this Question
Related Questions
subtracting numbers gives a very wrong answer 0 Answers
Get the two highest floats in a genericList without using .Sort()? 1 Answer
how to use a float in a GUI.TextField? 6 Answers
Javascript int to float 1 Answer
changing floats by input C# 1 Answer