- Home /
Why can't I add to a float?
Im not able to add small numbers to my float.
For example, ive got a float thats set to 10,000,000
If I add 3 to it, nothing changes. If I add 3,000,000 it does.
What do I need to do to make sure this kinda thing doesnt happen?
this shouldn't happen. Show us the piece of code where this happens.
print(totalGold + " : " + GetPayout (heroID,taskID));
//GetPayout returns 3
//total Gold is set to 10,000,000 but prints as 1E+07
totalGold += GetPayout (heroID,taskID);
print(totalGold + " : " + GetPayout (heroID,taskID));
//no difference in the print out or in the textbox that shows the number.
when you cast a float or double to string it will use scientific values(1E+07). if it prints 1E+07 at the second time, it doesn't mean you calculation is wrong, it's just a rounding. try printing it like this:
print(totalGold);
totalGold += GetPayout (heroID,taskID);
print(totalGold);
still get the same results?
Answer by siaran · May 02, 2015 at 04:25 PM
This has to do with the (lack of) accuracy of floats. I won't go into the details here, but floats are generally accurate withing 7 digits.
10,000,000 is 8 digits long, and adding 3 only affects the last digit, which is outside the accuracy range. Adding 3,000,000 affects digits well withing the 7 digit accuracy range on 10,000,000.
if you need higher accuracy, you should consider using double or decimal type ins$$anonymous$$d of float.
I thought something like that might be the case. THanks Siaran, ill switch over to using a double
Your answer
Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
Multiple Cars not working 1 Answer
Unity Float to db 2 Answers
C# divide float by integer 2 Answers
Comparing lerping quaternion 1 Answer