Path: utzoo!attcan!uunet!lll-winken!lll-tis!ames!mailrus!uflorida!usfvax2!pollock From: pollock@usfvax2.EDU (Wayne Pollock) Newsgroups: comp.sys.mac.programmer Subject: Re: Float to integer conversion problem in LSC 3.0? Keywords: Lightspeed, LSC, conversion Message-ID: <1120@usfvax2.EDU> Date: 22 Aug 88 17:38:15 GMT References: <5813@batcomputer.tn.cornell.edu> <4933@teddy.UUCP> Reply-To: pollock@usfvax2.usf.edu.UUCP (Wayne Pollock) Organization: University of South Florida at Tampa Lines: 27 In article <5813@batcomputer.tn.cornell.edu> (Julian Vrieslander) writes: > > double d; > long m,n; > > m = 126.0; /* m becomes 126 */ > d = 12.6 * 10.0; /* d becomes 126 */ > n = d; /* n becomes 125 !! */ > >I cannot understand why n gets "rounded down" to 125. This is just a result of floating point roundoff error. To make things work right when assigning a float or double to an int or long, always add a small amount like this: n = d + 0.00001; if you want to truncate accurately, or use: n = d + 0.5; or some library routine if you want to round the number. Assuming the default conversions will always work perfectly is just asking for trouble. Wayne Pollock (The MAD Scientist) pollock@usfvax2.usf.edu Usenet: ...!{uflorida, codas}!usfvax2!pollock GEnie: W.POLLOCK