On 23 December 2010 03:41, Marnen Laibow-Koser <[email protected]> wrote: > TomRossi7 wrote in post #970207: >> Any idea why this calculates the integer the way it does? >> >> irb> ("291.15".to_f * 100.0).to_i >> => 29114 > > Because of the vagaries of floating-point arithmetic: > irb> ("291.15".to_f * 100.0) < 29115 > => true > irb> printf "%.30f", ("291.15".to_f * 100.0) > 29114.999999999996362021192908287048 > > It bears repeating: never, ever use Floats for arithmetic. If you can't > use fixed-point math, then use BigDecimal.
I have to disagree here. I would rephrase it as _almost_ never use Floats The one time when they would be appropriate is if you have a large amount of arithmetic to perform. The sort of thing that might occur in complex statistical analysis for example or engineering calculations of some sort. On most (possibly all) computers floating point consumes vastly less processor time to perform than BigDecimal. This is a rare occurence in Rails apps I expect so I agree that for most people Marnen's advice would be appropriate. Colin -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to [email protected]. To unsubscribe from this group, send email to [email protected]. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

