Path: csiph.com!x330-a1.tempe.blueboxinc.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!border3.nntp.dca.giganews.com!Xl.tags.giganews.com!border1.nntp.dca.giganews.com!nntp.giganews.com!local2.nntp.dca.giganews.com!nntp.earthlink.com!news.earthlink.com.POSTED!not-for-mail NNTP-Posting-Date: Tue, 26 Jul 2011 12:48:59 -0500 Date: Tue, 26 Jul 2011 10:48:50 -0700 From: Patricia Shanahan User-Agent: Mozilla/5.0 (Windows NT 5.2; WOW64; rv:5.0) Gecko/20110624 Thunderbird/5.0 MIME-Version: 1.0 Newsgroups: comp.lang.java.programmer Subject: Re: Arithmetic overflow checking References: <015aeb15-57db-48ab-9cd4-77f8448b632f@w24g2000yqw.googlegroups.com> <4e26300b$0$309$14726298@news.sunsite.dk> <4e26b4ed$0$2501$db0fefd9@news.zen.co.uk> <4e28097f$0$2533$da0feed9@news.zen.co.uk> <7a23c9d2-508f-4dbd-af91-8cdf2a9764e1@p29g2000pre.googlegroups.com> In-Reply-To: Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Message-ID: Lines: 53 X-Usenet-Provider: http://www.giganews.com NNTP-Posting-Host: 70.230.200.239 X-Trace: sv3-2QlD8bXLmiZXfz4ynL8bjfWn5BYFdGiVIlBQH7/Fq9pWpaWfkPd6pDo1ADi9LtkTsBt1hgoqJir9tVP!HD4vErqLSYr0E+uBPSq+WUHE4jeP+R6vmMKAnbZKw8OoQHXdwR4ddehFIwDvbXuQcDOJmgscZ6xH!5bhcJ818ZsYxTDnUAOaOIM8tZKdi6DjFVWQmx7he/jZpx9I= X-Abuse-and-DMCA-Info: Please be sure to forward a copy of ALL headers X-Abuse-and-DMCA-Info: Otherwise we will be unable to process your complaint properly X-Postfilter: 1.3.40 X-Original-Bytes: 4619 Xref: x330-a1.tempe.blueboxinc.net comp.lang.java.programmer:6587 On 7/26/2011 8:35 AM, Joshua Cranmer wrote: > On 07/26/2011 04:53 AM, Henderson wrote: >> On 26/07/2011 3:28 AM, Joshua Cranmer wrote: >>> What do you mean by "truncate" in modular arithmetic? Mod 10, 1 and 11 >>> are the same thing (the set of numbers {..., -9, 1, 11, ... }). >> >> As in, if you have a mod10 type, and you assign 36 to it, do you just >> silently accept that and store it internally as 6, or do you throw an >> exception if the input isn't in the range 0 to 9? > > There is nothing inherently correct about the elements in mod 10 > arithmetic having values 0 to 9, so 36 is as valid in mod 10 as 6 is. > I think there may be at least two different models of mod 10 arithmetic in this discussion, but they should lead to the same answer for this issue. The one I prefer is based on equivalence classes: The integers are partitioned into 10 equivalence classes where two integers n and m are in the same class if, and only if, n - m is an integer multiple of 10. Mod 10 arithmetic is a set of operations on those equivalence classes. For example, if x and y are two of the classes, x + y is the class containing the sum of an arbitrary element of x and an arbitrary element of y. It is easy to show that you get the same class for the sum, regardless of the choice of an element of x and an element of y. In this model, there is an obvious conversion from integer to mod 10 - pick the class containing the integer. 36 maps to {..., -4, 6, 16, ...} in exactly the same way as 6 maps to the same class. We cannot store an infinite set, so we need some label to represent each of the equivalence classes. Each contains exactly one of the integers 0 through 9, and we often use that integer to name and represent the class. The usual answer for the conversion of a mod 10 class to an integer is to return the integer in 0 through 9 that is a member of the class. The other view is that mod 10 arithmetic is a form of finite range arithmetic on the integers 0 through 9. x + y is the integer sum of x and y, reduced by subtraction of an integer multiple of 10 to a number in the range 0 through 9. In this model, 36 does not exist in mod 10. However, I still think there is an obvious mapping. Suppose 36 arose as an intermediate result from e.g. 4 * 9 in mod 10 arithmetic. The mod 10 result would be 6. I think that gives a clear model for converting an integer to mod 10 - reduce it to an integer in the range 0 through 9 by subtraction of an integer multiple of 10. Patricia