Matlab floating point arithmetic and sensitive dependence

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 Commenti

Further to the above. Now if I want to convert Hex output which for my matrix 'a' example in 'format hex' is:
3ff0000000000000
3fe8ebef9eac820b
3fd78b56362cef38
3fbafb718e8457f7
3f92c155b8213cf4
3fe8ebef9eac820b
3fe368b2fc6f960a
3fd25618372a584f
3fb50385c094f424
3f8d369111221751
3fd78b56362cef38
3fd25618372a584f
3fc152aaa3bf81cc
3fa3da368521902d
3f7b993fe00d5376
Then with:
x = hex2dec('3fe8ebef9eac820b');
I get:
K>> x
x =
4.605190033378149e+18
Which is most definitely not 0.778800783071405
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...
No, the values aren't the same. I'm using the console to pull the numbers from the larger project and then pasting them into my test code. I believe the console is truncating the true values (hence why I started looking at the hex above) such that when I run my test code I get a different answer.
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))
On the other hand, hex2num interprets those 16 hex digits as an IEEE 754 double precision number.

Accedi per commentare.

 Risposta accettata

Jan
Jan il 12 Lug 2017
Modificato: Jan il 12 Lug 2017
The only problem is concealed in the sentence:
However when I try to replicate this with the following test: ...
Even with format long g the output to the command window is not exact. A conversion to the decimal output as text, than copy&pasting the data and converting them back to binary form can cause differences. So better post the values as MAT file and do not copy&paste inputs or outputs to compare the results.

4 Commenti

Thanks for the help, definitely some form of truncation is happening.
I'm not very familiar with the mat file format, is it possible to export it to plain text or at least get the data into something readable by C++ for example?
Do not export numerical data to a readable text file, because this would cause the same troubles as all text conversions of binary floating point numbers.
If you want to read the data from C++, create a binary file:
fid = fopen(FileName, 'w');
if fid == -1, error('Cannot open file: %s', FileName); end
fwrite(fid, a, 'double');
fclose(fid);
Such binary files can be imported directly in C++ and other programming languages.
The MAT file export was thought to post the data in the forum, such that a problem (if there is one) can be replicated.
Sorry, at time of writing I wasn't 100% sure that the console was truncating the original values. Now that I can read the binary file I can see that it is. Thanks for the help.
If you are on OS-X, you can use techniques such as
fprintf('%.999g\n', TheNumber)
to see the exact decimal representation of the number.
If you are on Linux, you can get a number of digits but if I recall correctly it starts going wrong after a while.
If you are on MS Windows then this will not get you useable extra digits :(
There is a File Exchange contribution str2numexact that will handle the conversion on all systems.

Accedi per commentare.

Più risposte (1)

You need to use num2hex and hex2num with floating point. hex2dec assumes that the hex is integer.

Prodotti

Community Treasure Hunt

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

Start Hunting!

Translated by