Different results between 2013a 32bit and 64bit in single precision
    8 views (last 30 days)
  
       Show older comments
    
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 Comments
Accepted Answer
  Walter Roberson
      
      
 on 20 Aug 2013
        Different execution paths and different processors (or even processor modes) have different roundoff effects.
More Answers (1)
  Ken Atwell
    
 on 21 Aug 2013
        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.
See Also
Categories
				Find more on Logical in Help Center and File Exchange
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

