Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #6587

Re: Arithmetic overflow checking

Date 2011-07-26 10:48 -0700
From Patricia Shanahan <pats@acm.org>
Newsgroups comp.lang.java.programmer
Subject Re: Arithmetic overflow checking
References (17 earlier) <slrnj2qjgv.6gl.avl@gamma.logic.tuwien.ac.at> <j0k14r$7rr$1@speranza.aioe.org> <j0lqb6$lh$1@dont-email.me> <j0lv9r$l57$1@speranza.aioe.org> <j0mmsn$b6e$1@dont-email.me>
Message-ID <QdudnSIccYaWY7PTnZ2dnUVZ_smdnZ2d@earthlink.com> (permalink)

Show all headers | View raw


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

Back to comp.lang.java.programmer | Previous | NextPrevious in thread | Next in thread | Find similar


Thread

Re: Arithmetic overflow checking Arne Vajhøj <arne@vajhoej.dk> - 2011-07-19 20:54 -0400
  Re: Arithmetic overflow checking markspace <-@.> - 2011-07-19 18:07 -0700
    Re: Arithmetic overflow checking Arne Vajhøj <arne@vajhoej.dk> - 2011-07-19 21:31 -0400
      Re: Arithmetic overflow checking Arved Sandstrom <asandstrom3minus1@eastlink.ca> - 2011-07-20 07:36 -0300
        Re: Arithmetic overflow checking RedGrittyBrick <RedGrittyBrick@spamweary.invalid> - 2011-07-20 11:58 +0100
          Re: Arithmetic overflow checking lewbloch <lewbloch@gmail.com> - 2011-07-20 09:51 -0700
            Re: Arithmetic overflow checking RedGrittyBrick <RedGrittyBrick@spamweary.invalid> - 2011-07-21 12:11 +0100
              Re: Arithmetic overflow checking Martin Gregorie <martin@address-in-sig.invalid> - 2011-07-21 12:43 +0000
                Re: Arithmetic overflow checking Tom McGlynn <taqmcglynn@googlemail.com> - 2011-07-21 07:15 -0700
              Re: Arithmetic overflow checking lewbloch <lewbloch@gmail.com> - 2011-07-21 07:35 -0700
                Re: Arithmetic overflow checking Andreas Leitgeb <avl@gamma.logic.tuwien.ac.at> - 2011-07-21 15:38 +0000
                Re: Arithmetic overflow checking lewbloch <lewbloch@gmail.com> - 2011-07-21 09:03 -0700
                Re: Arithmetic overflow checking Joshua Cranmer <Pidgeot18@verizon.invalid> - 2011-07-21 12:00 -0700
                Re: Arithmetic overflow checking Andreas Leitgeb <avl@gamma.logic.tuwien.ac.at> - 2011-07-22 17:16 +0000
                Re: Arithmetic overflow checking David Lamb <dalamb@cs.queensu.ca> - 2011-07-23 11:28 -0400
                Re: Arithmetic overflow checking Andreas Leitgeb <avl@gamma.logic.tuwien.ac.at> - 2011-07-23 21:03 +0000
                Re: Arithmetic overflow checking Henderson <h1@g1.f1> - 2011-07-23 22:55 -0400
                Re: Arithmetic overflow checking Patricia Shanahan <pats@acm.org> - 2011-07-24 09:16 -0700
                Re: Arithmetic overflow checking markspace <-@.> - 2011-07-24 10:40 -0700
                Re: Arithmetic overflow checking Patricia Shanahan <pats@acm.org> - 2011-07-24 10:54 -0700
                Re: Arithmetic overflow checking markspace <-@.> - 2011-07-24 11:09 -0700
                Re: Arithmetic overflow checking Patricia Shanahan <pats@acm.org> - 2011-07-24 12:53 -0700
                Re: Arithmetic overflow checking markspace <-@.> - 2011-07-24 15:15 -0700
                Re: Arithmetic overflow checking Patricia Shanahan <pats@acm.org> - 2011-07-24 15:41 -0700
                Re: Arithmetic overflow checking Henderson <h1@g1.f1> - 2011-07-25 03:21 -0400
                Re: Arithmetic overflow checking Patricia Shanahan <pats@acm.org> - 2011-07-25 00:56 -0700
                Re: Arithmetic overflow checking Arved Sandstrom <asandstrom3minus1@eastlink.ca> - 2011-07-25 07:03 -0300
                Re: Arithmetic overflow checking Thomas Richter <thor@math.tu-berlin.de> - 2011-07-26 09:43 +0200
                Re: Arithmetic overflow checking Andreas Leitgeb <avl@gamma.logic.tuwien.ac.at> - 2011-07-25 11:06 +0000
                Re: Arithmetic overflow checking Henderson <h1@g1.f1> - 2011-07-25 11:12 -0400
                Re: Arithmetic overflow checking Patricia Shanahan <pats@acm.org> - 2011-07-25 09:09 -0700
                Re: Arithmetic overflow checking Patricia Shanahan <pats@acm.org> - 2011-07-25 09:30 -0700
                Re: Arithmetic overflow checking David Lamb <dalamb@cs.queensu.ca> - 2011-07-25 13:33 -0400
                Re: Arithmetic overflow checking "John B. Matthews" <nospam@nospam.invalid> - 2011-07-26 03:04 -0400
                Re: Arithmetic overflow checking Joshua Cranmer <Pidgeot18@verizon.invalid> - 2011-07-26 03:28 -0400
                Re: Arithmetic overflow checking Henderson <h1@g1.f1> - 2011-07-26 04:53 -0400
                Re: Arithmetic overflow checking Joshua Cranmer <Pidgeot18@verizon.invalid> - 2011-07-26 11:35 -0400
                Re: Arithmetic overflow checking Patricia Shanahan <pats@acm.org> - 2011-07-26 10:48 -0700
                Re: Arithmetic overflow checking Arne Vajhøj <arne@vajhoej.dk> - 2011-07-21 17:00 -0400
        Re: Arithmetic overflow checking Arne Vajhøj <arne@vajhoej.dk> - 2011-07-20 19:50 -0400
          Re: Arithmetic overflow checking Arved Sandstrom <asandstrom3minus1@eastlink.ca> - 2011-07-20 23:21 -0300
            Re: Arithmetic overflow checking Martin Gregorie <martin@address-in-sig.invalid> - 2011-07-21 12:52 +0000
              Re: Arithmetic overflow checking Henderson <h1@g1.f1> - 2011-07-21 15:58 -0400
            Re: Arithmetic overflow checking Arne Vajhøj <arne@vajhoej.dk> - 2011-07-21 17:06 -0400
      Re: Arithmetic overflow checking Gene Wirchenko <genew@ocis.net> - 2011-07-20 14:35 -0700
        Re: Arithmetic overflow checking Arne Vajhøj <arne@vajhoej.dk> - 2011-07-20 18:22 -0400
          Re: Arithmetic overflow checking Gene Wirchenko <genew@ocis.net> - 2011-07-21 14:54 -0700

csiph-web