Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Bumblebee77 · May 04, 2016 at 01:52 PM · typecasting

(int)(variable*float ) problem?

Hi, I am really confused:

     float deneme = 100f;  // or int deneme=100
     int a = (int)(deneme * 0.01f);
     int b = (int)(100 * 0.01f);

Why both gives different results? a=0; b=1;

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

1 Reply

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

Answer by meat5000 · May 04, 2016 at 02:18 PM

Floating Point error.

On my machine with your snippet I get

 float deneme = 100.00001f; //a = 1;
 float deneme = 100.000001f; // a = 0;

An int will Never round up. Only down.

Comment
Add comment · Show 4 · 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 Bumblebee77 · May 04, 2016 at 02:31 PM 0
Share

But why using avariable and 100 gives different results?

avatar image Dave-Carlile Bumblebee77 · May 04, 2016 at 02:35 PM 1
Share

Different execution order? Different intermediate results? Hard to say without seeing what IL the two versions compile down to.

What Every Computer Scientist Should $$anonymous$$now About Floating-Point Arithmetic.

Also, $$anonymous$$athf.RoundToInt will take care of your issue.

avatar image Bumblebee77 · May 04, 2016 at 02:50 PM 0
Share

sorry i dont get it. Lets forget deneme is float.

int deneme=100; int a = (int)(deneme 0.01f); int b = (int)(100 0.01f);

now a =0 and b=1 again. why this is floating Point error?

avatar image Dave-Carlile Bumblebee77 · May 04, 2016 at 06:11 PM 1
Share

Read the link I provided.

When you do int b = (int)(100f * 0.01f) think about the intermediate result. 100f * 0.01f is likely giving you something like 0.9999999998. When you type cast that to an int it truncates everything past the decimal point, so you get 0. For whatever reason (as I stated earlier), when doing the same with a variable it's executing the floating point operations slightly differently so deneme * 0.01f might be giving you exactly 1 or 1.000000001 or something. And again, when you cast that to int it's truncating everything past the decimal point, leaving you with 1.

The reason for the "not exact" numbers is explained in my link above. And as the title explains it is information you should know about.

Use $$anonymous$$athf.RoundToInt(deneme * 0.01f) ins$$anonymous$$d of typecasting to int and your problem will cease to be.

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

6 People are following this question.

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

Related Questions

Instantiate Typecasting 4 Answers

TypeInferenceRuleAttribute on Resources.LoadAll? 1 Answer

Type casting error while instantiating. 1 Answer

How to Typecast JS Variables as C# Classes? 0 Answers

Invalid behavior of C# code while using IList on iOS 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