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.

Thanks! It worked. Why does (int) not work though? Any ideas?

2 Answers

2

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.

worldtogrid gives back 2f (when certain settings are present). Exactly 2f. I tried this piece of code: Debug.Log((int)2f); And it gives off 1, even though I put 2f, not 1.999999999f. Weird...

That doesn't happen on my computer. Are you SURE you are running the code you think you are?

This is weird. Yesterday that was happening but now, (int)2f just gives me 2...

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);