SIMOTION SCOUT V4.1 SP2: 常数表达式的计算结果

n

问题:

在SCOUT V4.1 SP2(或更新版本)中是否修改了常数的计算结果?n

回答:

从SCOUT V4.1 SP2开始修正了编译器的错误,因此会导致不同的编译行为。n

之前常数表达式的计算结果是不正确的,并且也与IEC不一致。这在SCOUT V4.1 SP2中被修改,但在Readme文件没有更新。n

只要带常数的源程序被编译,编译器输出一个报警16016表明用做计算的数据类型。n

注:n

如果使用"Save and compile"指令,那么未产生变化的代码将不会被重新编译,即只有修改过的代码才会被编译.n

通过"Accept and compile"和"Save and compile all afresh"指令可编译所有代码.n

为了更好的理解,请阅读如下的解释:n

改正错误之前的例子:n

"FONT-SIZE: 8pt; FONT-FAMILY: " color:blue? New?; Courier>PROGRAM"FONT-SIZE: 8pt; FONT-FAMILY: " Courier New??>test_old

VAR
lr : LREAL;
ui : UINT;
di : INT;
END_VARn

"FONT-SIZE: 8pt; FONT-FAMILY: " Courier New??> lr := 5/3; // Warning Calculated as USINT; Result -> 1.66
lr := -5/3; // Warning Calculated as SINT; Result -> -1n

"FONT-SIZE: 8pt; FONT-FAMILY: " Courier New??> ui := 3 - 127; // Warning Calculated as USINT; Result -> 65412
ui := 3 - 128; // Warning Calculated as USINT; Result -> 131n

"FONT-SIZE: 8pt; FONT-FAMILY: " Courier New??> ui := 127 + 127 + 3; // Warning Calculated as USINT; Result -> 257
ui := 128 + 127 + 3; // Warning Calculated as USINT; Result -> 2n

"FONT-SIZE: 8pt; FONT-FAMILY: " Courier New??> di := 127 + 127 + 3; // Warning Calculated as USINT; Result -> 257
di := 128 + 127 + 3; // Warning Calculated as USINT; Result -> 2n

"FONT-SIZE: 8pt; FONT-FAMILY: " Courier New??> lr := 5.0/3.0; // Uncritical: Calculated as LREAL; Result -> 1.66
lr := -5.0/3.0; // Uncritical: Calculated as LREAL; Result -> -1.66n

"FONT-SIZE: 8pt; FONT-FAMILY: " Courier New??>END_PROGRAM

改正错误之后的例子:n

"FONT-SIZE: 8pt; FONT-FAMILY: " color:blue? New?; Courier>PROGRAM"FONT-SIZE: 8pt; FONT-FAMILY: " Courier New??>test_fix

VAR
lr : LREAL;
ui : UINT;
di : INT;
END_VARn

"FONT-SIZE: 8pt; FONT-FAMILY: " Courier New??> lr := 5/3; // Warning Calculated as SINT; Result -> 1
lr := -5/3; // Warning Calculated as SINT; Result -> -1n

"FONT-SIZE: 8pt; FONT-FAMILY: " Courier New??> ui := 3 - 127; // Warning Calculated as USINT; Result -> 132
ui := 3 - 128; // Warning Calculated as USINT; Result -> 131n

"FONT-SIZE: 8pt; FONT-FAMILY: " Courier New??> ui := 127 + 127 + 3; // Warning Calculated as USINT; Result -> 1
ui := 128 + 127 + 3; // Warning Calculated as USINT; Result -> 2n

"FONT-SIZE: 8pt; FONT-FAMILY: " Courier New??> di := 127 + 127 + 3; // Warning Calculated as SINT; Result -> 1
di := 128 + 127 + 3; // Warning Calculated as USINT; Result -> 2n

"FONT-SIZE: 8pt; FONT-FAMILY: " Courier New??> lr := 5.0/3.0; // Uncritical: Calculated as LREAL; Result -> 1.66
lr := -5.0/3.0; // Uncritical: Calculated as LREAL; Result -> -1.66n

"FONT-SIZE: 8pt; FONT-FAMILY: " Courier New??>END_PROGRAMn

如果用符号常数(数据格式在声明中被指定)或者用相应的表达式定义(通过表达式“#”),则不会发生修改后的行为。这种情况下,不会输出报警16016 。
常数的数据类型可以直接用前缀“#”(见下面的例子)。n

如果想要得到和旧编码器相同的计算结果(最好是确定的行为),可以在代码中做如下修改:n

"FONT-SIZE: 8pt; FONT-FAMILY: " color:blue? New?; Courier>PROGRAM"FONT-SIZE: 8pt; FONT-FAMILY: " Courier New??>test
VAR
lr : LREAL;
ui : UINT;
di : INT;
END_VARn

"FONT-SIZE: 8pt; FONT-FAMILY: " Courier New??> "FONT-SIZE: 8pt; FONT-FAMILY: " Courier New??>lr"FONT-SIZE: 8pt; FONT-FAMILY: " Courier New??>:= LREAL#5/3; // Calculated as LREAL; Result -> 1.66
lr := -5/3; // Need not be adapted Calculated as SINT
// Result -> -1; Alternative: lr := SINT#-5/3n

"FONT-SIZE: 8pt; FONT-FAMILY: " Courier New??> ui := UINT#3 - 127; // Calculated as UINT; Result -> 65412
ui := 3 - 128; // Need not be adapted Calculated as USINT
// Result -> 131;Alternative: ui := USINT#3 - 128

"FONT-SIZE: 8pt; FONT-FAMILY: " Courier New??> ui := UINT#127 + 127 + 3; // Calculated as UINT; Result -> 257
ui := 128 + 127 + 3; // Need not be adapted Calculated as USINT;
// Result -> 2; Alternative: ui := USINT#128 + 127 + 3;

"FONT-SIZE: 8pt; FONT-FAMILY: " Courier New??> di := INT#127 + 127 + 3;// Calculated as INT; Result -> 257
di := 128 + 127 + 3; // Warning Calculated as USINT; Result -> 2;"FONT-SIZE: 8pt; FONT-FAMILY: " Courier New?;color:#009933?>
// Alternative: ui := USINT#128 + 127 + 3;

"FONT-SIZE: 8pt; FONT-FAMILY: " Courier New??> lr := 5.0/3.0; // Uncritical: Calculated as LREAL; Result -> 1.66
lr := -5.0/3.0; // Uncritical: Calculated as LREAL; Result -> -1.66

"FONT-SIZE: 8pt; FONT-FAMILY: " color:blue? New?; Courier>END_PROGRAM

结论:
如果通过设置数据类型为操作数定义了计算类型,可以在所有的编译器版本下得到同样的结果。

如果要求作为浮点数计算,建议用带小数点的常数。这样也可以增加代码的可读性,因为可以清楚地看出编程者使用的格式。

#Ende#


© Siemens AG, 1998, 2000

原创文章,作者:ximenziask,如若转载,请注明出处:https://www.zhaoplc.com/plc334235.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2020年11月5日
下一篇 2021年4月12日

相关推荐

发表回复

登录后才能评论