필터 지우기
필터 지우기

Division in stateflow is showing only integer part

조회 수: 1 (최근 30일)
Aditya
Aditya 2013년 3월 15일
편집: chaitanya balaga 2019년 11월 20일
I am trying to do a simple division operation in stateflow like a=5/2 but the output is coming to 2 instead of 2.5 The data type of a is double. Its rounding the decimal digits and giving me only the integer part. Pls help
  댓글 수: 1
Vishal Rane
Vishal Rane 2013년 3월 15일
Just noticed that using 5.0/2.0 instead of 5/2 gives you 2.5.

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

답변 (3개)

Vishal Rane
Vishal Rane 2013년 3월 15일
편집: Vishal Rane 2013년 3월 15일
I believe typecasting both dividend and divisor as double should do the job.
{
a = double(5)/double(2);
}
Not sure whether this the usual approach.
  댓글 수: 1
Aditya
Aditya 2013년 3월 16일
Thanks Vishal for the answer. But we don't need to typecast both numerator as well as denominator. Typecasting only numerator will do the job. And if we are giving num and deno as input variables from simulink then keeping the data type of those parameters as double will also give write answer

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


Aditya
Aditya 2013년 3월 19일
But still the question remains that why stateflow is doing floor(a/b) and giving 2 instead of 2.5. Is typecasting only solution in this case or is there a specific setting where we can avoid rounding of the integers. Simulink works fine in this case. Problem is only in stateflow.

chaitanya balaga
chaitanya balaga 2019년 11월 20일
편집: chaitanya balaga 2019년 11월 20일
Instead, you can try the following assignment, a:=5/2;
This overrides the floor(a/b) behaviour in stateflow and gives you the desired result which is entirely dependent on the datatype of a. In this case you will get a = 2.5

카테고리

Help CenterFile Exchange에서 Complex Logic에 대해 자세히 알아보기

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by