79658564

Date: 2025-06-09 08:16:10
Score: 0.5
Natty:
Report link
FUNCTION ST_FX : SFLOAT
VAR_INPUT
    X : SFLOAT; // Input value
    XPoints : ARRAY[1..10] OF SFLOAT; // X axis values
    YPoints : ARRAY[1..10] OF SFLOAT; // Y axis values
END_VAR
VAR_OUTPUT
    Y : SFLOAT; // Output interpolated value
END_VAR
VAR
    i : SFLOAT; // Loop counter in SFLOAT
    iIndex : INT; // Temporary integer index
END_VAR

// Clamp values outside the range
IF X <= XPoints[1] THEN
    Y := YPoints[1];
ELSIF X >= XPoints[10] THEN
    Y := YPoints[10];
ELSE
    i := 1.0;
    WHILE i <= 9.0 DO
        iIndex := REAL_TO_INT(i); // Convert SFLOAT to INT
        IF (X >= XPoints[iIndex]) AND (X < XPoints[iIndex + 1]) THEN
            Y := YPoints[iIndex] + 
                 ((YPoints[iIndex + 1] - YPoints[iIndex]) / 
                 (XPoints[iIndex + 1] - XPoints[iIndex])) * 
                 (X - XPoints[iIndex]);
            EXIT;
        END_IF;
        i := i + 1.0;
    END_WHILE;
END_IF;

// Return the result
ST_FX := Y;
END_FUNCTION
Reasons:
  • Long answer (-0.5):
  • Has code block (-0.5):
  • Unregistered user (0.5):
  • Low reputation (1):
Posted by: vaibhav kumbhar