• 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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



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

subtracting numbers gives a very wrong answer 0 Answers

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Comparing lerping quaternion 1 Answer

Programmatically specifying a random point in an arc in front of the player 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges