How parentheses effects multiplication with pi ?

Hi,
I am trying to generate sine wave. I am usning following two code lines. But they are slightly different (about e-15). Why is it happenning ? What is the differences of two lines;
f0=5e2;
fs=500e2;
len=3e3;
dt=1/fs;
t=0:dt:(len-1);
sing1= sin (2*pi*f0*t);
sing2= sin(2*pi*(f0*t));
isequal(sing1,sing2)
Thanks for your help,

 採用された回答

Bruno Luong
Bruno Luong 2020 年 8 月 5 日
編集済み: Bruno Luong 2020 年 8 月 5 日

0 投票

"While floating-point addition and multiplication are both commutative (a + b = b + a and a × b = b × a), they are not necessarily associative. That is, (a + b) + c is not necessarily equal to a + (b + c). ... "
  • "(2*pi*f0*t)" interpreted by MATLAB as ((2*pi)*f0)*t
  • "2*pi*(f0*t)" interpreted by MATLAB as (2*pi)*(f0*t)
As floating-point multiplication is NOT ASSOCIATIVE, both results might be different.

5 件のコメント

Emre Doruk
Emre Doruk 2020 年 8 月 5 日
Thanks for your answer. But I have another one now, Which one is more accurate for simulating a sine wave ? Which one I should go for ?
Bruno Luong
Bruno Luong 2020 年 8 月 5 日
編集済み: Bruno Luong 2020 年 8 月 5 日
Hard to tell, IMO (2*pi)*(f0*t) is better since f0*t make multiplcation of 2 quantities of inverse unit 1/s and s.
The first one if rad/s and s, so less homogeneous. But I guess both is OK.
Another reason I vote for (2*pi)*(f0*t) is that for some implement of trigonometric functions, it checks input for roundness of pi/2, pi, 2*pi, so it's better you multiply (2*pi) as last operation.
If your code is sensitive to such small errors, then it's not stable and might be you have to revise the algorithm itself.
Note that (floating) multiplication of three numbers a*b*c where at least one is power of 2 is always associative.
Emre Doruk
Emre Doruk 2020 年 8 月 5 日
Thanks for your answer, again. I can see your point.
Bruno Luong
Bruno Luong 2020 年 8 月 5 日
Sometime this non-associativity is a real headeach, such as finding a limits of a sign- alternate series by partial sum, or integral of an oscillated signal (any wave propagation SW might encounters this). There is a real challenge to know what really the limits and the order of the sum can make the result change widely.
Emre Doruk
Emre Doruk 2020 年 8 月 5 日
Actually I realized it recently, I thougth this problem is about pi. Because it is irrational and maybe MATLAB coverge it sth. I asked for this reason but I see that it is more common from this. I should check all of my old codes which needs exact values. Genereally it can be accept like precision mistake but sometimes I really need exact value.

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

その他の回答 (1 件)

madhan ravi
madhan ravi 2020 年 8 月 4 日

0 投票

In the first the order of operation is from left to right.
In the second the order of operation is inside the parenthesis and then the outer.

7 件のコメント

madhan ravi
madhan ravi 2020 年 8 月 4 日
BODMAS
Emre Doruk
Emre Doruk 2020 年 8 月 5 日
Thaks for your answer.
I think not about that. Because it is the same oparetaion, left to right or the right to left.
Let f0=5e3;
t=1:1:100;
sing1= sin (2*pi*f0*t);
sing2= sin(2*pi*(f0*t));
fo*t and (f0*t) gives same output. So they are same vector. How same vector gives different output? The difference is about 3e-14 but still it is different.
madhan ravi
madhan ravi 2020 年 8 月 5 日
編集済み: madhan ravi 2020 年 8 月 5 日
Click on the tag floating-point. And read my answer once again.
Emre Doruk
Emre Doruk 2020 年 8 月 5 日
I did not understant what you mean. Variable format is already double.
Stephen23
Stephen23 2020 年 8 月 5 日
編集済み: Stephen23 2020 年 8 月 5 日
"Because it is the same oparetaion, left to right or the right to left. "
No, that is incorrect.
In general operations on binary floating point numbers are NOT associative:
A classic example of this is called catastrophic cancelation:
>> (1 + 1e100) - 1e100
ans =
0
>> 1 + (1e100 - 1e100)
ans =
1
"Why is it happenning?"
Because of the well-documented properties of binary floating point numbers.
Emre Doruk
Emre Doruk 2020 年 8 月 5 日
Thanks for your clear expleantion.
madhan ravi
madhan ravi 2020 年 8 月 5 日
Thanks Stephen:)

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

カテゴリ

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

質問済み:

2020 年 8 月 4 日

編集済み:

2020 年 8 月 5 日

Community Treasure Hunt

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

Start Hunting!

Translated by