Filter löschen
Filter löschen

Division in stateflow is showing only integer part

1 Ansicht (letzte 30 Tage)
Aditya
Aditya am 15 Mär. 2013
Bearbeitet: chaitanya balaga am 20 Nov. 2019
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 Kommentar
Vishal Rane
Vishal Rane am 15 Mär. 2013
Just noticed that using 5.0/2.0 instead of 5/2 gives you 2.5.

Melden Sie sich an, um zu kommentieren.

Antworten (3)

Vishal Rane
Vishal Rane am 15 Mär. 2013
Bearbeitet: Vishal Rane am 15 Mär. 2013
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 Kommentar
Aditya
Aditya am 16 Mär. 2013
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

Melden Sie sich an, um zu kommentieren.


Aditya
Aditya am 19 Mär. 2013
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 am 20 Nov. 2019
Bearbeitet: chaitanya balaga am 20 Nov. 2019
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

Kategorien

Mehr zu Complex Logic finden Sie in Help Center und File Exchange

Tags

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by