' ******************************************************************
' Rounding functions (contributed by R. Keeling)
' ******************************************************************

FUNCTION Round (x AS DOUBLE, Digit_Count AS INTEGER = 0) AS DOUBLE 
   
   CONST MaxRoundPlaces = 16 
   
   DIM AS DOUBLE  ReturnAnswer, Dec_Place
   DIM AS INTEGER N
   
   IF (Digit_Count >= 0) AND (Digit_Count < MaxRoundPlaces) THEN 
     N = Digit_Count
   ELSE
     N = 0
   END IF
   
   Dec_Place = 10 ^ N
   ReturnAnswer = INT((ABS (x) * Dec_Place) + 0.5) 
   Round = SGN(x) * ReturnAnswer / Dec_Place 
END FUNCTION 

FUNCTION Ceil (x AS DOUBLE) AS INTEGER 
   DIM ReturnAnswer AS INTEGER 
   ReturnAnswer = INT(x) 
   IF ReturnAnswer < x THEN ReturnAnswer = ReturnAnswer + 1 
   Ceil = ReturnAnswer 
END FUNCTION 

FUNCTION Floor (x AS DOUBLE) AS INTEGER 
   DIM ReturnAnswer AS INTEGER 
   ReturnAnswer = INT(x) 
   IF ReturnAnswer > x THEN ReturnAnswer = ReturnAnswer - 1 
   Floor = ReturnAnswer 
END FUNCTION 

