Type Casting

Navigation:  Language Reference > Types >

Type Casting

Previous pageReturn to chapter overviewNext page

Type casting is a way to convert a variable from one data type to another data type. For example from real to boolean or from real to integer. Typecasting can be done in equations using the data type between parentheses:

 

variables

 real t;

 boolean b;

equations

 t = if time > 10 then 1.33 else 0 end;

 b = (boolean) t;

 

In the example above the real variable s is converted to the boolean variable b. In 20-sim the C-code style type casting is used. An integer or real value of 0 is converted to True and a non zero value is converted to false.

 

Typecasting is done automatically in 20-sim, but when the data type is not given a warning will be generated:

 

variables

 real s;

 boolean b;

equations

 s = if time > 10 then 1.33 else 0 end;

 b = s;

 

In the example above the warning "Possible loss of data when converting from real to boolean in equation b = s;" will be given. To prevent this warning, use typecasting with parenthesis  as shown in the top example.