Re: BUG! Erratic for loop behavior
- Posted by Rolf Schröder <r.schr at t-online.de> Mar 23, 2002
- 460 views
Andy Serpa wrote: > ... > ------------------------------ > for i = 31.1 to 34.2 by .1 do > ? i > end for > ------------------------------ > stops at 34.1 (!), NOT as expected. > Hi Andy, that's ok. Look at this: for i = 31.1 to 34.1 by 0.1 do printf(1,"%20.15f\n",i) end for and you will notice why: the decimal given value 0.1 is exactly representable in the computer for it has to stores it internally as an binary value with a limited number of bits. Actually, 0.1 (dec.) is has an unlimited binary representation: 0.0001100110011..... . This is the correct way to do it (have in mind that 31.1 and 34.1 are also not exactly representable!): for i = 311 to 341 by 1 do printf(1,"%20.15f\n",i/10) end for Have a nice day, Rolf