4

A ring is a standard mathematical structure describing objects which can be added and multiplied. Do C# and Java signed longs obey all the properties of a ring? For example, is multiplication by Long.MIN_VALUE always associative and distributive? Assume we are in an unchecked context.


(definition copied from Wikipedia)

A ring is a set R equipped with binary operations + and · satisfying the following three sets of axioms, called the ring axioms.

  1. R is an abelian group under addition, meaning that
    • (a + b) + c = a + (b + c) for all a, b, c in R (+ is associative).
    • a + b = b + a for all a, b in R (+ is commutative).
    • There is an element 0 in R such that a + 0 = a for all a in R (0 is the additive identity).
    • For each a in R there exists −a in R such that a + (−a) = 0 (−a is the additive inverse of a).
  2. R is a monoid under multiplication, meaning that:
    • (a ⋅ b) ⋅ c = a ⋅ (b ⋅ c) for all a, b, c in R (⋅ is associative).
    • There is an element 1 in R such that a ⋅ 1 = a and 1 ⋅ a = a for all a in R (1 is the multiplicative identity).
  3. Multiplication is distributive with respect to addition:
    • a ⋅ (b + c) = (a ⋅ b) + (a ⋅ c) for all a, b, c in R (left distributivity).
    • (b + c) ⋅ a = (b ⋅ a) + (c ⋅ a) for all a, b, c in R (right distributivity).

A commutative ring is one where multiplication is commutative (meaning a ⋅ b = b ⋅ a).

Robert Cooper
  • 1,270
  • 9
  • 11

2 Answers2

2

At least by Java's overflow rules, addition, subtraction, and multiplication for signed longs are exactly equivalent to what you get if you treat the 64 bits as 64-bit unsigned values, which is the same as treating them as integers mod 2^64, which should suffice to prove the claim.

Louis Wasserman
  • 191,574
  • 25
  • 345
  • 413
2

On platforms were signed values that overflow are defined as wrapping, signed and unsigned values will behave in isomorphic fashion when fed to the +, -, *, &, ^, |, <<, and ~ operators, or when performing an unchecked cast to a smaller type. They will behave differently when used with the relational operators, >>, %, and / operators, as well as when casting or promoting to larger types.

Because unsigned values of any given size will behave as a ring, signed values will do so as well. Note that because of implicit promotion to int, smaller types may not necessarily behave as arithmetic rings because applying + and * to some values of such a type may yield something that is not a value of that type.

supercat
  • 77,689
  • 9
  • 166
  • 211