Matlab cast before calculation rule
Ältere Kommentare anzeigen
I have been tracing some floating point precision issue in my design. Found out, mixing single and double types are sometime confusing. Is there are general rule for cast before math or there is certain property function can set global rule.
I found out 2 scenarios in my design:
The final results are all double,
1. [double vector] = [single vector] + [double vector], the double is cast to single before the math is performed (simulink fcn function)
2. [single vector] = [single vector] + [double vector];
[double_vector] = [single vector];
the double is not cast to single before the math is performed (matlab code)
Example:
A = 192;
B = A - 7.165700000000015; % Original B value is single type not representable, the math seems to give the B value correctly. eps(B) = 2.842170943040401e-14
C = A - B
ans =
7.165700000000015
double(single(A) - B) % this is neither single - single nor double - double.
ans =
7.165699958801270 % my matlab code gets this
double(single(A) - single(B))
ans =
7.165695190429688 % my simulin fcn code gives this
Please comment,
especially if I need fcn code to behavior exactly as matlab code how to prevent B from been cast to single before doing the math?
1 Kommentar
James Tursa
am 15 Apr. 2013
Instead of listing pseudo-code as above, please post actual m-code. The rules can vary depending on exactly what you are doing.
Akzeptierte Antwort
Weitere Antworten (0)
Kategorien
Mehr zu Programming finden Sie in Hilfe-Center und File Exchange
Produkte
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!