Coming soon - Get a detailed view of why an account is flagged as spam!
view details

This post has been de-listed

It is no longer included in search results and normal feeds (front page, hot posts, subreddit posts, etc). It remains visible only via the author's post history.

8
How do I deal with floating point rounding errors?
Post Flair (click to view more posts with a particular flair)
Post Body

I am currently developing an incremental game using gold as currency. However when you buy an item that gives gold per second after three seconds it starts giving values such as 4.1000000000000005 for player gold.

This is my function I have set to calculate player gold by adding the gold per second value every second:

setInterval(function() {
player.gold = player.gold   Math.round(player.gold_per_second * 100) / 100;
player.lifetime_gold = player.lifetime_gold   Math.round(player.gold_per_second * 100) / 100;
update_view();
}, speed);

I have read somewhere that by multiplying player.gold_per_second by 100 then dividing it by 100 would eliminate floating point rounding errors however it hasn't done anything for me.

Author
Account Strength
100%
Account Age
13 years
Verified Email
Yes
Verified Flair
Yes
Total Karma
8,447
Link Karma
1,741
Comment Karma
6,706
Profile updated: 8 hours ago
Posts updated: 1 month ago

Subreddit

Post Details

We try to extract some basic information from the post title. This is not always successful or accurate, please use your best judgement and compare these values to the post title and body for confirmation.
Posted
10 years ago