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 件のコメント

Walter Roberson
Walter Roberson 2016 年 8 月 4 日
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
per isakson 2016 年 8 月 5 日
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
Colby Smith 2016 年 8 月 5 日
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
dpb
dpb 2016 年 8 月 5 日
Wonder how much if not all may not be related to the underlying compiler libraries...so versions used by TMW matter?

サインインしてコメントする。

回答 (1 件)

dpb
dpb 2016 年 8 月 5 日

0 投票

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
>>

カテゴリ

ヘルプ センター および File ExchangeGraphics Performance についてさらに検索

製品

タグ

質問済み:

2016 年 8 月 4 日

コメント済み:

dpb
2016 年 8 月 5 日

Community Treasure Hunt

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

Start Hunting!

Translated by