Skip to main content
Logo image

Subsection B.2 The floor, ceil, and round functions

MATLAB provides three built-in functions for rounding real numbers to integers: floor, ceil, and round. Each function rounds in a different direction, making them useful in different situations.
The floor function rounds a number down to the nearest integerβ€”that is, it returns the greatest integer that is less than or equal to the input. For example, floor(3.7) returns 3 and floor(-2.3) returns -3.
The ceil function (short for ceiling) rounds a number up to the nearest integerβ€”it returns the smallest integer that is greater than or equal to the input. For example, ceil(3.2) returns 4 and ceil(-2.7) returns -2.
The round function rounds to the nearest integer. When the fractional part is exactly 0.5, MATLAB rounds away from zero. For example, round(3.5) returns 4, while round(2.5) returns 3, and round(-2.5) returns -3.
All three functions also accept arrays as input and apply the rounding operation element-wise to every entry. For example:
x = [1.2, 2.7, -0.5, -3.9];
floor(x)   % returns [1, 2, -1, -4]
ceil(x)    % returns [2, 3,  0, -3]
round(x)   % returns [1, 3, -1, -4]
A common use of floor is to compute how many whole units fit into a quantity. For instance, to find out how many complete weeks are in a given number of days:
days = 20;
weeks = floor(days / 7)   % returns 2
The ceil function is often used when you need to allocate enough whole units to cover a quantityβ€”for example, determining how many buses are needed to transport a group:
students = 53;
capacity = 20;
buses = ceil(students / capacity)   % returns 3
The round function is useful when displaying or comparing values that should be rounded to a certain number of decimal places. To round to two decimal places, multiply before rounding, then divide:
x = 3.14159;
round(x * 100) / 100   % returns 3.14

Reading Questions Check Your Understanding

1.

(a) Floor function basic behavior.
What does floor(4.9) return?
  • 4
  • floor always rounds down to the nearest integer, so floor(4.9) is 4.
  • 5
  • floor rounds down, not up. The value 5 would be the result of ceil(4.9).
  • 4.9
  • floor always returns an integer. It does not leave the fractional part unchanged.
  • 0
  • floor returns the greatest integer not exceeding the input, which is 4, not 0.
(b) Ceiling function with negative input.
What does ceil(-1.3) return?
  • -1
  • ceil rounds toward positive infinity. The smallest integer greater than or equal to -1.3 is -1.
  • -2
  • -2 would be the result of floor(-1.3). The ceil function rounds up (toward positive infinity).
  • 1
  • ceil rounds toward positive infinity starting from the input value, not toward the nearest positive integer.
  • 2
  • 2 is too large. ceil(-1.3) returns the smallest integer that is still \(\geq -1.3\text{,}\) which is -1.
(c) Comparing floor and ceil behavior.
Which of the following correctly describes the difference between floor and ceil?
  • floor rounds toward negative infinity; ceil rounds toward positive infinity.
  • Correct. floor always goes down (toward \(-\infty\)), and ceil always goes up (toward \(+\infty\)), regardless of the sign of the input.
  • floor rounds toward zero; ceil rounds away from zero.
  • This is not correct. For negative numbers, floor(-1.3) gives -2, which is away from zero, not toward it.
  • floor rounds down for positive numbers only; ceil rounds up for positive numbers only.
  • Both functions work for all real numbers, positive or negative. floor always rounds toward \(-\infty\) and ceil always rounds toward \(+\infty\text{.}\)
  • floor and ceil produce the same result for non-integer inputs.
  • They produce different results for any non-integer input. For example, floor(2.5) is 2 while ceil(2.5) is 3.
(d) Using ceil for resource allocation.
You have 37 students and each table seats 4. Which expression gives the minimum number of tables needed so every student has a seat?
  • ceil(37 / 4)
  • ceil rounds up, ensuring that the last partial group still gets a full table. ceil(9.25) gives 10.
  • floor(37 / 4)
  • floor(9.25) gives 9 tables, but that only seats 36 students. The remaining student would have no seat.
  • round(37 / 4)
  • round(9.25) gives 9, which is not enough. When you need to cover all items, use ceil to guarantee sufficient capacity.
  • 37 / 4
  • This returns the non-integer 9.25. The number of tables must be a whole number, so you need a rounding functionβ€”specifically ceil.
(e) Floor with negative fractional values.
What is the output of the following code?
x = -2.5;
disp(floor(x))
  • -3
  • floor rounds toward negative infinity. The greatest integer that is \(\leq -2.5\) is -3.
  • -2
  • -2 would be the result of ceil(-2.5). Remember that floor rounds down, which for negative numbers means toward a more negative integer.
  • 2
  • floor does not drop the negative sign. The input is negative, so the output is also negative.
  • 3
  • floor does not drop the negative sign. The input is -2.5, so the output is -3, not 3.
(f) Round half-away-from-zero behavior.
What does round(2.5) return in MATLAB?
  • 2
  • MATLAB’s round resolves exactly-half values by rounding away from zero, not toward the nearest even integer. Since 2.5 is positive, it rounds up to 3.
  • 3
  • MATLAB’s round rounds halfway cases away from zero. Because 2.5 is exactly halfway and positive, it rounds up to 3. Similarly, round(-2.5) returns -3.
  • 2.5
  • round always returns an integer when given a scalar real input; it does not leave a fractional part.
  • 0
  • round(2.5) rounds to the nearest integer, not to zero. The result is 3.
(g) Element-wise rounding on arrays.
Given x = [0.4, 1.5, 2.6, -0.7], what does round(x) return?
  • [0, 2, 3, -1]
  • round operates element-wise: 0.4 β†’ 0, 1.5 β†’ 2 (MATLAB rounds halves away from zero), 2.6 β†’ 3, -0.7 β†’ -1.
  • [0, 1, 2, 0]
  • This looks like truncation toward zero. round rounds to the nearest integer, not toward zero. For example, round(2.6) gives 3, not 2.
  • [1, 2, 3, 0]
  • This would be ceil(x), not round(x). round(0.4) is 0, not 1.
  • [0, 1, 2, -1]
  • This would be floor(x). Note that round(2.6) gives 3, not 2.
(h) Rounding functions with integer inputs.
For any integer n, which of the following is always true?
  • floor(n) == n
  • When the input is already an integer, floor returns the input unchanged. The same is true for ceil and roundβ€”all three functions leave integers unmodified.
  • floor(n) == ceil(n) - 1
  • For integers, floor(n) equals ceil(n) (both return n), so this expression would give n == n - 1, which is false.
  • floor(n) < n
  • floor(n) rounds down only when there is a fractional part. For integers, floor(n) == n, so this strict inequality is false.
  • floor(n) == n - 1
  • floor only subtracts from the value when there is a fractional part to discard. For an integer input, floor(n) == n.
(i) Testing for integer values.
A program needs to check whether a value x is already an integer (has no fractional part). Which expression accomplishes this?
  • floor(x) == x
  • If x has no fractional part, floor(x) returns x unchanged, so the comparison is true. The same check can also be written as x == round(x) or x == ceil(x). In this textbook, we use these patterns assuming that x is an exact integer-valued quantity. In practical numerical code with floating-point values, a more robust approach is to allow for tiny rounding errors, for example by testing abs(x - round(x)) < 1e-10.
  • floor(x) == 0
  • This checks whether x is between 0 and 1 (exclusive), not whether x is an integer.
  • round(x) == 0
  • This checks whether x rounds to zero, not whether it is already an integer.
  • ceil(x) == floor(x) + 1
  • For non-integers, ceil(x) is one more than floor(x), so this expression is true for non-integers, not integers. The opposite of what we want.
You have attempted of activities on this page.