Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!mnetor!uunet!husc6!rutgers!cbmvax!fred From: fred@cbmvax.UUCP (Fred Bowen) Newsgroups: comp.sys.cbm Subject: Re: C Power 128 floating point to integer conversion bug Message-ID: <2693@cbmvax.UUCP> Date: Thu, 5-Nov-87 18:01:53 EST Article-I.D.: cbmvax.2693 Posted: Thu Nov 5 18:01:53 1987 Date-Received: Sun, 8-Nov-87 07:10:45 EST References: <2119@gryphon.CTS.COM> <2675@cbmvax.UUCP> Organization: Commodore Technology, West Chester, PA Lines: 37 In article <2675@cbmvax.UUCP>, fred@cbmvax.UUCP (Fred Bowen) writes: > In article <2119@gryphon.CTS.COM>, howie@pnet02.cts.com (Howard Herman) writes: > > Well one source of the prob may relate to the Upgrade ROM's for the > > 128. It was supposed to fix this prob, but it still is in evidence. > > A=8.75:B=INT(A):C=A-B:PRINT C > > Now you would expect to get ".75". However, well I'll let you run it, > > and maybe this is where your prob stems from. > > I think you mis-remembered the magic numbers- the ones don't cause a > problem on either ROM set. If you found some numbers that do not work > on the upgrade ROMs, I'd like to hear about it. > Just found out what you are referring to from Bob Umfer of Plink. The problem was A=8.65 : B=INT(A) : C=A-B : PRINT C and you get .649999999 and not .65 like you'd expect. This is not a bug- it's typical floating point rounding error (.000000001 error is not all that bad, now is it? :-) Actually, most machines will do likewise- the VAX FORTAN compiler, using REAL*4, is actually worse, returning something like .6499996. Now, .65 looks kinda simple to you, but to a computer with a mere 24 bits of precision for the mantissa, it's ugly as hell. For most calculations, this is no big deal. Now, if you want dollars and cents output, use PRINT USING "###.##";C and you will get 0.65, since the formatted output of PRINT USING *rounds* the result to fit your format. If you are using a C64, you'll hafta add a fraction, multiply by 100 and integerize, then divide by 100. Or you could get a C128. Whichever seems easiest... -- -- Fred Bowen uucp: {ihnp4|rutgers|caip}!cbmvax!fred arpa: cbmvax!fred@RUTGERS.EDU tele: 215 431-9100 Commodore Electronics, Ltd., 1200 Wilson Drive, West Chester, PA, 19380