2 equal functions, different outputs
情報
この質問は閉じられています。 編集または回答するには再度開いてください。
古いコメントを表示
Hi
I have two functions that are the same. The inputs are 10^-1, 10^-2...10^-14
Why does the first one get funny answers after 10^-7?
Thanks

4 件のコメント
John BG
2017 年 3 月 28 日
have you tried vpa?
vpa(exp(2*x)-(exp(x).*cos(x)).^2)'
ans =
0.012173368402139450594745540001895
0.00010201673337717487299869389971718
0.000001002001667416152486111968755722
0.000000010002000117026454972801730036736
0.00000000010000200667548142519081011414528
0.0000000000010000889005823410116136074066162
0.000000000000010436096431476471479982137680054
0.00000000000000022204460492503130808472633361816
-0.00000000000000022204460492503130808472633361816
0
0
-0.00000000000000022204460492503130808472633361816
0.00000000000000022204460492503130808472633361816
0
>> vpa(exp(2*x)-(exp(2*x).*cos(x)).^2)'
ans =
-0.25555335373862742009976045665098
-0.020505356557536158490506750240456
-0.0020050053356654995440067068557255
-0.00020005000533362959913574741221964
-0.000020000500005057375574324396438897
-0.0000020000049998358804259623866528273
-0.0000002000000498547649385727709159255
-0.000000020000000544584395356650929898024
-0.0000000019999999434361370731494389474392
-0.00000000020000001654807419981807470321655
-0.000000000020000001654807419981807470321655
-0.0000000000019999557565597569919191300868988
-0.00000000000020006218903745320858433842658997
-0.000000000000019984014443252817727625370025635
>>
also, why do you double the x in the 2nd exponential, don't you say the expressions are the same?
John BG
For clarification the formulas as code, not as screenshot, with additional parenthesis:
exp(2*x) - (exp(x) .* cos(x)) .^ 2
exp(2*x) - exp(2*x) .* (cos(x) .^ 2)
Calculating the values in double format at first and using vpa only to expand the displayed digits afterwards does not increase the number of significant digits.
Craig Smith
2017 年 3 月 29 日
Stephen23
2017 年 3 月 29 日
"I have two functions that are the same". Sure, they might be mathematically the same, but when working with finite precision floating point numbers on a computer then you are not working with mathematical operations, but with numeric operations.
"Why does the first one get funny answers..."
They are not funny at all, these are quite reasonable answers for floating point calculations, where the floating point error accumulates with each operation.
And a thousand other pages on the internet which explain floating point numbers.
回答 (1 件)
Walter Roberson
2017 年 3 月 28 日
1 投票
Both versions are correct. When you use floating point numbers, then when you calculate the same expression two different ways, the rounding might come out differently.
Furthermore if you examine the ieee 754 specifications on the required accuracy of each of the floating point operations you will find that for some of the operations the last bit is not required to be correct.
Basically you should consider the last bit of a floating point calculation to generally be random.
0 件のコメント
この質問は閉じられています。
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!