PLCgen generates code for CIF `real` literals that has warnings if the generated values for PLC `REAL`s have more than 7 significant digits
When a REAL
PLC literal with more than 7 significant digits is used for S7-300/400, there are warnings about the accuracy ("The sign or the accuracy of the value may be lost"). Likely S7-1200/1500 have similar issues, as do other PLCs. It likely depends on REAL
vs LREAL
how many significant digits are supported.
Original discussion
This was originally discussed at #1109 (comment 2966166):
I made the test:
Test_#1109_incorporate_scaling.zip
The results are good. However, I have the following observations:
- There are multiple warnings about the accuracy.
First warning relates to:
"DB".a_rawDataA := SEL_DINT(G := DINT_TO_REAL(#trunced) <> #outMin + (#value - #inMin) / (#inMax - #inMin) * (#outMax - #outMin) + 0.5 AND #outMin + (#value - #inMin) / (#inMax - #inMin) * (#outMax - #outMin) + 0.5 < 0.0 AND #outMin + (#value - #inMin) / (#inMax - #inMin) * (#outMax - #outMin) + 0.5 > -2147483648.0, IN0 := #trunced, IN1 := #trunced - 1);
Second warning relates to:
#value := 4.0005787;
- Regarding the 1st warning (and really all the warnings on long lines): can you manually edit the line by removing some different parts to see if you can pinpoint what exactly gives the warning? So, what part of line, what operation(s), what operations on what type of arguments, etc?
- Regarding the 2nd warning: It has no sign, so that is likely not the cause. The value has 8 significant digits. Looking at this page with S7-1200 information about
REAL
, the values-3.402823e+38
,-1.175495e-38
,1.175495e-38
and+3.402823e+38
have at most 7 significant digits. Maybe that is the cause? Can you try rounding to one less digit, so changing the value to4.000579
, to see if then the warning goes away?
OK. Then we just need to figure out the warnings.
There are six warnings given. All these warnings are the same:
One warning is given on:
This warning is give because of implicit conversion of constant. Compare it to the following code without the warning:
And five warnings are given on:
The value
-2147483648.0
is a part of the solution of the (un)scaling.I have given you the cause of the warnings here . However, I do not know the solution or even of these warnings are important.
Regarding the 2nd warning: It has no sign, so that is likely not the cause. The value has 8 significant digits. Looking at this page with S7-1200 information about
REAL
, the values-3.402823e+38
,-1.175495e-38
,1.175495e-38
and+3.402823e+38
have at most 7 significant digits. Maybe that is the cause? Can you try rounding to one less digit, so changing the value to4.000579
, to see if then the warning goes away?Could you still try this?
The value
-2147483648.0
is a part of the solution of the (un)scaling.
-2147483648.0
is the same as-2.1474836480e9
. It has 11 significant digits. Could you try changing it to-2.1474836e9
and-2.147484e9
. I assume that-2.1474836e9
still gives a warning, while-2.147484e9
doesn't. That is, if my assumption about at most 7 significant digits holds. Would be good to confirm that.
If indeed it is about 7 significant digits, we can see how to get rid of the warnings. For some that may be doable, for others more tricky. But, let's first get confirmation that this is indeed the exact issue.
so changing the value to
4.000579
, to see if then the warning goes away?Yes, it does.
I assume that
-2.1474836e9
still gives a warning, while-2.147484e9
doesn't.Your assumption is correct. After the changes, we have no warnings (see below). The results are still correct (see below). I am truly impressed.
OK, good to know. Then I think we should create a separate issue for PLCgen generating code for
real
literals that have warnings due to having more than 7 significant digits forREAL
. That is a separate issue from scaling, and I think we should discuss that separately, to have a focused discussion about it. Also, I think the problems is there even without using thescale
function, for instance if you just assign areal
value to areal
-typed variable in your CIF model that has more significant digits than the PLC'sREAL
type supports.
OK. I have adjusted the test to:
Test_#1109_incorporate_scaling_v2.zip
alg real fixScaleDataB = 4.000579;