From: "Henry T. So Jr." Date: 2004-10-02T09:43:03+09:00 Subject: Re: BigDecimal Float worries On Sat, Oct 02, 2004 at 08:09:10AM +0900, Henrik Horneber wrote: > seems a little strange to me. > what's going on? :) > > Henrik > > D:\>ruby -v > ruby 1.8.2 (2004-07-29) [i386-mswin32] You are seeing what I consider a classic problem of computer programming. Floating point numbers are usually represented in computers using a format such as IEEE 754. Basically, there are so many bits to represent the mantissa and so many bits for the exponent. The rub is that everything is in binary. So what can be represented as an exact decimal cannot always be represented exactly in binary (and vice-versa). Conversion of two slightly different floating point representations into a string for display may result in the same printed value because the difference is so insignificant in decimal terms. In school, we were taught never to compare floating point numbers directly because of this. We were taught to compare them to see if they were different by less than some threshold (which we generically called "epsilon"). If the difference was less than epsilon then the numbers were considered "equal" enough. The problem is that epsilon depends on the application (i.e., depending on how much error the calculations were willing to deal with). This problem is also why things like the BigDecimal class are needed. The BigDecimal class stores numbers with arbitrary precision in case the error introduced by IEEE 754 and such formats is too significant in the calculation. Hope that wasn't more confusing... Henry.