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 Trond · Apr 27, 2013 at 05:23 PM · mathfrounding-errors

Mathf.CeilToInt I do not understand it ?

hey, i have this line in my code:

 holding[1] = Mathf.CeilToInt( (slots[counter][1]) / 2 );

basicly, if "slots[counter][1]" is a odd number, like 25, its rounded down 12, not up 13...

Have i misunderstood how "Mathf.CeilToInt" works?

Comment
Add comment · Show 3
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 xortrox · Apr 27, 2013 at 06:03 PM 0
Share

Could you add this to your code before calling CeilToInt

Debug.Log("Calling CeilToInt on: " + (slots[counter][1] / 2));

Then post the result where CeilToInt of 25/2 becomes 12?

avatar image Fattie · Apr 27, 2013 at 09:01 PM 1
Share

to be clear, it's an extreme basic of working with computers that what we (we programmers) think of as "division" has no connection at all to what normal human beings think of as division.

avatar image Keith1024 · Apr 28, 2013 at 06:33 PM 1
Share

Did any of the answers solve your problem? Note, it's considered common courtesy to vote up a helpful answer, and/or mark an answer as the answer if you asked the question.

2 Replies

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

Answer by Keith1024 · Apr 27, 2013 at 06:50 PM

The problem you're experiencing here could just be a casting issue.

I'm assuming your slots array is storing int types here. If that's the case, your division is being performed entirely as integers. Which means that 25 / 2, never becomes 12.5f as you are expecting as the argument to Mathf.CeilToInt, instead the result is just 12.

Here is some commented code showing your case and the way you expect.

 // In this line, division is performed on integers.
 // result = 12
 int result = Mathf.CeilToInt(25 / 2);
 
 // On the other hand here, it will work as you expect.
 // result = 13.
 int result = Mathf.CeilToInt((float)25 / 2);

Casting the left term of the division expression causes the division to be performed as float data types, rather than int.

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
2

Answer by Loius · Apr 27, 2013 at 06:14 PM

25 / 2 = 12

  1. / 2.0 = 12.5

CeilToInt(12) = 12

CeilToInt(12.5) = 13

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 xortrox · Apr 27, 2013 at 08:12 PM 0
Share

Totally didn't consider the fact he was probably using javascript.

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

16 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

Related Questions

How to limit the rotation of an object?? 2 Answers

Mathf.PingPong from a random position problems 0 Answers

SmoothDamp from 360 to 1 1 Answer

Make value change to anoter over time and back loop 3 Answers

Using Math to adjust a transforms angle about an axis? 0 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