RodrigoReis
RodrigoReis

Reputation: 111

Matlab (2008a-32bit) single precision bug

If I make a single precision operation with the values, it will give a result ending with 8:

>> single(single(6.500001e+02)*single(-64.1775131)*single(0.65)*single(2))
ans =   -5.4230008e+004

Then I make any operation using double precision, and the same operation as before, using single precision, the result will be different from the first time I run it:

>> double(6.5000012e+02)*double(-64.1775131)*double(0.65)*double(2)
ans =   -5.423000858119204e+004

>> single(single(6.500001e+02)*single(-64.1775131)*single(0.65)*single(2))
ans =  -5.4230004e+004

This problem happens in Matlab 2008a 32 bits. This is not a problem in Matlab 2012b 64 bits.

Any thoughts on how to avoid this problem?

Thank you.

Upvotes: 5

Views: 137

Answers (1)

user2271770
user2271770

Reputation:

I could not test but, from what I could find on MATLAB Central, it seems to be a bug in the Global Workspace @versions R2008*. So, to avoid the problem:

  • Don't execute code from Command Window;
  • Stick to double precision, unless under severe memory constraints (it's even faster, because default type is double);
  • Work in functions rather than scripts (apparently the function local Workspace is not affected by this bug)
  • Use a R2009+ MATLAB release, which seems to have fixed the bug.

Upvotes: 2

Related Questions