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.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With