Int vs. Float operations

Hi guys,

this seems like a really stupid question to have to be asking but Unity can't seem to manage to do a simple maths sum. (Its probably me and I'll kick myself for it afterwards I'm sure)

a code sample:

function Update () {
    var division = 3/4;
    print (division);

this simple piece of code in a much larger script is giving me a lot of hassle. For some reason Unity decides that the answer to 3/4 is 0?!

whereas in this example:

function Update () {
    var division = 3 * 0.25;
    print (division);

it works out the right answer. I need to use the equivalant to the first example as I am using variables instead of the simple numbers

Any help would be much appreciated. I'm guessing its just me having a dumb moment

thanks Scribe

more ▼

asked Mar 30, 2011 at 07:42 PM

Scribe gravatar image

6.4k 35 37 71

thanks for the answer

Mar 30, 2011 at 08:10 PM Scribe

FYI, @Scribe, you can move the checkmark from one answer to another - in this case, it seems that @MGB's answer is the correct one.

Mar 31, 2011 at 01:52 AM Cyclops

I know his answer worked better however I am using variables instead of numbers which I stated in my question therefore I can't write word.0 so I had to change both vars to floats. Tool55's answer was more helpful in solving this problem

Mar 31, 2011 at 05:31 AM Scribe

Thats not a bad question. Normally, when you declare health or mana variables, you want them to be as int. Scribe, check my answer if you want to keep you vars as ints.

Mar 31, 2011 at 05:34 AM SirGive

If you had typed var division = 3.0 / 4.0 it would work. By not defining your variables as float or int Unity will default to int in the case you present.

Mar 30, 2011 at 07:57 PM karl_
(comments are locked)
10|3000 characters needed characters left

4 answers: sort voted first

You are getting the result of an integer division. You just need to make the divisor (the 4) a float value:

var division = 3/4.0;
more ▼

answered Mar 30, 2011 at 08:01 PM

_MGB_ gravatar image

1.1k 10 17 37

(comments are locked)
10|3000 characters needed characters left

var division : float = 3/4;

You may need to define the type as shown above. In your second example, you use a floating point value so Unity automatically defines it as a float. I believe that may be your problem.

more ▼

answered Mar 30, 2011 at 07:49 PM

tool55 gravatar image

755 7 10 19

Thank you both 1 up to both of you but tool55 got their first sorry MGB

Mar 30, 2011 at 08:09 PM Scribe

Scribe: You do realize this doesn't solve the problem though?

Mar 30, 2011 at 09:02 PM Statement

Interesting, Eric. Good bit of info. I would have assumed a float would be returned if you declared the variable as a float. Live and learn :)

Mar 31, 2011 at 12:45 AM tool55

actually for me I had to use an adapted version of this answer not MGB as I said I am using variables meaning I have to put word/word I can't therefore use MGB's answer as I can't write word/word.0 can I. I therefore solved the problem by changing my variables to floats which is more related to tool55's answer than MGB. I did try both answers out and realised that MGB's answer worked better but for my example using var's I deemed that tool55's answer had been more helpful in solving my question.

Mar 31, 2011 at 05:28 AM Scribe

@Scribe: MGB's answer is correct, and I'm afraid tool55's is not. It doesn't matter whether you're using literals or variables, the point is that the things that are doing the actual math have to be floats, not the variable you're assigning the result to.

Mar 31, 2011 at 07:38 AM Eric5h5
(comments are locked)
10|3000 characters needed characters left

The reason that your not getting the correct answer is because an "int" is defined as an integer (If you don't know what an integer is, its a whole number).

Edited at the request of Statement: (Whole numbers are numbers with no decimal part value. Ex: 2, 30012, 4239842. These are not whole numbers: 1.2, 1.4, 54.4 )

When you divide with integers, it rounds to the nearest whole number. In this case, its closer to 0, so it rounds down. Like numerous people have already said, adding ".0" would have converted it to a floating point decimal (float) which would have an integer plus the string of digits past the decimal.

Another way to get around this is to cast. For instance, you might be using values that are awkward if they aren't integers (health or experience are common variables that are best left as ints).

This is how you cast in javascript:

function Update () {
    var division = parseFloat(3)/parseFloat(4);
    print (division);

And this is how you cast in C# for you C# lurkers :P

void Update () {
    float division = (float)3/(float)4;
    print (division);
more ▼

answered Mar 31, 2011 at 05:28 AM

SirGive gravatar image

1.5k 75 70 102

Thanks for the answer but it just gave the error: The type 'float' does not have a visible constructor that matches the argument list '(int)' I'm guessing that means thats not how you cast but 1up for answering anyway It would be useful to find a way of keeping them as ints

Mar 31, 2011 at 05:41 AM Scribe

hmm hold on. let me find an example. I've done this before, but i normally stick to c#

Mar 31, 2011 at 05:46 AM SirGive

that should be the right syntax

Mar 31, 2011 at 05:49 AM SirGive

hey thanks loads it worked perfectly and is the answer I was looking for. Thank you

Apr 01, 2011 at 05:06 PM Scribe

"(If you don't know what an integer is, its a whole number)." I just realized this doesn't make any sense to anyone who doesn't know the difference between floats and ints :) "What is a whole number? Does the other numbers have gaps in them?"

Apr 03, 2011 at 12:07 AM Statement
(comments are locked)
10|3000 characters needed characters left

//better  using  like  this: 
#Pragma strict 
function Update () {
var division:float = 3/4;    
print (division);

more ▼

answered Mar 31, 2011 at 01:40 AM

u3d_loveye gravatar image

11 1 2 6

I'm assuming the #Pramga Strict allows for float values from the operation. I try to avoid using such things like this when i'm not doing much in my code relevant to #pragma strict

Mar 31, 2011 at 05:59 AM SirGive

"#pragma strict" is completely irrelevant here, and that code won't work anyway. As I already commented, using integer values in an operation will always result in an integer value, regardless of what type the variable is. This is why it's usually a good idea to test your code before posting it. (You would see that it prints 0.)

Mar 31, 2011 at 07:35 AM Eric5h5

Eric, as a follow up question, will the returned value have the same decimal accuracy as the value one uses? In other words, if I use 3.0/4.0 will I get one decimal accuracy as opposed to 3.00/4.00 where I might expect two decimal accuracy? Just curious. Thanks, as always.

Mar 31, 2011 at 01:50 PM tool55

@tool55, you'll get the full accuracy of a float in either case, which means 32-bit precision (roughly 7 digits). Which side of the decimal point your digits fall on, depends on the number. http://msdn.microsoft.com/en-us/library/aa691146(v=vs.71).aspx

Mar 31, 2011 at 05:40 PM Cyclops

Thanks, Cyclops. So, you're saying that the minute I add a decimal point to the values I input, the output will be a floating point out to the maximum precision that 32 bit allows. In other words, no rounding based on what I input. Have I got that right?

Mar 31, 2011 at 07:29 PM tool55
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here



Answers and Comments



asked: Mar 30, 2011 at 07:42 PM

Seen: 10420 times

Last Updated: May 29, 2011 at 09:33 AM