The following statements are all true in Ruby 1.8.6:
0.6 - 0.5 < 0.1
(0.29 * 100).to_i == 28
0.28 * 100 != 28
(0.28 * 100).to_i == 28
(0.27 * 100) == 27
Well, what can I say? Floating point errors in Ruby can really bite you. I had always thought higher precision numbers were necessary only when dealing with very small numbers or when computing compound interest, but look out! Ruby floating point can really bite you with some strange, unexpected results. It makes me wonder why Matz included a to_i method at all!
Monday, March 3, 2008
Improbable Truths in Ruby
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment