Path: utzoo!attcan!utgpu!jarvis.csri.toronto.edu!rutgers!cs.utexas.edu!uunet!sdrc!scjones From: scjones@sdrc.UUCP (Larry Jones) Newsgroups: comp.lang.c Subject: Re: int divided by unsigned. Message-ID: <748@sdrc.UUCP> Date: 16 Jun 89 21:31:59 GMT References: <13940@lanl.gov> Distribution: na Organization: Structural Dynamics Research Corp., Cincinnati Lines: 31 In article <13940@lanl.gov>, jlg@lanl.gov (Jim Giles) writes: > > [ example of -5 / 1000U -- Cray gets zero instead of the large > number required by ANSI ] > > This > is an example of a case where deviating from the C definition produces > desireable results. I hope Cray doesn't "fix" their C compiler. > > (Note: this behaviour occurs because C requires arguments to be "promoted" > to unsigned if either is already unsigned. The preferable rule would be > that if one argument is an int and the other is an unsigned, _both_ should > be promoted to long before the operator is applied. Unsigned is _not_ a > promotion from int - it is a break-even semantics change.) Well, there're two good reasons why you DON'T want to promote to long: long might well be the same size as int and, if it's not, it may well take a lot longer to compute the answer. In the first case, you get an answer which is no more useful than the unsigned version, in the second case you violate the Spirit of C by doing non-obvious things behind the programmer's back. If you want long division, put in a cast! (Then when it doesn't work because long and int are the same size, people can blame you instead of the ANSI committee :-). ---- Larry Jones UUCP: uunet!sdrc!scjones SDRC scjones@SDRC.UU.NET 2000 Eastman Dr. BIX: ltl Milford, OH 45150-2789 AT&T: (513) 576-2070 "You can't get a body like mine in a bottle -- unless you push REAL HARD." - Judy Tenuta / Dr. Pepper