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 /
This question was closed Jun 28, 2015 at 08:39 PM by meat5000 for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by The-Wicked-One · Jun 28, 2015 at 06:12 PM · math

Unity thinks that 14 = 15?

Hello,

Debug.Log ((int) ((0.4875f + 0.4125f - 0.75f) * 100f));

Why does Unity tell me 14? Any way to fix that? I give you that numbers as a result from other formulas, so you can count it easily, I... I just don't know what to think.

Btw Unity converts -15 float into -15 int normally, but for some odd reason it thinks that 15 float is 14 int.

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

  • Sort: 
avatar image
1
Best Answer

Answer by tanoshimi · Jun 28, 2015 at 06:15 PM

Simple casting a float to (int) loses the fractional part due to integer truncation The result of your calculation is something like 14.99997f, so (int)14.99997f = 14.

If you want the answer 15, try Mathf.Round() instead.

Comment
Add comment · Show 12 · 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 meat5000 ♦ · Jun 28, 2015 at 06:18 PM 0
Share

This illustrates the point. Run both of these scripts and notice the number of decimal places in the added decimal number. The scripts are identical except one has 1 less decimal place on the added constant in the debug.

 using UnityEngine;
 
 public class Test : $$anonymous$$onoBehaviour
 {
     float one;
 
     void Start ()
     {
         one = ((0.4875f + 0.4125f - 0.75f) * 100f);
     }
 
     void Update ()
     {
         Debug.Log (one);
         Debug.Log ((int)(one + 0.000001f));
     }
 }


 using UnityEngine;
 
 public class Test : $$anonymous$$onoBehaviour
 {
     float one;
 
     void Start ()
     {
         one = ((0.4875f + 0.4125f - 0.75f) * 100f);
     }
 
     void Update ()
     {
         Debug.Log (one);
         Debug.Log ((int)(one + 0.00001f));
     }
 }
avatar image The-Wicked-One · Jun 28, 2015 at 06:19 PM 0
Share

But it's not 14.99997f, it's 15.00000000000(infinite 0s)f It's 15. Fifteen.

How did you get 14.99997?

avatar image meat5000 ♦ · Jun 28, 2015 at 06:19 PM 0
Share

There is ALWAYS floating point error in a float.

It says its 15.0000000000000000 but its likely not.

$$anonymous$$y little script demo will illustrate to you how far you can expect a float to be out.

As tanoshimi explained, an int will ALWAYS round down, so 14.99997 will become 14.

avatar image The-Wicked-One · Jun 28, 2015 at 06:22 PM 0
Share

Oh, thanks.

avatar image maccabbe · Jun 28, 2015 at 08:19 PM 1
Share

Rounding up rounds to int that has a larger value

1.5 -> 2

-1.5 -> -1

Rounding down rounds to int that has a smaller value

1.5 -> 1

-1.5 -> -2

Rounding to 0 rounds down for positive numbers and up for negative numbers

1.5 -> 1

-1.5 -> -1

https://en.wikipedia.org/wiki/Rounding#Rounding_to_integer

Show more comments

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Trying to move an object towards another's axis. 1 Answer

Attaching game objects with joints - Matching angles 0 Answers

[RESOLVED] Raycast - Calculate Angular Difference from Object 3 Answers

radar orthogonal line to plane 1 Answer

getting the lowest point of a sine wave in javascript 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