- Home /
The question is answered, right answer was accepted
Whole Numbers Divide Incorrectly....BUG?
Ive been pulling my hair out with this problem. Finally i tried something i thought to not be a solution, yet it works. Here is my equation..
yield WaitForSeconds(2/difficulty);
difficulty is set to 3, and the equation returns 0. If i change it to..
yield WaitForSeconds(2.0/difficulty);
it works as expected. Does it not expect the return to be a float if the .0 isnt added? Is this the same in 3.5? Can someone do a quick test if you are running 3.5 and let me know. If the return is 0, i would like to add it to the Fix-It list.
I suppose that difficulty is also an int - it it were a float, the float division should be used by the compiler. When both operands are integers, integer division takes place.
After some more experimentation, i realized that one indeed does needs to be a float(the whole value or the var). I guess its not to big a deal now that i know. $$anonymous$$aybe this will help someone in the future. Thanks for the response, case closed.
This is pretty common behavior in a lot of program$$anonymous$$g languages - C, C++, C#, I think Java as well. The system doesn't store integers with a decimal portion, which is faster - the downside is that uneven division leads to missing decimal portions.
As an additional side note, when you say "2", you are declaring an int. When you say "2.0", you are declaring a double. When you say "2.0f", you are declaring a float. Floats are smaller than doubles, so it is worth adding the "f" to the end if possible.
@kromenak: that's not true. The question is using Unityscript, and in Unityscript, "2" is an int, "2.0" is a float, and "2.0d" is a double. You can write "2.0f" if you want, but it's the same as "2.0", so you might as well not bother, unless you're trying to write code that's consistent with C#.
@hijinxbassist: It would be a bug if integer division did not return an integer. This is not anything that needs to be fixed, it's correct behavior.
Follow this Question
Related Questions
Javascript int to float 1 Answer
Random.Range Problems (Errors CS1502 and CS0266) 1 Answer
How to calculate distance to top of ground in Unity 2D 0 Answers
C# Int to Float conversion performance 3 Answers
setting max varible value 5 Answers