필터 지우기
필터 지우기

How parentheses effects multiplication with pi ?

조회 수: 8 (최근 30일)
Emre Doruk
Emre Doruk 2020년 8월 4일
편집: Bruno Luong 2020년 8월 5일
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일
"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
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일
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
Emre Doruk
Emre Doruk 2020년 8월 5일
Thanks for your clear expleantion.
madhan ravi
madhan ravi 2020년 8월 5일
Thanks Stephen:)

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Dynamic System Models에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by