Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are fixed-width integers distributive over multiplication?

For three n-bit signed integers a, b, and c (such as 32-bit), is it always true that a * (b + c) == (a * b) + (a * c), taking into account integer overflow?

I think this is language-independent, but if it's not, I'm specifically interested in the answer for Java.

like image 891
Taymon Avatar asked Jan 07 '13 03:01

Taymon


People also ask

What is distributive property of multiplication in integers?

Distributive Property of Multiplication of Integers Distributive property states that for any expression of the form a (b + c), which means a × (b + c), operand a can be distributed among operands b and c as (a × b + a × c) i.e., a × (b + c) = a × b + a × c.

How do you know if a multiplication will overflow?

If either of the number is 0, then it will never exceed the range. Else if the product of the two divided by one equals the other, then also it will be in range. In any other case overflow will occur.


1 Answers

Yes, it holds, because integer arithmetic is modulo arithmetic over finite rings.

You can see some theoretical discussion here: https://math.stackexchange.com/questions/27336/associativity-commutativity-and-distributivity-of-modulo-arithmetic

like image 89
DuckMaestro Avatar answered Oct 15 '22 20:10

DuckMaestro