• 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 /
  • Help Room /
avatar image
1
Question by Noxides · Jan 07, 2018 at 01:24 PM · floatsubtracting

Subtracting a float from a float not always working

I've been trying to solve this problem all evening. I'm either really stupid or doing something completely wrong.

I have 2 floats, Amount and Access defined as below:

 float amount = UnityEngine.Random.Range(0f, 10000000f);
 float access = (float)Math.Round(UnityEngine.Random.Range(0.1f, 1f), 1);


These are used to create a new mineral deposit class

  MineralDeposit mineralDeposit = new MineralDeposit(mineralType, amount, access);

MineralDeposit.cs

 public class MineralDeposit
 {
     public MineralDeposit(MineralType despositType, float amountInDesposit, float access)
     {
         DespositType = despositType;
         Amount = amountInDesposit;
         Access = access;
     }
 
     public float Access { get; private set; }
 
     public float Amount { get; private set; }
 
     public MineralType DespositType { get; private set; }
 
     public float RemoveFromDeposit(float amountToRemove)
     {
         if (amountToRemove > Amount)
         {
             Amount = 0;
             return amountToRemove;
         }
         else
         {
             UnityEngine.Debug.Log(Amount);
             Amount -= amountToRemove;
             return amountToRemove;
         }
     }
 }

The RemoveFromDeposit method is called like this:

 float amountMined = minerals.RemoveFromDeposit(minerals.Access / 10f);

The method is returning correct but the issue is the Amount variable in the MineralDeposit class isn't subtracting. It works if I use RemoveFromDeposit(minerals.Access * 10f); but whenever it gets a smallish float passed it's refusing to subtract.

I'm am doing something blatantly wrong here?

Thanks.

Comment
Add comment · Show 1
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 ShadyProductions · Jan 07, 2018 at 01:54 PM 0
Share

Perhaps you should try this with decimal's ins$$anonymous$$d. Floating point numbers can be inaccurate and give different results based on how big/small your float is.

2 Replies

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

Answer by Bunny83 · Jan 07, 2018 at 02:47 PM

That's because you just run out of significant digits. I've posted a reference table over here. The larger the value of a float gets, the larger the smallest possible difference gets.


If your value is around 10 million you are in the range "8M - 16M". The smallest representable difference is "1.0" in this case. Anything smaller than that can not have any effect on the number. It's a floating point number. The precision varies depending on how large the number is.


edit

If you really need to work with that large values and that small values at the same time you may want to split the number into two variables. One long values to store the whole number part and a float to store just the fraction. Of course the first step could be to simply switch to double instead of float. Though if the numbers might get much larger you may run into problems with doubles as well.


When using a long / float combination you would add / subtract an amount from the float part first and then carry the whole number part from the float to the long variable.


Example

 f = 0.5f;
 l = 10000000;
 // add 2.0123
 f += 2.0123f; // --> 2.5123
 
 int a = Mathf.FloorToInt(f); // --> 2
 f -= a; // --> 0.5123
 l += a; // --> 10000002
 
 // subtract 5.7f
 f -= 5.5f; // --> -4.9877
 
 int a = Mathf.FloorToInt(f); // --> -5
 f -= a; // --> 0.0123
 l += a; // --> 9999997


Comment
Add comment · Show 1 · 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 Noxides · Jan 07, 2018 at 03:15 PM 0
Share

This seems to be the problem

Thank you for the example as well. steers me in the right direction. $$anonymous$$uch appreciated.

avatar image
0

Answer by OneCept-Games · Jan 07, 2018 at 01:58 PM

I might be wrong, but i think your get/set's are not doing anything.
At least you should update and/or return a local variable, and use this instead of accessing the variable directly.

 float _access;
 val { get { return _access; } set { _access = value; } }
Comment
Add comment · Show 1 · 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 ShadyProductions · Jan 07, 2018 at 02:37 PM 0
Share

empty getter and setters will just access it's own variable. So this isn't correct.

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

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

75 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

Related Questions

float subtraction gives wild answers? 2 Answers

Not Checking if Greater than 0 1 Answer

Reference Vector 3 variable to float variable. 2 Answers

Really confusing float question 1 Answer

Make a float update its value based on another floats value 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