From: Rick DeNatale Date: 2010-05-12T22:40:10+09:00 Subject: Re: ruby 1.9+ , floats, and decimal On Tue, May 11, 2010 at 8:05 PM, botp wrote: > On Wed, May 12, 2010 at 12:05 AM, Rick DeNatale wrote: >> Neither floats (regardless of base) nor rationals can represent all >> real numbers.  Each pair of consecutive float values has an infinite >> number of reals which fall in the crack between them.  Neither can >> exactly represent irrational values including Pi, e, sqrt(2) etc. etc. > > i hope you are not exaggerating ;-) > everyone knows that pi or e or sqt(2) or 1/3 cannot be express exactly > in decimal form. And the first three can't be represented numerically exactly in a finite space in any base. > the problem w current float representation is that they balk in plain > and simple addition and subtraction operation :( > can we not just fix that at least ? > If I knew how, I'd be on a private island somewhere. Floats have been around for a long time, FORTRAN was born in 1954. I've been programming for about 71% of FORTRANs life span, and been keeping an eye on Computer Science since then. Floats will always be floats, and have the properties of floats. Computer floats are really just the same kind of representation used on slide rules with more digits. The real use case for floats is to represent numbers with a very wide range of magnitudes, from very large to very small in a limited number of digits/bytes. This is also the purpose of scientific notation. Slide rule operators generally work with a maximum of 3 digits, and slide rules had enough precision to serve the purpose of engineers for decades. When you add 1.2E10 and 9.9E-2 and only preserve only 3 digits you get 1.2E10. With more digits theres still a point where a smaller addend gets lost. The properties of float representations have been studied for over 50 years, and although they are well understood by those who have actually studied them, and IEEE has standardized their behavior, they will always hold surprises to those who casually expect them to always get the 'right' results. It's not that the results aren't right, but that they are sometimes surprising to a naive observer. -- Rick DeNatale Blog: http://talklikeaduck.denhaven2.com/ Github: http://github.com/rubyredrick Twitter: @RickDeNatale WWR: http://www.workingwithrails.com/person/9021-rick-denatale LinkedIn: http://www.linkedin.com/in/rickdenatale