Matlab floating point arithmetic and sensitive dependence
Show older comments
As part of a larger project, I have the following simple matrix multiplication:
c = a* b;
Where a is:
Columns 1 through 7
1.000000000000000 0.778800783071405 0.367879441171442 0.105399224561864 0.018315638888734 0.778800783071405
0.606530659712633
Columns 8 through 14
0.286504796860190 0.082084998623899 0.014264233908999 0.367879441171442 0.286504796860190 0.135335283236613
0.038774207831722
Column 15
0.006737946999085
and b is:
1.0e+02 *
1.681770403629075
-3.848571335694688
4.372852859312935
-3.552127988829399
4.433852194145209
-1.091783715227434
2.219644313019083
-1.858966831671062
0.917816691444462
-2.715630209256668
1.794744879202224
-4.126306698348984
4.755107353594159
-3.930478238870790
4.574731559033815
Giving the result for c:
-3.064215547965432e-14
However when I try to replicate this with the following test:
a = [1, 0.778800783071405, 0.367879441171442, 0.105399224561864, 0.018315638888734, 0.778800783071405, 0.606530659712633, 0.28650479686019, 0.082084998623899, 0.014264233908999, 0.367879441171442, 0.28650479686019, 0.135335283236612, 0.038774207831722, 0.006737946999085];
b = [168.1770403629075; -384.8571335694688; 437.2852859312935; -355.2127988829399; 443.3852194145209; -109.1783715227434; 221.9644313019083; -185.8966831671062; 91.7816691444462; -271.5630209256668; 179.4744879202224; -412.6306698348984; 475.5107353594159; -393.0478238870790; 457.4731559033815];
c = a* b;
My result for c is:
-5.813127756937320e-13
I assume this has something to do with floating point arithmetic and despite having the MatLab console set to format long I'm perhaps not seeing the full values of the data I'm then pasting in to my test code. If this is the case how does one work around it?
Thanks in advance
4 Comments
Nicholas Wilton
on 12 Jul 2017
dbmn
on 12 Jul 2017
Are you sure that the input values are the same? I tried to replicate your behaviour by recreating your values and calculating the difference of them (values from your post)
a1=[1.000000000000000 0.778800783071405 0.367879441171442 0.105399224561864 0.018315638888734 0.778800783071405 0.606530659712633 0.286504796860190 0.082084998623899 0.014264233908999 0.367879441171442 0.286504796860190 0.135335283236613 0.038774207831722 0.006737946999085];
a2 = [1, 0.778800783071405, 0.367879441171442, 0.105399224561864, 0.018315638888734, 0.778800783071405, 0.606530659712633, 0.28650479686019, 0.082084998623899, 0.014264233908999, 0.367879441171442, 0.28650479686019, 0.135335283236612, 0.038774207831722, 0.006737946999085];
a2-a1
This gives:
>> a2-a1
ans =
1.0e-15 *
Columns 1 through 13
0 0 0 0 0 0 0 0 0 0 0 0 -0.9992
Columns 14 through 15
0 0
same behaviour with the b values - different input values need to give you different results...
Nicholas Wilton
on 12 Jul 2017
Steven Lord
on 12 Jul 2017
You don't want to use hex2dec there. You want to use hex2num to convert the number as displayed by format hex into its normal numeric form.
>> hex2dec('3fe8ebef9eac820b')
ans =
4.60519003337815e+18
>> hex2num('3fe8ebef9eac820b')
ans =
0.778800783071405
hex2dec computes 3*16^15 + 15*16^14 + 14*16^13 + ... + 11*16^0.
S = '3fe8ebef9eac820b';
D = zeros(size(S));
D(S > '0' & S <= '9') = S(S > '0' & S <= '9') - '0';
D(S >= 'a' & S <= 'f') = S(S >= 'a' & S <= 'f') - 'a' + 10
doubleValue = polyval(D, 16)
isequal(doubleValue, hex2dec(S))
Accepted Answer
More Answers (1)
Walter Roberson
on 12 Jul 2017
0 votes
You need to use num2hex and hex2num with floating point. hex2dec assumes that the hex is integer.
Categories
Find more on Language Support in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!