If X is any real number, we write

X or FLOOR(X)= the greatest integer less than or equal to X;
X or CEILING(X)= the least integer greater than or equal to X.


FLOOR: procedure
parse arg F
return TRUNC(F) - (F < 0) * (F <> TRUNC(F))
CEILING: procedure
parse arg C
return TRUNC(C) + (C > 0) * (C <> TRUNC(C))


The program

say FLOOR(-4.1) CEILING(-4)
say FLOOR(4.1) CEILING(4.1)
say FLOOR(4.9) CEILING(4.9)

displays on the screen

4 4
-5 -4
4 5
4 5

If X and Y are any real numbers, we define the following operation:

MOD(X,Y)=X-Y*FLOOR(X/Y) if Y<> 0;


MOD: procedure
parse arg X, Y
if Y = 0 then return X
return X - Y * FLOOR(X/Y)


There are the results of Knuth's excercises 8, 9, 10 in the chapter 1.2.4: say MOD(100,3) MOD(100,7) MOD(-100,7) MOD(-100,0)

          displays 1 2 5 -100

say MOD(5,-3) MOD(18,-3) MOD(-2,-3)

          displays -1 0 -2

say MOD(1.1,1) MOD(0.11,0.1) MOD(0.11,-0.1)

          displays 0.1 0.01 -0.09


  • FLOOR(X)==TRUNC(X), if X>=0
  • In the BINARY_SEARCH algorithm we use the expression (L+R)%2 instead of FLOOR((L+R)/2)
  • The time complexity of the MINIMAX algorithm is CEILING(3*N/2)-2 comparisons, it is equal FORMAT(3*N/2-2,,0) for N>=2. The expression FORMAT(N,,0) rounds N to the nearest integer
  • For real numbers X>=0, Y>0 is X//Y=MOD(X,Y), see Euclid's GCD (and LCM) algorithms

Gerard Schildberger - the CEILING and FLOOR function

Knuth D. E., Fundamental Algorithms, vol. 1 of The Art of Computer Programming
- 2nd ed. Addison-Wesley, 1973

Cover Contents Index Main page

last modified 10th November 2003
Copyright 2000-2003 Vladimir Zabrodsky
Czech Republic