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
0
Question by the_genius · Mar 31, 2015 at 01:08 PM · mathfdivision

Incorrect Division and Remainder

Hello,

I am using the following code and looking at the results in the inspector at runtime:

 var number = 0.0;
 var quotient = 0.0;
 var remainder = 0.0;
 
 function Update () {
 
     quotient = Mathf.Floor(number / 12);
     remainder  = number % 12;
 
 }


It should divide the varable 'number' by 12 giving the 'quotient' rounded down to the nearest whole number, and the 'remainder'.

But, if I set 'number' to equal 12.99, the remainder given is 0.9899998, instead of 0.99.

Can anyone see where i am going wrong?

Any help is appreciated.

Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by _dns_ · Mar 31, 2015 at 01:52 PM

Hi, I guess this is a float precision issue. Here is a good article about it here: http://www.gamasutra.com/view/feature/1965/visualizing_floats.php?print=1

In your case, your may be able to increase precision using: remainder = number - (quotient * 12). You could also try using double precision float, it has the same behavior as regular floats but with greater accuracy.

Comment
Add comment · Show 1 · 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 the_genius · Mar 31, 2015 at 02:04 PM 0
Share

Thanks. 'remainder = number - (quotient * 12)' makes no difference. I will probably just round it to 2 or 3 decimal places, as that gives the right answer to a good enough accuracy.

avatar image
0

Answer by scythe75 · Mar 31, 2015 at 02:18 PM

you can´t divide 0 I think that this is the error.

Comment
Add comment · Show 1 · 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 the_genius · Mar 31, 2015 at 02:31 PM 0
Share

I am not dividing by zero as I change the values in the inspector and set 'number' to 12.99.

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

21 People are following this question.

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

Related Questions

Mathf.Approximatley if statement 0 Answers

Mathf clamp movement to Camera space in js 1 Answer

How to get a value between 0 and 1 2 Answers

A math issue. 3 Answers

Make my player who uses Character Controller to stop moving in one certain direction once it reaches the borders. 1 Answer


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