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 Aug 12, 2014 at 06:03 AM by tanoshimi for the following reason:

The question is answered, right answer was accepted

avatar image
1
Question by mobeeDREAM · Aug 12, 2014 at 04:30 AM · floatinteger

int myX = (int)transform.position.x;

Can someone explain me why the following lines of code :

 int myX = (int)transform.position.x;
 
 int myZ = (int)transform.position.z;
 
 Debug.Log("trxy= " + transform.position.x + ", " + transform.position.z + "   myxz= " + myX + ", " + myZ);

are tracing me those results :

trxz= 25, 12 myxz= 25, 11

trxz= 30, 13 myxz= 30, 12 ...

but on 40 traces, several are ok, example :

trxy= 15, 27 myxz= 15, 27

trxy= 27, 21 myxz= 27, 21 ...

i'm loosing time on that one !

Comment
Add comment · Show 1
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 rutter · Aug 12, 2014 at 04:32 AM 0
Share

So, computers are pretty good at faking it, but they're actually not all that precise about "floating point" numbers. For example, what you think is "11.0" might actually be closer to "10.99999".

When you cast a float to an int, the computer always rounds down. That 10.99999 would just become 10.

Programmers usually get around this in two ways:

  • Ins$$anonymous$$d of expecting exact equality, look for "fuzzy equality" within some tolerance.

  • Be aware that int conversions will truncate; if that's a problem, you can specify your rounding with Unity's $$anonymous$$athf.Round and RoundToInt.

I'm hesitant to let this through the moderation queue because it's a frequently asked question. It's not a bad question, we're just trying to cut down on the number of duplicate threads in this area of the site. If you have anything else to add, feel free to comment here; if not, I'll drop the question from the queue in a little while.

If you can't find help, here, you can always try the forums or look up some tutorials online.

1 Reply

  • Sort: 
avatar image
4
Best Answer

Answer by robertbu · Aug 12, 2014 at 04:33 AM

It is likely the ToString() method for integers rounds to some decimal place, but the casting to an int truncates. So if the real value of transform.position.z was 11.99999999999999, the string would be 12, and the integer cast of that value would be 11. To get around the problem do:

  int myX = Mathf.RoundToInt(transform.position.x);
  int myZ = Mathf.RoundToInt(transform.position.z);
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

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

OpenFeint float scores 2 Answers

Why are floats not accessing my inputted values? 1 Answer

[Closed] Cannot convert types? 2 Answers

Checking an increasing number 1 Answer

Rounded particle motion 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