- Home /
Problem is not reproducible or outdated
Why say Unity that 320* (1 / 320) = 0?
When I run the code below, I got this "Answer is 0" while it have to be 1. Can anybody tell me how to fix this? Thanks in advance. Code: Debug.Log("Answer is " + 320* (1 / 320));
Even when I use this code, the answer is still 0 Code:` float y = 320 * (1 / 320); Debug.Log("Answer is " + y);`
Answer by Demonicdaron · Aug 10, 2015 at 12:33 PM
To actually make it work you gotta convert the "1" must be a float, it doesn't matter if the "320" multiplying is float or not. So the correct form is:
Debug.Log("Answer is " + 320 * (1f/320));
If "1/320" is not a float, you will get a integer result (rounded if necessary), which is what you were getting there:
1/320 = 0.0... which rounded to integer = 0, which multiplied by anything gives you 0
Answer by Hellium · Aug 10, 2015 at 12:34 PM
@allenallenallen , you were nearly right !
Debug.Log( "Answer is " + 320 * ( 1 / (float) 320 ) ); // OR
//Debug.Log( "Answer is " + 320 * ( (float) 1 / 320 ) );
Other possibility :
Debug.Log( "Answer is " + 320 * ( 1 / (float) 320f ) );
(float) ( 1 / 320 ) )
converts the result of the integer division (0) to a floating value : 0.0; while ( 1 / (float) 320 )
will give a floating result of the floating division 1/320.0
Answer by allenallenallen · Aug 10, 2015 at 12:24 PM
Because you didn't specify it to be a float so it automatically thinks it's an integer.
320 x (1/320) = 320 x 0 = 0
If you want this to work the way you wanted it to:
Debug.Log("Answer is " + 320 * (1f/320)); // C# script, not sure about JS
Edited: This one works now.
Thank for answering so fast! But unfortunately it doesn`t work the answer is still 0.
Nope, your code doesn't work :/ it still outputs zero
To add syntax highlighting, you highlight your code and then click on the 101010 button.
And I tried my own code and didn't work, but I found a way to make it work.
Debug.Log("Answer is " + 320 * (1f / 320));
Just add 'f' to either the 1 or 320 which would mark that number as a float and force the calculation answer to be a float.
Thank you allenallenallen now it works! :) But if I click the 101010 button, the only I get is: Debug.Log("No syntax highlighting");
Answer by NeverHopeless · Aug 10, 2015 at 12:33 PM
You should try like this:
Debug.Log("Answer is " + 320.0f * (1.0f/320.0f));
Specifying f
ensures that the number should be treated as a float. So it will preserve the remaining part.
Hope it helps!
@Demonicdaron, i personally prefers to keep numbers in a same number system when being manipulate to avoid unusual conditions (e.g., loss of precision) but you are possibly correct that it may not necessary for some cases.
Answer by RLin · Aug 10, 2015 at 12:31 PM
This likely occurs because floats attempt to round themselves off to be faster and more efficient, but rounding will obviously cause some miscalculations; hence the name floating point precision errors. You could try using doubles instead of floats, or rewriting your statement to (320 * 1) / 320;