From: Mark Hubbart Date: 2004-10-28T17:23:09+09:00 Subject: Re: Rounding error, (100.0 * 9.95).to_i == 994 On Thu, 28 Oct 2004 05:57:53 +0900, Jamis Buck wrote: > trans. (T. Onoma) wrote: > > On Wednesday 27 October 2004 04:36 pm, Hal Fulton wrote: > > | Jason DiCioccio wrote: > > | > I'm confused now.. 100.0 * 9.95 is clearly 995. So what exactly is the > > | > issue with floating point numbers is making this come out to > > | > 994.999999999983 (or wahtever ;))? If every language is plagued by this > > | > problem, then I'd be curious to know the history behind it.. > > | > > > | :) It's not history, it's math. > > | > > | Here's the short explanation. > > | > > | Remember repeating decimals, which you learned about in elementary school? > > | For example, 1/3 = 0.33333... can't be expressed in a finite number of > > | digits. > > > > Not exactly, '1/3' is finite. > > ... That's not what I learned in school. How many decimal digits does it > take to express 1/3, then? Reconfigure your misconceptions: decimal numeric notation has no basis in reality. It is simply a way for humans to write down numbers. Decimal, like binary, is a notation, a way to represent finite numbers. It can not represent all finite numbers satisfactorily, though; the notation has it's failings. One of the failings is shown when representing finite rational numbers where the denominator is not solely a multiple of powers of 2 and 5 (the factors of ten). 1/5 => 0.2 1/2 => 0.5 1/4 => 0.25 1/7 => 0.142857142857143.... (whoops) In binary notation, you can only have finite representations of rational numbers whose denominators are powers of two (the factors of... two). The examples above translated into simple binary math: 1/101 => 0.0011001100110011.... (repeats) 1/10 => 0.1 1/100 => 0.01 1/111 => 0.0010010010010010.... (repeating) To further confuse the subject. What you are representing when you write the decimal number 42.23 is the rational number, 4223/100; aka 42 23/100, forty-two and twenty-three hundredths. That is a fraction; a rational number. Decimal notation is simply a more convenient way of writing it down, for making calculations or keeping records or whatever. In closing: Decimal numbers are made up! They aren't real! They are all in your head! ;) cheers, Mark ps: I know this doesn't give a straight answer to your question. I'm just arguing out that the question is somewhat irrelevant, since 1/3 is a rational number, and decimal notation is a flawed way of representing rational numbers. A convenient way, but flawed nonetheless.