Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to round floats to integers while preserving their sum?

Let's say I have an array of floating point numbers, in sorted (let's say ascending) order, whose sum is known to be an integer N. I want to "round" these numbers to integers while leaving their sum unchanged. In other words, I'm looking for an algorithm that converts the array of floating-point numbers (call it fn) to an array of integers (call it in) such that:

  1. the two arrays have the same length
  2. the sum of the array of integers is N
  3. the difference between each floating-point number fn[i] and its corresponding integer in[i] is less than 1 (or equal to 1 if you really must)
  4. given that the floats are in sorted order (fn[i] <= fn[i+1]), the integers will also be in sorted order (in[i] <= in[i+1])

Given that those four conditions are satisfied, an algorithm that minimizes the rounding variance (sum((in[i] - fn[i])^2)) is preferable, but it's not a big deal.

Examples:

 [0.02, 0.03, 0.05, 0.06, 0.07, 0.08, 0.09, 0.1, 0.11, 0.12, 0.13, 0.14]     => [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1] [0.1, 0.3, 0.4, 0.4, 0.8]     => [0, 0, 0, 1, 1] [0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1]     => [0, 0, 0, 0, 0, 0, 0, 0, 0, 1] [0.4, 0.4, 0.4, 0.4, 9.2, 9.2]     => [0, 0, 1, 1, 9, 9] is preferable     => [0, 0, 0, 0, 10, 10] is acceptable [0.5, 0.5, 11]     => [0, 1, 11] is fine     => [0, 0, 12] is technically not allowed but I'd take it in a pinch 

To answer some excellent questions raised in the comments:

  • Repeated elements are allowed in both arrays (although I would also be interested to hear about algorithms that work only if the array of floats does not include repeats)
  • There is no single correct answer - for a given input array of floats, there are generally multiple arrays of ints that satisfy the four conditions.
  • The application I had in mind was - and this is kind of odd - distributing points to the top finishers in a game of MarioKart ;-) Never actually played the game myself, but while watching someone else I noticed that there were 24 points distributed among the top 4 finishers, and I wondered how it might be possible to distribute the points according to finishing time (so if someone finishes with a large lead they get a larger share of the points). The game tracks point totals as integers, hence the need for this kind of rounding.

For the curious, here is the test script I used to identify which algorithms worked.

like image 874
David Z Avatar asked Apr 27 '09 06:04

David Z


People also ask

How do you round numbers to integers?

Rounding to the Nearest Integer If the digit in the tenths place is less than 5, then round down, which means the units digit remains the same; if the digit in the tenths place is 5 or greater, then round up, which means you should increase the unit digit by one.

How do you round a floating point?

The round() function returns a floating point number that is a rounded version of the specified number, with the specified number of decimals. The default number of decimals is 0, meaning that the function will return the nearest integer.

Why use integers over floats and not just floats all the time?

Another reason to favour integers over floats is performance and efficiency. Integer arithmetic is faster. And for a given range integers consume less memory because integers don't need to represent non-integer values. Another reason is to show intent.

What is the most common method to round numbers?

Rounding Numbers: The Common Method The common rounding method says that if the number you are rounding is followed by the number 5 or above (5,6,7,8,or 9) then you should round that number up. For example, you can round the number 27 to 30 because 7 is greater than 5.


1 Answers

One option you could try is "cascade rounding".

For this algorithm you keep track of two running totals: one of floating point numbers so far, and one of the integers so far. To get the next integer you add the next fp number to your running total, round the running total, then subtract the integer running total from the rounded running total:-

number  running total   integer integer running total    1.3       1.3          1           1    1.7       3.0          2           3    1.9       4.9          2           5    2.2       8.1          3           8    2.8      10.9          3          11    3.1      14.0          3          14 
like image 98
James Anderson Avatar answered Sep 25 '22 18:09

James Anderson