Different results between 2013a 32bit and 64bit in single precision
1 次查看(过去 30 天)
显示 更早的评论
I am squaring and summing two numbers in single precision, yet I get different results (one bit) between Matlab 32bit and Matlab 64bit on the same Win64 PC. Moreover, I get different results in Matlab 32bit between multiple implementations of this same equation...
Can anyone offer some insight?
Code to reproduce:
format hex
a = single([-0.1113907,0.64]);
%Method 1 (wrong on 32bit?)
r1 = a(1)^2 + a(2)^2
%Method 2 (wrong on 32bit?)
r2 = a(1)*a(1) + a(2)*a(2)
%Method 3 (correct)
tmp1 = a(1)^2; tmp2 = a(2)^2;
r3 = tmp1 + tmp2
%Method 4 (correct)
r4 = sum(a.^2)
Matlab 32bit results:
r1 =
3ed8116a
r2 =
3ed8116a
r3 =
3ed8116b
r4 =
3ed8116b
Matlab 64bit results:
r1 =
3ed8116b
r2 =
3ed8116b
r3 =
3ed8116b
r4 =
3ed8116b
0 个评论
采纳的回答
Walter Roberson
2013-8-20
Different execution paths and different processors (or even processor modes) have different roundoff effects.
更多回答(1 个)
Ken Atwell
2013-8-21
32-bit and 64-bit versions of MATLAB may use registers of differing widths as related in this 64-bit migration page, with single being called out as being particularly sensitive.
I forget the details, but it may have something to due with legacy 80-bit registers that are perhaps not used by 64-bit versions of MATLAB. 64-bit floating point is said to be more consistent that 32-bit floating point. Your compiler may or may not produce different answers depending on its implementation and the compilation flags you pass to it.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Arithmetic Operations 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!