- Home /
Question by
MountDoomTeam · Mar 06, 2013 at 08:36 PM ·
errordivision
strange maths division error-unityscript
if I write --- var size1 : float= 1/(44-1);
it's equals 0
var size1 : float = 1/(size -1);
if ( x ==20 ){ Debug.Log( "size "+ size + " divider " + size1*1000 );}
prints this:
size 44 divider 0
Comment
Best Answer
Answer by OperationDogBird · Mar 06, 2013 at 08:47 PM
It looks like integer math is taking place. If you make either number a float first you will get the proper output.
var size1:float = 1.0/(size - 1);
if(x==20)Debug.Log( "size "+ size + " divider " + size1*1000 );
Answer by Eric5h5 · Mar 06, 2013 at 08:44 PM
It's not an error, it's expected behavior because you're using integer division. Making the variable a float doesn't cause Unity use float division when all of the numbers involved in the division are integers. So you need to make at least one of the numbers a float.