Matlab cast before calculation rule
显示 更早的评论
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 个评论
James Tursa
2013-4-15
Instead of listing pseudo-code as above, please post actual m-code. The rules can vary depending on exactly what you are doing.
采纳的回答
更多回答(0 个)
类别
在 帮助中心 和 File Exchange 中查找有关 Programming 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!