4 ms·
Any time you're generating percentage data that should sum to 100, not appreciating floating point math will burn you. For those interested, the largest remain
by pcprincipal 10y ago
Any time you're generating percentage data that should sum to 100, not appreciating floating point math will burn you.
For those interested, the largest remainder method (https://gist.github.com/hijonathan/e597addcc327c9bd017c https://gist.github.com/hijonathan/e597addcc327c9bd017c) is useful for dealing with this.
- protonfish 10y agoUsing tricks to make numbers total what you think they "should" other than using rounding to proper amount of precision is lazy and deceitful.
- benjoffe 10y agoDepends on the application of this, i.e. whether the total is more important or the individual values are more important. Example: You're filling out a timesheet for a contracting job, and you worked 8 hours on several different tasks for your client, but your client's software rounds things to the nearest hour, then it would make sense to use an algorithm like this if your pay was going to be determined by this data entry. If your pay was not determined by this data entry then it may make sense to just round normally.
- jacobolus 10y agoYou should just round the percentages to the nearest value of the appropriate precision, and let them sum to slightly more or less than 100% if that’s how the numbers work out. If you want, add an asterisk and a “note, numbers do not sum to 100% because of rounding” at the bottom.