Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
2
Question by Scribe · Mar 30, 2011 at 07:42 PM · floatint

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

Comment
Add comment · Show 5
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image karl_ · Mar 30, 2011 at 07:57 PM 3
Share

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.

avatar image Scribe · Mar 30, 2011 at 08:10 PM 0
Share

thanks for the answer

avatar image Cyclops · Mar 31, 2011 at 01:52 AM 0
Share

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

avatar image Scribe · Mar 31, 2011 at 05:31 AM 0
Share

I know his answer worked better however I am using variables ins$$anonymous$$d 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

avatar image SirGive · Mar 31, 2011 at 05:34 AM 0
Share

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.

4 Replies

· Add your reply
  • Sort: 
avatar image
10
Best Answer

Answer by _MGB_ · Mar 30, 2011 at 08:01 PM

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;
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
3
Best Answer

Answer by tool55 · Mar 30, 2011 at 07:49 PM

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.

Comment
Add comment · Show 7 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Scribe · Mar 30, 2011 at 08:09 PM 0
Share

Thank you both 1 up to both of you but tool55 got their first sorry $$anonymous$$GB

avatar image _MGB_ · Mar 30, 2011 at 08:33 PM 2
Share

Heh. Unfortunately tool55's code still results in a zero.

avatar image Eric5h5 · Mar 30, 2011 at 08:42 PM 3
Share

Using integer values in an operation will always result in an integer value, regardless of what type the variable is.

avatar image Statement · Mar 30, 2011 at 09:02 PM 0
Share

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

avatar image Eric5h5 · Mar 31, 2011 at 07:38 AM 1
Share

@Scribe: $$anonymous$$GB'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.

Show more comments
avatar image
2
Best Answer

Answer by SirGive · Mar 31, 2011 at 05:28 AM

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);
}
Comment
Add comment · Show 7 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Scribe · Mar 31, 2011 at 05:41 AM 0
Share

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

avatar image SirGive · Mar 31, 2011 at 05:46 AM 0
Share

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

avatar image SirGive · Mar 31, 2011 at 05:49 AM 0
Share

that should be the right syntax

avatar image SirGive · Apr 15, 2011 at 06:59 AM 1
Share

come now, in elementary we learn what whole numbers and decimals are :P

avatar image SrBilyon · Apr 15, 2011 at 07:04 AM 1
Share

Seriously, if you don't know what an int/whole number is, cease program$$anonymous$$g now. :D

Show more comments
avatar image
0

Answer by u3d_loveye · Mar 31, 2011 at 01:40 AM

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

Comment
Add comment · Show 8 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Cyclops · Mar 31, 2011 at 01:54 AM 1
Share

This doesn't appear significantly different from @tool55's answer.

avatar image SirGive · Mar 31, 2011 at 05:59 AM 0
Share

I'm assu$$anonymous$$g 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

avatar image Eric5h5 · Mar 31, 2011 at 07:35 AM 0
Share

"#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.)

avatar image tool55 · Mar 31, 2011 at 01:50 PM 0
Share

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.

avatar image Cyclops · Mar 31, 2011 at 05:40 PM 0
Share

@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

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

No one has followed this question yet.

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

setting max varible value 5 Answers

Turn float to int 2 Answers

Cannot implicitly convert type `float' to `int'. An explicit conversion exists (are you missing a cast?) 1 Answer

Resticing variables to whole numbers 3 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges