mean without float conversion : Utility Method « Method « Ruby






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

1.Taking Mean
2.Track Median
3.Track Mode
4.mean and standard deviation
5.Converting Between Degrees and Radians