• 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 mrravi · Jan 17, 2015 at 10:03 AM · c#typecasting

Why typecasting not working properly in c# ?

I want to do a calculation which seems to be like

 float addvalue,currentpostion;
 int currentrowvalue;
 
 void Start
 {
 addvalue = 2.1;
 currentposition = ((addvalue- 0.3f)/0.6f)+5;
 currentrowvalue = (int)currentposition;
 Debug.Log("currentrowvalue " +currentrowvalue+ " currentposition " + currentposition);
 }

Its working fine for all other value of addvalue i have supply 0.9,0.3 in every condition it work fine but for 2.1 i am getting following output

currentrowvalue 7 currentposition 8

why typecasting not working properly what mistake iam making ?

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
2

Answer by tanoshimi · Jan 17, 2015 at 10:13 AM

Doing a simple cast to int truncates a float. Because your result is probably something like 7.999999, (int)7.999999 will be 7.

To round a float to int, you should use Mathf.Round instead.

Comment
Add comment · Show 3 · 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 Mmmpies · Jan 17, 2015 at 10:30 AM 0
Share

Or

 currentvalue = $$anonymous$$athf.RoundToInt(currentposition);

Odd though, even in my head I know 1.8 / 0.6 = 3 because 6 goes into 18 three times, so it should equal 8.

avatar image tanoshimi · Jan 17, 2015 at 10:45 AM 1
Share

That would only be true if you were using exact, decimal mathematics. This is a floating-point calculation, so it's inherently imprecise.

 1.8 / 0.6 = 3

 1.8f / 0.6f != 3f  (not necessarily, anyway)
avatar image Mmmpies · Jan 17, 2015 at 10:48 AM 0
Share

Glad you're around @tanoshimi - thanks for the explanation.

avatar image
0

Answer by pasman · Jan 17, 2015 at 01:48 PM

Use Mathf.RoundToInt instead

 currentposition = ((addvalue - 0.3f) / 0.6f) + 5f;
 currentrowvalue = Mathf.RoundToInt(currentposition);
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

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Illuminating a 3D object's edges OnMouseOver (script in c#)? 1 Answer

Unity. Some fields go to null on Play 1 Answer

Flip over an object (smooth transition) 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