0.0
No commit activity in last 3 years
No release in over 3 years
Sum of rounded elements in the array should be equal the rounded sum of elements
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
 Dependencies
 Project Readme

smart_rounding

If you have an array of float numbers and you need to round them in such way: Sum of all rounded elements are equal the rounded sum of elements

What issues does it solve

basic_array = [3.3, 3.3, 3.3] If you will round every element in basic_array, you will get rounded_array = [3, 3, 3] Sum of elements in basic_array is equal 9.9 and if we round it we will get 10 Sum of elements in rounded_array is equal 9 and if we round it we will get 9 10 != 9 So here is the question. How should we round elements in the array, so their sum will be equal to the rounded sum?

Installation

gem 'smart_rounding'

bundle install

Examples

require 'smart_rounding'

array  = [3.3, 3.3, 3.3]
sr = SmartRounding.new(array)

sr.rounded_array() #=> [4, 3, 3]
sr.rounded_sum()   #=> 10
sr.rounds_equal?() #=> true



array = [1.23, 2.54, 3.1, 9.33, 1.2, -1.34, 4.89, 9, 81.1]
sr = SmartRounding.new(array)

sr.rounded_array() #=> [1, 3, 3, 9, 1, -1, 5, 9, 81] 
sr.rounded_sum()   #=> 111 
sr.rounds_equal?() #=> true