1.
(a) Floor function basic behavior.
What does
floor(4.9) return?
-
4 -
flooralways rounds down to the nearest integer, sofloor(4.9)is4. -
5 -
floorrounds down, not up. The value5would be the result ofceil(4.9). -
4.9 -
flooralways returns an integer. It does not leave the fractional part unchanged. -
0 -
floorreturns the greatest integer not exceeding the input, which is4, not0.
(b) Ceiling function with negative input.
What does
ceil(-1.3) return?
-
-1 -
ceilrounds toward positive infinity. The smallest integer greater than or equal to-1.3is-1. -
-2 -
-2would be the result offloor(-1.3). Theceilfunction rounds up (toward positive infinity). -
1 -
ceilrounds toward positive infinity starting from the input value, not toward the nearest positive integer. -
2 -
2is 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.
-
floorrounds toward negative infinity;ceilrounds toward positive infinity. -
Correct.
flooralways goes down (toward \(-\infty\)), andceilalways goes up (toward \(+\infty\)), regardless of the sign of the input. -
floorrounds toward zero;ceilrounds away from zero. -
This is not correct. For negative numbers,
floor(-1.3)gives-2, which is away from zero, not toward it. -
floorrounds down for positive numbers only;ceilrounds up for positive numbers only. -
Both functions work for all real numbers, positive or negative.
flooralways rounds toward \(-\infty\) andceilalways rounds toward \(+\infty\text{.}\) -
floorandceilproduce the same result for non-integer inputs. -
They produce different results for any non-integer input. For example,
floor(2.5)is2whileceil(2.5)is3.
(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) -
ceilrounds up, ensuring that the last partial group still gets a full table.ceil(9.25)gives10. -
floor(37 / 4) -
floor(9.25)gives9tables, but that only seats 36 students. The remaining student would have no seat. -
round(37 / 4) -
round(9.25)gives9, which is not enough. When you need to cover all items, useceilto 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βspecificallyceil.
(e) Floor with negative fractional values.
What is the output of the following code?
x = -2.5;
disp(floor(x))
-
-3 -
floorrounds toward negative infinity. The greatest integer that is \(\leq -2.5\) is-3. -
-2 -
-2would be the result ofceil(-2.5). Remember thatfloorrounds down, which for negative numbers means toward a more negative integer. -
2 -
floordoes not drop the negative sign. The input is negative, so the output is also negative. -
3 -
floordoes not drop the negative sign. The input is-2.5, so the output is-3, not3.
(f) Round half-away-from-zero behavior.
What does
round(2.5) return in MATLAB?
-
2 -
MATLABβs
roundresolves exactly-half values by rounding away from zero, not toward the nearest even integer. Since2.5is positive, it rounds up to3. -
3 -
MATLABβs
roundrounds halfway cases away from zero. Because2.5is exactly halfway and positive, it rounds up to3. Similarly,round(-2.5)returns-3. -
2.5 -
roundalways 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 is3.
(g) Element-wise rounding on arrays.
-
[0, 2, 3, -1] -
roundoperates 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.
roundrounds to the nearest integer, not toward zero. For example,round(2.6)gives3, not2. -
[1, 2, 3, 0] -
This would be
ceil(x), notround(x).round(0.4)is0, not1. -
[0, 1, 2, -1] -
This would be
floor(x). Note thatround(2.6)gives3, not2.
(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,
floorreturns the input unchanged. The same is true forceilandroundβall three functions leave integers unmodified. -
floor(n) == ceil(n) - 1 -
For integers,
floor(n)equalsceil(n)(both returnn), so this expression would given == 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 -
flooronly 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
xhas no fractional part,floor(x)returnsxunchanged, so the comparison is true. The same check can also be written asx == round(x)orx == ceil(x). In this textbook, we use these patterns assuming thatxis 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 testingabs(x - round(x)) < 1e-10. -
floor(x) == 0 -
This checks whether
xis between 0 and 1 (exclusive), not whetherxis an integer. -
round(x) == 0 -
This checks whether
xrounds to zero, not whether it is already an integer. -
ceil(x) == floor(x) + 1 -
For non-integers,
ceil(x)is one more thanfloor(x), so this expression is true for non-integers, not integers. The opposite of what we want.
