- Home /
Mathf.CeilToInt I do not understand it ?
hey, i have this line in my code:
holding[1] = Mathf.CeilToInt( (slots[counter][1]) / 2 );
basicly, if "slots[counter][1]" is a odd number, like 25, its rounded down 12, not up 13...
Have i misunderstood how "Mathf.CeilToInt" works?
Could you add this to your code before calling CeilToInt
Debug.Log("Calling CeilToInt on: " + (slots[counter][1] / 2));
Then post the result where CeilToInt of 25/2 becomes 12?
to be clear, it's an extreme basic of working with computers that what we (we programmers) think of as "division" has no connection at all to what normal human beings think of as division.
Did any of the answers solve your problem? Note, it's considered common courtesy to vote up a helpful answer, and/or mark an answer as the answer if you asked the question.
Answer by Keith1024 · Apr 27, 2013 at 06:50 PM
The problem you're experiencing here could just be a casting issue.
I'm assuming your slots array is storing int types here. If that's the case, your division is being performed entirely as integers. Which means that 25 / 2, never becomes 12.5f as you are expecting as the argument to Mathf.CeilToInt, instead the result is just 12.
Here is some commented code showing your case and the way you expect.
// In this line, division is performed on integers.
// result = 12
int result = Mathf.CeilToInt(25 / 2);
// On the other hand here, it will work as you expect.
// result = 13.
int result = Mathf.CeilToInt((float)25 / 2);
Casting the left term of the division expression causes the division to be performed as float data types, rather than int.
Answer by Loius · Apr 27, 2013 at 06:14 PM
25 / 2 = 12
/ 2.0 = 12.5
CeilToInt(12) = 12
CeilToInt(12.5) = 13
Totally didn't consider the fact he was probably using javascript.