필터 지우기
필터 지우기

Explain this please?

조회 수: 4 (최근 30일)
Anne Nguyen
Anne Nguyen 2019년 10월 14일
답변: Steven Lord 2019년 10월 14일
x = 2;
e = 2.713;
c = 5 + (x < 2 || (e < pi));
c = 6
How does the value of c=6 after the code is executed? 2 is not less than 2, while e is not less than pi, so that is throwing me off. Thank you!
  댓글 수: 2
Mil Shastri
Mil Shastri 2019년 10월 14일
e = 2.713
pi = 3.1416
thus, (e < pi) indeed is true
Walter Roberson
Walter Roberson 2019년 10월 14일
But e is less than pi. 2.713 < 3.14159265358979323 [etc]

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

채택된 답변

Steven Lord
Steven Lord 2019년 10월 14일
c = 5 + (x < 2 || (e < pi));
Let's look at each section of the code inside the outer parentheses in turn.
x < 2
Since x is equal to 2, x < 2 is false.
(e < pi)
Since e is less than pi, this returns true. [By the way, you can use exp(1) to return the value of the constant e.]
(false || true)
This results in true.
So what is 5 + true? For many intents and purposes in MATLAB, you can think of false like 0 and true like 1. Indexing is the main exception; you can't index into an array with 0 but you can with false. [Search the documentation for "logical indexing" for more information on this.] So 5 + true is essentially 5 + 1 which results in 6.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by