IEEE Arithematic

I have a row vector [3 (6-3e)/(2-2e) 1] and a column vector [3 14 -5-((6-3e)/(2-2e))*14]' where e = 1+0.5*10^-15. When I multiply these two vectors I get a 0 and I don't understand why? The second and third product is equal in magnitude but opposite in sign, so they should cancel out and the answer should be close to 9.This is how matlab computes these two vectors:
row vector = [3.000000000000000e+000 -3.377699720527871e+015 1.000000000000000e+000]
column vector = [ 3.000000000000000e+000 1.400000000000000e+001 4.728779608739018e+016]

 Accepted Answer

James Tursa
James Tursa on 29 Jan 2012

0 votes

Limitations of floating point arithmetic, again. Particularly with matrix and vector operations, you are not guaranteed any particular order of operations in MATLAB. If you want a particular order then you would have to manually write the expression using parentheses. So in general, relatively "small" values can get swamped by relatively "large" values in a computation and lose significance. When you subtract two relatively "large" values the relatively "small" value significance may not be there to recover, and you end up with a lot of garbage bits. I could go through your calculation in detail to explain it, but I think I will leave that up to you instead. Just write out the expression in detail and use the num2strexact utility to examine each intermediate result and then you should see what is going on with the lost precision.

6 Comments

Ashish Bhatt
Ashish Bhatt on 29 Jan 2012
Thanks James. Can you tell me how to make that FEX file work? It is asking me for C/C++ compiler.
James Tursa
James Tursa on 29 Jan 2012
Type the following:
mex -setup
(Then press Enter)
(Then enter the number of a C compiler, such as lcc)
(Then press Enter again)
Then you can try num2strexact again.
Ashish Bhatt
Ashish Bhatt on 29 Jan 2012
Does that mean I need to install a C compiler on my PC? I tried to install a few of them but cannot make that file run. mex-setup doesn't accept lcc.
Are you using 32 bit MATLAB or 64 bit MATLAB? The 64 bit MATLAB does not support lcc.
The supported compilers depend upon your MATLAB version. http://www.mathworks.com/support/sysreq/previous_releases.html
Ashish Bhatt
Ashish Bhatt on 29 Jan 2012
I am using MATLAB 7.5.0.342 (R2007b) on a 64 bit Windows 7 Machine. I am currently downloading intel C++. What should I enter for compiler when mex file prompts me?
James Tursa
James Tursa on 29 Jan 2012
If you download a supported compiler, it should show up in the list when you type mex -setup.

Sign in to comment.

More Answers (0)

Categories

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!