Float to int conversion (using (int)) gives wrong answer ((int)2f = 1)

EDIT: Even doing (int)2f for me returns 1.

I was getting unexpected answers when a script was run and the x position of the object was at a certain number. So I have this debug here to show you that I made:

Debug.Log ("Before (int): " + Main.worldToGrid (transform.position.x, transform.position.y).x + 
                   "  After (int): " + (int)Main.worldToGrid (transform.position.x, transform.position.y).x);

I use the (int) version of that in my code and it turns out wrong sometimes. When Main.worldToGrid (transform.position.x, transform.position.y).x is 0, 1 or 3, the (int) is normal (0, 1, or 3). But when it’s 2, the (int) is 1 for some reason.

Look:

Before (int): 2  After (int): 1
UnityEngine.Debug:Log(Object)
Tile:Start() (at Assets/Scripts/Tile.cs:9)

Does anyone know why? I’m using C#, by the way.

We’d need to see your worldtogrid code but, seeing as you’re dealing with floating point mathematics, it seems likely that the value being returned is actually 1.99999999 (for example) which, when cast to int, is 1.

This is a subtle roundoff error in ToString.

The ToString function rounds numbers beyond a certain decimal place, wheres an int cast simply truncates.
The result is that when you are VERY close to 2, they will produce different values:

Here’s a test case.

float f=2F-(0.0000001f);
Debug.Log (f);
int i = (int)f;
Debug.Log(i);