Strange behavior when passing single precision numbers into atan2

When I pass two single-precision arguments into the atan2 function, the result is different if I assign the output to a variable compared with just allowing it to be assigned to ans. If I run the following code:
a = single(1.2345678);
b = single(1.3456789);
atan2(a,b)
c = atan2(a,b)
This is displayed to the workspace:
ans =
7.4236256e-01
c =
7.4236250e-01
Why is there a discrepancy between these two numbers?

4 commentaires

Which MATLAB version are you using, on which operating system?
Also, what is your "format" set to? I do not get that format of output for any of the "format" options I try.
format hex
a = single(1.2345678);
b = single(1.3456789);
atan2(a,b)
c = atan2(a,b)
version
outputs
ans =
3f3e0b79
c =
3f3e0b79
ans =
9.0.0.341360 (R2016a)
i.e. identical values (on Win7)
I'm using R2014b and Windows 7. Format is longe. If I use hex:
format hex
a = single(1.2345678);
b = single(1.3456789);
atan2(a,b)
c = atan2(a,b)
The following is output:
ans =
3f3e0b79
c =
3f3e0b78
If I use double precision:
format hex
a = 1.2345678;
b = 1.3456789;
atan2(a,b)
c = atan2(a,b)
The answers match:
ans =
3fe7c16f1bd9f8ff
c =
3fe7c16f1bd9f8ff
Wonder how much if not all may not be related to the underlying compiler libraries...so versions used by TMW matter?

Connectez-vous pour commenter.

Réponses (1)

single doesn't have anything to do with it (albeit the precision of the input values is about that of single precision in decimal digits)...
>> sprintf('%.7e\n',atan2(a,b))
ans =
7.4236256e-01
>> sprintf('%.7e\n',c)
ans =
7.4236250e-01
I'm guessing OP had to have used something like the above to see the result as shown, Walter.
The answer likely has to do with Matlab displaying a result directly to the workspace from the FPP vis a vis a variable store as the difference is on order of double resolution...
>> eps(c)
ans =
5.9605e-08
>>

Catégories

En savoir plus sur Graphics Performance dans Centre d'aide et File Exchange

Produits

Tags

Commenté :

dpb
le 5 Août 2016

Community Treasure Hunt

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

Start Hunting!

Translated by