Re: BUG! Erratic for loop behavior
- Posted by Dan Moyer <DANIELMOYER at prodigy.net> Mar 24, 2002
- 486 views
Andy, Got it. I think Kat made a suggestion once for doing "string math" to avoid this problem in general, don't remember if there was a problem or downside other than speed. If it's always/only add/subtract dollars & cents, though, mult/divide everything that's dollars by 100 should work, but multiplication (like but not limited to percents) could become a problem, as Chris pointed out that my solution won't work for 7 places to right of decimal (his mod of his method works up to 9 I think). And I agree that this should be mentioned in the manual, you caught it in time, others might not, although that brings up the question "who reads the fine manual?". :) (actually I refer to it constantly, but that's bad memory at work!) Dan ----- Original Message ----- From: "Andy Serpa" <renegade at earthling.net> To: "EUforum" <EUforum at topica.com> Sent: Saturday, March 23, 2002 10:24 PM Subject: RE: BUG! Erratic for loop behavior > > > Dan Moyer wrote: > > Andy, > > > > Although I assume this is at least obvious, you could just do this: > > > > for i = 10*31.1 to 10*34.2 do > > ?i/10 > > end for > > > > Same for whatever max "level" of floating point you have, ie, if *any* > > value > > goes out to .001,multiply all by 1000, then divide, etc. > > > > > Maybe there's some good reason that doesn't occur to me offhand why this > > won't work? > > > > No, that works fine for getting this particular loop to work. But the > problem outside of a loop as well. Let's say you make an application > that adds and subtracts dollars & cents a lot. > > .1 + .1 + .1 + .1 + .1 + .1 + .1 + .1 + .1 does not equal 1 -- just try > it. It will display as one if you print it, but if you do this: > > x = .1 + .1 + .1 + .1 + .1 + .1 + .1 + .1 + .1 > if x = 1 then > puts(1,"TRUE.") > else > puts(1,"FALSE.") > end if > > It will come up false. > > So if you have a program that does any kind of floating-point > calculations, and then check to see if a particular value = some other > value, you'll often get the wrong answer. > > I just didn't realize before it was so serious. Those of use who don't > come from a low-level programming background or don't have a lot of > hardware knowledge aren't told this anywhere. It really ought to be in > the manual.... > > > >