Floating point addition error
Hi guys, I'm having a somewhat unusual problem adding floating point numbers. The issue occurs when I try to add low decimal place values to to floats above 2,100,000 as they simply stop adding to the number; I also get issues when adding low whole numbers to large floating point values, for example if i try to add a float of 10.0 to a float of 52,100,440 then it will only add 8 instead... I'm at lose and this has broken our scoring system. More details below:
[Information on the Game and our floating point scoring system] We have a game with a scoring system where the player earns points for killing the bad guys the idea is to give the play really big stupidly large values for this. The scores are stored as floating point numbers but only displayed on the screen as whole numbers (the display is rounded by the ToText node in blueprints, to remove the decimal values, the raw score itself is never rounded), we're using floating points to store the scores so that we can multipliers (with decimal places) as well based on the number of enemies killed (the multiplier increases by 0.2 for each enemy killed and will reduce back to 1 if no enemies are killed in the last five seconds). The scores are designed to get pretty big, into the hundreds of millions. We want the score to be permanently in motion so for each tick that the player is moving we were adding 0.1 to the score. Score is stored in the GameState and the functions for added to the score and the multiplier are also in the GameState.
[Problems] When the score get's over 2,100,000 the decimal place values stop being added to it and and the score stops ticking up. I've since tried which larger numbers and i'm getting some real odd results (see the attached output log below).
[Tests] I set up some quick controls for adding 50,000 | 100,000 | 10,000,000 points to the score and augmented the scoring system to add 10 points to the score for each tick in which the player is in motion. The 10 points for motion get added just fine until I force the score above 52,100,440 after which 8 points are added instead of 10 even though it is still being told to add 10 (see output log). This continues until the score until the score is forced up above 152,100,576 after which 16 points are added when it is told to add 10. After pushing the score to 412,100,672 no points are added when it is told to add 10.
The Game is currently in 4.16.0. This issue occurs both in engine and in the build. All of the functionality is made in blueprints, but it is a code project.
Any suggestions would be really appreciated as I'm at a lose here. Thanks.
I've added a quick video on YouTube which shows the issue and the blueprints in question, hope it helps: https://youtu.be/hrMsuESQLhg
[Link to the Output Log] link text
Of course, this will happen with floating point numbers. Floats are only guaranteed accuracy up to 7 significant decimals. And, since you are in the 50,000,000 and 10,000,000 respectively, you have already taken up more than 7 significant decimal digits. Thus, accuracy is lost when adding, multiplying and etc.
See this stackoverflow for more details on this subject. https://stackoverflow.com/questions/28045787/how-many-decimal-places-does-the-primitive-float-and-double-support
You might consider switching to integer for storing the absolute score value as int64 (long) can store up to 9,223,372,036,854,775,807 on 64bit machines. Then apply your multiplier to a slightly lower number and add that to the absolute integer value. A 32 bit int can store up to 2,147,483,647.
answered May 29 '17 at 07:22 PM
Follow this question
Once you sign in you will be able to subscribe for any updates here