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 Rexkwong · Jun 21, 2020 at 08:22 AM · calculationmultiply

Calculation error when multiplying a float

I am making a bank system and I found some issues about the interest calculation. If you try **Debug.Log(100000 1.05f - 100000);*, you will get a result 4999.992. Does everyone has idea how to solve this error?

Below are some of the wrong output:

Debug.Log(100000 * 1.05f - 100000); //Output: 4999.992

Debug.Log(100000 * 1.06f - 100000); //Output: 5999.992

Debug.Log(100000 * 1.07f - 100000); //Output: 7000.008

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

Answer by SamKim99 · Jun 21, 2020 at 03:01 PM

I think it is caused by Resolution of Float.

In computer, we should represent the numbers with bits, 0 and 1. So, the values we can get after some calculations might be an approximated values.

There can be countless ways to represent floating points. In fact, you can make your own method. Therefore, we need a standard method. Most of programming languages use IEEE Standard for Floating-Point Arithmetic (IEEE 754) to represent it. It represent the floating points with 1 sign bit, 8 exponent bits, 23 fractions.

So... it is long long way to get that point, you can use more precise data type, which might be decimal or double!


Double

 class Program
 {
     static void Main(string[] args)
     {
         Console.WriteLine(100000 * (double)1.05 - 100000);
     }
 }

Result

alt text



Decimal

 class Program
 {
     static void Main(string[] args)
     {
         Console.WriteLine(100000 * (decimal)1.05 - 100000);
     }
 }

Result

alt text


double.png (318 B)
decimal.png (337 B)
Comment
Add comment · Show 2 · 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 Bunny83 · Jun 21, 2020 at 03:50 PM 1
Share

Right. For everyone wondering why this happens I can recommend watching this Computerphile video on floating point numbers. I've also made this table for 32 bit floating point numbers to see what the smallest representable change would be at a certain interval.

avatar image Rexkwong Bunny83 · Jun 21, 2020 at 04:10 PM 0
Share

Thank you very much both! Did learn something today!

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

128 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 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 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 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 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 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 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 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 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Are OnGUI calculations cached? 1 Answer

How to save run-time variables to dictionary? 1 Answer

How can I dynamically change the percentage value so that it always adds up to 100%? 1 Answer

Multiplying quaternions and Multiplying quaternion with Vector3 3 Answers

What do I add to this script, so if you clicked the button ex. 30 times, a gui.box will appear? 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