Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!hellgate.utah.edu!cs.utexas.edu!uunet!cs.dal.ca!silvert From: silvert@cs.dal.ca (Bill Silvert) Newsgroups: comp.lang.pascal Subject: Re: Problem in Turbo Pascal 4.0 Summary: Don't check for equality with reals Message-ID: <1989Dec30.135702.12086@cs.dal.ca> Date: 30 Dec 89 13:57:02 GMT References: <4658@ur-cc.UUCP> Sender: silvert@cs.dal.ca.UUCP (Bill Silvert) Reply-To: bill@biomel.UUCP Organization: Habitat Ecology Div., Bedford Inst. of Oceanography Lines: 28 In article <4658@ur-cc.UUCP> lowj_ltd@uhura.cc.rochester.edu (John Alan Low, aka "Travis" Low) writes: >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. You should never check for equality with reals. Computers do their math in binary, not decimal, and 0.30 is a continuing binary fraction. Change the test to: > Until x >= 0.30; or, better, to > Until x >= 0.2999; -- Bill Silvert, Habitat Ecology Division. Bedford Institute of Oceanography, Dartmouth, NS, Canada B2Y 4A2 UUCP: ...!{uunet,watmath}!dalcs!biomel!bill Internet: bill%biomel@cs.dal.CA BITNET: bill%biomel%dalcs@dalac