mean without float conversion
def mean(array)
array.inject(0) { |sum, x| sum += x } / array.size.to_f
end
def mean_without_float_conversion(array)
array.inject(0) { |x, sum| sum += x } / array.size
end
require 'rational'
numbers = [Rational(2,3), Rational(3,4), Rational(6,7)]
puts mean(numbers)
puts mean_without_float_conversion(numbers)
Related examples in the same category