Path: utzoo!attcan!uunet!tut.cis.ohio-state.edu!uc!cs.umn.edu!ux.acs!vw.acs.umn.edu!dhoyt From: dhoyt@vw.acs.umn.edu Newsgroups: comp.sys.mac.programmer Subject: Re: Why can't the Mac add? Message-ID: <2257@ux.acs.umn.edu> Date: 24 Sep 90 18:52:04 GMT Sender: news@ux.acs.umn.edu Organization: University of Minnesota, Academic Computing Services Lines: 16 >As you can see, it ought to stop when x==10.0. However, >it actually runs in an infinite loop. This is because >x never equals exactly 10.0. The +0.2 always makes x >equal to 9.999999 or 10.000001 or something. Changing >x from a float to a double makes x stay closer to the >correct value, but it still isn't exactly correct, and >as the loop runs longer, x gets off more and more. >So my question is, why can't the Mac do simple math? 0.2 is a continuing fraction in base 2. Just like 1/3 can't be expressed in base ten notation, 1/5, 1/10 and other numbers cann't be represented in base 2. Some machines have some very special checks to handle 1/10th nicely, but you should never count on it. Real programmers use >=, even for integer loop counters. Paranoia, it's not just a state of mind, it's a job. david paul hoyt | dhoyt@vx.acs.umn.edu | dhoyt@umnacvx.bitnet