Path: utzoo!attcan!uunet!husc6!bloom-beacon!apple!claris!drc From: drc@claris.UUCP (Dennis Cohen) Newsgroups: comp.sys.mac.programmer Subject: Re: Float to integer conversion problem in LSC 3.0? Keywords: Lightspeed, LSC, conversion Message-ID: <4571@claris.UUCP> Date: 8 Aug 88 14:32:38 GMT References: <5813@batcomputer.tn.cornell.edu> Reply-To: drc@claris.UUCP (Dennis Cohen) Organization: Claris Corporation, Mountain View CA Lines: 26 In article <5813@batcomputer.tn.cornell.edu> eacj@tcgould.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. Am I missing >something subtle about C syntax or IEEE type conversions, or is this a bug >in SANE or LSC? The variable values shown in the comments were verified >with the LSC debugger and with my own debugging dialog. If I use the >debugger to force a value of 126.0 into d before the last line is >executed, then n gets the correct result. What's happening here? Well, d is a double that is being assigned the product of two floats, the first of which does not have an exact binary representation. Because of this, d will be 125.999(something). When you now assign d to n (an integer type) it will truncate (not round) to make the assignment. This is not very nice, but it is "correct" behavior (so far as C, Pascal, and most other languages are concerned). Dennis Cohen Claris Corp. ------------ Disclaimer: Any opinions expressed above are _MINE_!