- Home /
Incorrect Division and Remainder
Hello,
I am using the following code and looking at the results in the inspector at runtime:
var number = 0.0;
var quotient = 0.0;
var remainder = 0.0;
function Update () {
quotient = Mathf.Floor(number / 12);
remainder = number % 12;
}
It should divide the varable 'number' by 12 giving the 'quotient' rounded down to the nearest whole number, and the 'remainder'.
But, if I set 'number' to equal 12.99, the remainder given is 0.9899998, instead of 0.99.
Can anyone see where i am going wrong?
Any help is appreciated.
Answer by _dns_ · Mar 31, 2015 at 01:52 PM
Hi, I guess this is a float precision issue. Here is a good article about it here: http://www.gamasutra.com/view/feature/1965/visualizing_floats.php?print=1
In your case, your may be able to increase precision using: remainder = number - (quotient * 12). You could also try using double precision float, it has the same behavior as regular floats but with greater accuracy.
Thanks. 'remainder = number - (quotient * 12)' makes no difference. I will probably just round it to 2 or 3 decimal places, as that gives the right answer to a good enough accuracy.
Answer by scythe75 · Mar 31, 2015 at 02:18 PM
you can´t divide 0 I think that this is the error.
I am not dividing by zero as I change the values in the inspector and set 'number' to 12.99.