Ruby Float vs. Fixnum gotcha

Be careful when doing some computation with integers (Fixnum in Ruby). If what you want as a result is supposed to be a float (Float) at least one of the operands has to be float as well.

irb> 1/2
=> 0

irb> 1.to_f/2
=> 0.5

irb> 1/2.to_f
=> 0.5
blog comments powered by Disqus