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 /
This question was closed Apr 01, 2012 at 04:54 PM by hijinxbassist for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by hijinxbassist · Apr 01, 2012 at 04:28 PM · floatbug-perhapsintreturnequation

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.

Comment
Add comment · Show 6
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 aldonaletto · Apr 01, 2012 at 04:42 PM 2
Share

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.

avatar image hijinxbassist · Apr 01, 2012 at 04:53 PM 0
Share

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.

avatar image kromenak · Apr 01, 2012 at 05:04 PM 1
Share

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.

avatar image hijinxbassist · Apr 01, 2012 at 08:08 PM 0
Share

Good to know. I will add the f to the end. Thanks.

avatar image Eric5h5 · Apr 01, 2012 at 08:50 PM 3
Share

@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.

Show more comments

0 Replies

  • Sort: 

Follow this Question

Answers Answers and Comments

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

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


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