Different results between 2013a 32bit and 64bit in single precision

2 visualizzazioni (ultimi 30 giorni)
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

Risposta accettata

Walter Roberson
Walter Roberson il 20 Ago 2013
Different execution paths and different processors (or even processor modes) have different roundoff effects.
  1 Commento
Jonathan Currie
Jonathan Currie il 21 Ago 2013
While that makes sense, I repeated this problem in C using x86 and x86-64 builds on the same PC and got the same solution (bit identical). Therefore differences in instruction sets should also show between these builds? I have both versions of Matlab using only one core (by setting the affinity) - no threading effects.
Could you expand perhaps what is going on a little more by Matlab? The issue is repeatable across multiple computers, operating systems and processor variations.

Accedi per commentare.

Più risposte (1)

Ken Atwell
Ken Atwell il 21 Ago 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.
  1 Commento
Jonathan Currie
Jonathan Currie il 21 Ago 2013
Thanks Ken I'm happy with that explanation. I know the 80-bit registers are about, but I thought with larger width AVX/AVX2 registers in the later Intel Core series these had gone. Prof. Kahan's paper is also a good read on this topic (although quite dated): http://www.cs.berkeley.edu/~wkahan/MxMulEps.pdf
Also, I was using the default floating point flag (precise) in VC++, as described in the following article: http://msdn.microsoft.com/en-us/library/aa289157(v=vs.71).aspx
I would presume Matlab and its numerical libraries would use the same (or equivalent Intel C++ flags)?

Accedi per commentare.

Categorie

Scopri di più su Arithmetic Operations in Help Center e File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by