Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Posting-Version: version B 2.10.1 6/24/83; site digi-g.UUCP Path: utzoo!linus!decvax!tektronix!uw-beaver!cornell!vax135!houxm!ihnp4!stolaf!umn-cs!digi-g!brian From: brian@digi-g.UUCP (Brian Westley) Newsgroups: net.lang Subject: Summary of modulus function Message-ID: <220@digi-g.UUCP> Date: Thu, 6-Sep-84 12:14:57 EDT Article-I.D.: digi-g.220 Posted: Thu Sep 6 12:14:57 1984 Date-Received: Sun, 16-Sep-84 12:24:58 EDT Organization: DigiGraphic Systems Corp., Mpls. MN Lines: 22 Here is a summary of the responses I received about the 'C' & Pascal & Modula, etc. modulus functions. I asked if (A mod B) should always fall within 0..B-1 even with negative A. It seems that the usual mathematical definition of modulus says yes, while most programming languages say no. Most programming languages really use remainder, such that: (A div B) * B + (A mod B) = A or: (A mod B) = A - (A div B) * B (This still depends on whether div truncates towards zero, or always down) Anyway, most languages return -2 for (-2 mod 3), while math modulus would say (-2 mod 3) is 1. Also, in math modulus, if B is negative, the answer is in B+1..0 Some langs don't allow B (or even A) to be negative. Wouldn't it be nice to have a modulus function, a remainder function, and two integer division functions, one truncating towards zero, the other always truncating to largest integer <= A/B ? I'm tired of having to do ((A+B) mod B) to get modulus to work the way I want it to....