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 CloverKitsune · Apr 16, 2013 at 03:16 AM · c#mathwronganswersubtract

Subtraction gives completely wrong answers

I'm working on a tutorial (substituting C# for JS) for a camera that zooms out when the player jumps. Changing the variable to restrict the maximum zoom out had no effect, so I tested some stuff in the main camera's script and found an issue in this piece of code in the Update function:

         if (camZoom)
         {
             targetCurPos = player.transform.position.y;                    // get target's current Y position
             playerJumpHeight = targetCurPos - playerScript.groundPos;    // get latest jump height
             
             print ("STARTING POS " + playerScript.groundPos);
             print ("CURRENT POS " + targetCurPos);
             print (playerJumpHeight);

When I stand still, the starting and current positions both alternate between 0.74 and 0.7399999 and when subtracted, the answer alternates between - or + 5.0960464E-08. I have no idea where these answers are coming from. Am I missing something?

Comment
Add comment · Show 4
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 Eric5h5 · Apr 16, 2013 at 03:28 AM 1
Share

It's not "completely wrong", it's only very slightly wrong, and you should always expect that when using floating point.

avatar image CloverKitsune · Apr 16, 2013 at 03:43 AM 0
Share

Sorry, but without $$anonymous$$roni$$anonymous$$'s helpful answer I wouldn't have known that an E means that a number like 5.09etc. is actually a tiny decimal.

avatar image xKroniK13x · Apr 16, 2013 at 03:49 AM 0
Share

E is scientific notation, essentially. So, 5.0960464E-08 is the same as 5.0960464 x 10^-8 . So that means you move the decimal eight places to the left. The same would be true if it was a positive eight, you'd just move it to the right eight places. Glad to help :)

avatar image Eric5h5 · Apr 16, 2013 at 03:49 AM 0
Share

That's just scientific notation.

1 Reply

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

Answer by xKroniK13x · Apr 16, 2013 at 03:26 AM

A lot of times things like this will have weird decimals. 5.0960464E-08 is essentially 0, since it is .000000050960464. If you're aiming for 0 here, it's negligible. I've had lot of crazy decimals like this throughout coding, especially when doing something like manipulating the positions of a player. Unless it is clean cut like 2+2=4, then you may get weird numbers.

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

13 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

Related Questions

Distribute terrain in zones 3 Answers

Multiple Cars not working 1 Answer

subtracting numbers gives a very wrong answer 0 Answers

Connect matlab and unity 0 Answers

Trying to get my player to move forward, backward, left and right using WASD in relation to camera direction. My problem cannot be solved using AddRelativeForce, my player is a sphere. 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