From: "Matthias Wächter" Date: 2007-09-04T06:53:53+09:00 Subject: Re: wierd floating point output Stefan Rusterholz schrieb: > Matthias Wächter wrote: >> What I mean is that any float stored as a IEEE 754 double (64 bit), >> like 0.1 with hex notation 0x3FB999999999999A should have a distinct >> string representation such that (a==b) == (a.to_s==b.to_s). Note >> that the next higher double 0.1+2.0**-56 with hex notation >> 0x3FB999999999999B has the same .to_s representation, which is not good. > I disagree. As long as you're calculating, you are working with floats > anyway, so you have maximum precision. Comparing floats should be done > using delta comparison anyway too. Which, as discussed in the 0.06 thread, is hard to accomplish nevertheless. Anyway. > Comparing floats as string is slightly put stupid. It was the code meaning of "if two numbers are identical, I want to see a difference in the string representation, too). Not more. For _any_ other (primary) object/class this holds true, but not for floats. This is unacceptable, period. Suppose Array#inspect would only output the first three and the final element, putting a "..." inbetween. > For printing a result it only makes sense to print > a maximum precision for very very rare cases. In the most cases, > printing with 5, 6 places is enough. If you need more you more, you can > always resort to sprintf/String#% What brings me/us away from this argument to the primary question how precise the sprintf/String#% can output strings. Do you have any feelings about the top posting as well? Does noone care about on some platforms, Ruby is not able to correctly output floating points with the asked precision? what does ruby -e 'print "%.60f" %0.1' output on your platform? Again, it's not a matter of taste how a floating point value is transferred back to decimals. This can be done unambiguously, especially if enough positions after decimal point are requested like in "print %.60f". - Matthias