Applies To:
  • CitectSCADA 5.XX, 6.XX, 7.0

Summary:

In the CSV_Include project, the CSV_Math_Rounddown() and CSV_Math_Truncate() produce the incorrect results when the operand number’s least significant digit is greater than or equal to 5.

That is to say CSV_Math_Rounddown(158.96,0) produces 159 instead of 158. Similarly CSV_Math_Truncate(158.96) produces 159 instead of 158.

158.94 produces the correct result. Using 158.95 produce the correct result because 32bit float its actually 158.949996948242. See Knowledge Base article Q3244.

 

Solution:

This problems exists in CitectSCADA V7 and earlier version projects which utilise the CSV_Include project.

The CSV_Math_RoundDown() and CSV_Math_Truncate() code is located in the CSV_Math.ci file in the CSV_Include project. It is recommended not to change the CSV_Include project but to create your own functions within the application project.

The following functions should be placed in a cicode file within the project and used instead of those provided in CSV_Include project.

REAL FUNCTION My_Math_RoundDown(REAL rValue, INT iDecPlaces) 
    STRING sValue; 
    
REAL rValueRounded;

    sValue = RealToStr(rValue, 1, iDecPlaces + 2); 
    
//RealToStr uses Symmetric Arithmetic Rounding or Round-Half-Up 
    
sValue = StrLeft(sValue, StrLength(sValue) - 2) + "00"; 
    
rValueRounded = StrToReal(sValue);

    RETURN rValueRounded;
END

INT FUNCTION My_Math_Truncate(REAL rValue) 
    
STRING sValue; 
    
INT iValueTruncated;  

    sValue = RealToStr(rValue, 1, 2); 
    
iValueTruncated = StrToInt(sValue); 
    
RETURN iValueTruncated;
END

See article Q3802 for more information about rounding and for alternate Cicode that does not require slow string manipulations.


Keywords:
 

Attachments