Strange behavior when passing single precision numbers into atan2
Afficher commentaires plus anciens
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
Walter Roberson
le 4 Août 2016
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.
per isakson
le 5 Août 2016
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)
Colby Smith
le 5 Août 2016
dpb
le 5 Août 2016
Wonder how much if not all may not be related to the underlying compiler libraries...so versions used by TMW matter?
Réponses (1)
dpb
le 5 Août 2016
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
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!