I make a simple code to show my problem. I have to increment a variable in 0.2f, but sometimes increases with another presision.
I don`t understand why it happens. Here is my code:
public class Test : MonoBehaviour {
public static float areaDeMovimiento = 4.0f;//2m
public static int cantCubitos = 20;
private static float size = areaDeMovimiento/cantCubitos;
// Use this for initialization
void Start () {
for(float i = 0; i < areaDeMovimiento; i += size)
{
print(i + "---" + size);
for(float j = 0; j < areaDeMovimiento; j+= size)
{
//:S
}
}
}
// Update is called once per frame
void Update () {
}
}
And heres is my output:

If you only require a single digit of precision, it’s best to work in Integers, and divide by 10.
Thanks JTBBentley, it’s a good idea but i need float or double precision.
I changed all float by doubles and I found the same error:
Here is my code:
public class Test : MonoBehaviour {
public static double areaDeMovimiento = 4d;//2m
public static int cantCubitos = 20;
private static double size = areaDeMovimiento/cantCubitos;
// Use this for initialization
void Start () {
for(double i = 0; i < areaDeMovimiento; i += size)
{
print("I:" + i + " --- A:" + (areaDeMovimiento*0.5d)+ " --- R"
+ (i-(areaDeMovimiento*0.5d)) + "---" + size);
}
}
// Update is called once per frame
void Update () {
}
}
And heres is my output: http://i41.tinypic.com/sxg9d5.png
125
125