Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!mnetor!seismo!mcvax!ukc!its63b!hwcs!zen!frank From: frank@zen.UUCP (Frank Wales) Newsgroups: comp.arch Subject: Re: Optimization vs. the programmer Message-ID: <596@zen.UUCP> Date: Thu, 23-Apr-87 22:07:57 EDT Article-I.D.: zen.596 Posted: Thu Apr 23 22:07:57 1987 Date-Received: Sun, 26-Apr-87 04:41:59 EDT References: <479@danews.ATT.COM> <3300003@uiucdcsm> <3135@jade.BERKELEY.EDU> <775@killer.UUCP> Reply-To: frank@zen.UUCP (Frank Wales) Organization: Zengrange Limited, Leeds, England Lines: 57 Keywords: HP-41 HP-71 MOD Summary: which mod? In article <775@killer.UUCP> jfh@killer.UUCP (John Haugh) writes: >Third grade math time ... > Try an example ... > 23 / 8 = 2 r 7 right? > Now the tricky one ... > -23 / 8 = -2 r -7 <- my intuition says 'Yes'. > >This is the way I understand. Do the expression (in integer math) > > Remainder = Dividend - (Quotient * Divisor) > >So - > Remainder = -23 - (-2 * 8) = -23 - (-16) = -7. > >But how come when I ask my HP-41CV what it thinks, I get 1? If I ask for >INT of -23 / 8 I get -2, not -3. (Yes, -3 is floor (-23.0 / 8.0)). ... >- John (jfh@killer.UUCP) [this is a bit of a digression, but...] The problem is that there is more than one way of computing the mod function, and each has an effect on the outcome with negative numbers. The HP-41 implements MOD as: MOD(Y,X) = Y - MINT(Y/X)*X where MINT(Y/X) is the maximum integer not larger than Y/X. Special cases are where X==0 (result is Y) and Y==0 (result is 0). [1] The HP-71 (the HP-41's big cousin) actually offers three different remainder functions to get around this problem: MOD(X,Y), which works like the 41's MOD; RED(X,Y), which evaluates X - Y * N, where N is the nearest integer to X/Y -- it is the remainder function defined as REM in the IEEE Floating Point Standard; RMD(X,Y), which evaluates X - Y * IP(X/Y), where IP is the Integer Part of (X/Y) [as would be found by INT on the HP-41] -- it is the remainder function defined as REM in the ANSI BASIC Standard. As implemented on the HP-71, X==Infinity or Y==0 yield an Invalid Arg error.[2] Your mission, should you choose to accept it, is to decide which of these the % operator in C represents (or even, which you think it should represent :-). { References: [1] -- Extend Your HP-41 by W.A.C. Mier-Jedrzejowicz, Ph.D., p 53 [2] -- HP-71 Reference Manual, pp 190, 240, 252. } [digression ends...] Frank Wales [frank@zen.uucp<->..!mcvax!ukc!zen.co.uk!frank] Development Engineer, Zengrange Ltd, Greenfield Rd, Leeds, England, LS9 8DB.