Path: utzoo!utgpu!jarvis.csri.toronto.edu!clyde.concordia.ca!uunet!samsung!zaphod.mps.ohio-state.edu!sunybcs!uhura.cc.rochester.edu!lowj_ltd From: lowj_ltd@uhura.cc.rochester.edu (John Alan Low) Newsgroups: comp.lang.pascal Subject: Re: Problem in Turbo Pascal 4.0 Summary: I understand now, thanks to all who emailed me! Keywords: Nonterminating Repeat loop Message-ID: <4660@ur-cc.UUCP> Date: 30 Dec 89 19:06:58 GMT References: <4658@ur-cc.UUCP> Reply-To: lowj_ltd@uhura.cc.rochester.edu (John Alan Low, aka "Travis" Low) Organization: University of Rochester Lines: 35 In article <4658@ur-cc.UUCP> I wrote: >I am having a problem with Turbo Pascal 4.0. The repeat loop in the following >loop never terminates. >Program Foo; >var x : real; >Begin >x := 0.10; > Repeat > x := x + 0.01; > Until x = 0.30; >End. Thanks to everybody who emailed me on this. I forgot that 0.01 is a repeating decimal in binary. I never will again. For those who are as mystified as I was: Since 0.01 cannot be represented as a non-repeating decimal in binary, there is a floating point roundoff error. So x is never equal to 0.30, just very close. Strangely enough, although I put no formatting in the writeln statement, I missed the error. I don't know if it showed up at all, I was very tired when I wrote it. The two simplest solutions were: 1) replace the terminating condition with "Until x >= 0.30". 2) replace the terminating condition with "Until abs(x - 0.30) < delta", where delta is some predetermined error factor. The best solution in terms of speed was: Make x an integer (start at 10) and increment by 1 each time. Where x is used in the loop, subsitute x/100. Terminate when x = 30. Thanks again for all the help. (Boy, do I feel stupid) ---Travis Ich suche die Leidenschaft, die keine Leiden schafft.