From: Roger Pack Date: 2010-01-20T02:26:36+09:00 Subject: Re: Float Points error > It is true that floats are approximations of real numbers but the issue > that most often trips people up is the conversion between the internal > binary representation and the external decimal representation. More > specifically, by default Ruby doesn't show you the full precision > available in a float: > >>> 1/3.0 > => 0.333333333333333 >>> "%0.60f" % (1/3.0) > => "0.333333333333333314829616256247390992939472198486328125000000" So currently ruby "doesn't show you the real floating point number" which leads to surprising behavior, for example >> (2.0 - 1.1) == 0.9 => false This has been changed in the latest dev trunk, to be more explicit (Float#to_s is totally explicit now). >> 2.0 - 1.1 => 0.8999999999999999 >> 0.9 => 0.9 >> 2.0 - 1.1 == 0.9 => false Unfortunately this means that writing out to the screen used to be >> puts 2.0 - 1.1 0.9 but is now >> puts 2.0 - 1.1 0.8999999999999999 I have a proposal out to make Float#to_s be "the imprecise [old] way" and Float#inspect to be the new, precise way (split the functionality--it's currently the same). Any feedback/preferences on this before I do anything in that regard? -r -- Posted via http://www.ruby-forum.com/.