home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #30 / NN_1992_30.iso / spool / comp / std / c / 3193 < prev    next >
Encoding:
Text File  |  1992-12-12  |  2.4 KB  |  55 lines

  1. Newsgroups: comp.std.c
  2. Path: sparky!uunet!uunet.ca!wildcan!sq!msb
  3. From: msb@sq.sq.com (Mark Brader)
  4. Subject: Re: mathematical modulo operation
  5. Message-ID: <1992Dec12.231115.26787@sq.sq.com>
  6. Organization: SoftQuad Inc., Toronto, Canada
  7. References: <Byx9D9.5v7@jrd.dec.com> <1254@zogwarg.etl.army.mil> <Bz2nuL.130@jrd.dec.com>
  8. Date: Sat, 12 Dec 92 23:11:15 GMT
  9. Lines: 44
  10.  
  11. I've found the persistence of this thread surprising.  To my mind,
  12. the answer to the original question
  13.  
  14. | Is this implementation of the modulo function guaranteed [to work?]
  15. | int mod(int j,int N) {return((((j)%N) < 0) ? ((j) % N)+N : ((j) % N));}
  16.  
  17. is clearly Yes.  The counter-argument
  18.  
  19. > Strictly speaking, the implementation is free to define which of two
  20. > allowed results is produced for % when the divisor is negative.  I don't
  21. > know any implementation where the implementation definition might say that
  22. > the result alternates from one invocation to the next, but it's not clear
  23. > if the standard allows it.
  24.  
  25. seems entirely specious to me.  What section 3.3.5/6.3.5 actually says is
  26. the following; I have added emphasis.
  27.  
  28. #  If either operand is negative, WHETHER THE RESULT of the / operator
  29. #  IS the largest integer less than or equal to the algebraic quotient
  30. #  OR the smallest integer greater than or equal to the algebraic
  31. #  quotient IS IMPLEMENTATION-DEFINED, as is the sign of the result
  32. #  of the % operator.  If the quotient a/b is representable, the
  33. #  expression (a/b)*b + a%b shall equal a.
  34.  
  35. To me this clearly means that the implementation must define it as one
  36. value or the other.  If it was intended that it could vary from one to
  37. another from one instance of the operation to another, then there would
  38. be no point in making it implementation-defined at all; the standard
  39. might as well have said that it was unspecified, the way it does for
  40. evaluation order.
  41.  
  42. By wording it as it does, the standard is, precisely, allowing for
  43. functions such as the one mentioned above.
  44.  
  45. It's still a puzzle to me why, having provided the above definition,
  46. they then proceeded to specify the library functions div() and ldiv()
  47. as they did, but not provide functions that would provide the mathematical
  48. modulo function, which I think is more useful.  But this is incidental.
  49. -- 
  50. Mark Brader            "Could you please continue the petty bickering?
  51. SoftQuad Inc., Toronto         I find it most intriguing."
  52. utzoo!sq!msb, msb@sq.com        -- Data ("Haven", ST:TNG, Tracy Torme)
  53.  
  54. This article is in the public domain.
  55.