I don't understand the why the distributive property (in math) is not always true in MatLab?
11 views (last 30 days)
Show older comments
I am using matlab and I have to find an example of when the distributive property does not work. ex. a*(b+c) = a*b + a*c
I don't understand how this can't be true. I have inputted everything I can think of. Thanks for the help.
Accepted Answer
Roger Stafford
on 9 Mar 2014
Edited: Roger Stafford
on 9 Mar 2014
Yes, even the associative law can fail in numerical procedures in the presence of round-off errors. Try this on your matlab computer:
(3/14+15/14)+3/14 == 3/14+(15/14+3/14)
It will be false. It will probably also fail on your favorite decimal calculator.
The cause is, of course, that the round-off errors on the two sides are different, resulting in a tiny difference.
You will have to get used to such phenomena when dealing with numerical computers with limited numbers of digits, either binary or decimal. Matlab's "double" has 53 binary bits of precision and therefore is subject to possible errors in what would be its 16th decimal place (if it used decimals.)
If you are looking for counterexamples to the distributive law, you don't have to look very far. Try this:
a = exp(1);
b = pi;
c = sin(1);
a*(b+c) == a*b+a*c
Added note: You will find that the commutative laws of addition and multiplication do indeed hold in matlab. You will always get a+b == b+a and a*b == b*a. That is implied by the IEEE 754 standard for double precision binary floating point numbers, provided your computer system adheres to that standard.
More Answers (1)
the cyclist
on 8 Mar 2014
You don't really give detail or an example of what you are seeing, but I am guessing you are encountering a floating point arithmetic issue, as described here, and lots of other places.
No, it's not a bug, and no, MATLAB doesn't mess with fundamental mathematics. But doing math on a computer does have some limitations.
0 Comments
See Also
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!