Skip to content

Instantly share code, notes, and snippets.

@heftig
Last active December 11, 2015 06:38
Show Gist options
  • Save heftig/4560494 to your computer and use it in GitHub Desktop.
Save heftig/4560494 to your computer and use it in GitHub Desktop.
class ErrorRounder
def initialize
@error = 0.0
end
def round(x, n=0)
out = (x - @error).round(n)
@error += out - x
out
end
def self.split(total, items, n=0)
e = new
Array.new(items) { e.round(total.to_f/items, n) }
end
end
a = ErrorRounder.split 101, 3, 2
p a
p a.inject(:+)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment