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 Nyight · Jun 03, 2016 at 06:15 AM · 5.3algebra

5.3.4f1 comparison confusion

I apologize if this is a known issue or if I'm just being stupid but while doing some algebra to simplify some of the equations I'm using, I came across some odd inconsistencies which are shown below

 int r = 10;
 Debug.Log(r * 0.5f - r * 0.04f == r * 0.46f); // false
 Debug.Log((r * 1 / 2) - (r * 4 / 100) == (r * 46 / 100)); // false
 Debug.Log(r * (0.5f - 0.04f) == r * 0.46f); // true
 Debug.Log(10 * 0.5f - 10 * 0.04f == 10 * 0.46f); // true

Hopefully I'm just overlooking something simple but to me, all 4 of these are equivalent unless I'm not accounting for rounding errors somewhere. The biggest issue and most confusing this is that when each part is broken into variables and their types compared, they are both System.Single's and all result in 4.6.

So my question is why do the first 2 comparisons result in false while the second two return true? Especially the first and last. I'm using 5.3.4f1

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
0

Answer by mikelortega · Jun 03, 2016 at 07:19 AM

In the first line, you should nerver use == to compare floats, you will have precision problems, use Mathf.Approximately()

 Debug.Log(Mathf.Approximately(r * 0.5f - r * 0.04f , r * 0.46f)); // true

In the second line you are using int arithmetic, there are no floats.

 (10 * 1 / 2) - (10 * 4 / 100)  = 5 - 0 = 5
 (10 * 46 / 100) = 4

And 5 != 4

The third and fourth lines are correct, but you should better use Mathf.Approximately()

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.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

How do you make an algebra function for this pattern? 1 Answer

JointDriveMode is Obsoleted on Unity 5.3, so what now? 2 Answers

Touch in WebGL on Windows, STILL broken? 2 Answers

(Problem) Turning postprocessing on and off 0 Answers

Find point perpendicular to line 2 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