REAL tag in function - curious transformation

When I put a real tag into my Cicode function the value like 0.127 I see in Debugger Stack Windows 1.226999998 .

What is it ? I tried some real values and got the same result

The trouble is with only tag.

If I put a raw value  0.127 to the CiCode function I see 0.127 in Debugger ...

At the picture below you can see a simple CiCode wich shows the problem is in Citect, not in input pag .....

I noticed it on my input pad ...

Parents
  • This is because Citect (and most software) stores floating point values in the IEEE 32-bit floating point number format. It's stored as a number * 10 to a power, which is converted to a decimal value to display it. Unfortunately, some decimal values can't be stored exactly and will be adjusted up or down slightly and you end up with lots of unwanted decimal places. See TN4858. Note that Cicode REAL variables are 64-bit floating point numbers so they have much better precision and you won't see as much rounding as you'll see with PLC variables or other variable tags.

    In your tag definition you should set the tag format. Then you don't have to set it in each graphic object and it should be automatically set when it is passed to the FormNumPad function. For example: 

    tag1 = FormNumpad("Enter value", tag1, 0);

    You should only set it manually in the graphic object if you need to override the default format to display more or less precision in one or two places. If you cannot use the default format from the tag definition, you can override the format in the call to Formnumpad. For example:

    tag1 = FormNumpad("Enter value", RealToStr(tag1, 5, 4), 0);

    or, use the format operator:

    tag1 = FormNumpad("Enter value", tag1:#.####, 0);

Reply
  • This is because Citect (and most software) stores floating point values in the IEEE 32-bit floating point number format. It's stored as a number * 10 to a power, which is converted to a decimal value to display it. Unfortunately, some decimal values can't be stored exactly and will be adjusted up or down slightly and you end up with lots of unwanted decimal places. See TN4858. Note that Cicode REAL variables are 64-bit floating point numbers so they have much better precision and you won't see as much rounding as you'll see with PLC variables or other variable tags.

    In your tag definition you should set the tag format. Then you don't have to set it in each graphic object and it should be automatically set when it is passed to the FormNumPad function. For example: 

    tag1 = FormNumpad("Enter value", tag1, 0);

    You should only set it manually in the graphic object if you need to override the default format to display more or less precision in one or two places. If you cannot use the default format from the tag definition, you can override the format in the call to Formnumpad. For example:

    tag1 = FormNumpad("Enter value", RealToStr(tag1, 5, 4), 0);

    or, use the format operator:

    tag1 = FormNumpad("Enter value", tag1:#.####, 0);

Children
No Data