Different results between 2013a 32bit and 64bit in single precision
조회 수: 3 (최근 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에서 Logical에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!