What is the output of the following Python code? import math; print(math.floor(-2.5), math.ceil(-2.5))
Correct answer A. -3 -2
Explanation
math.floor() returns the largest integer less than or equal to x, and for -2.5 that is -3, because -3 lies below -2.5 on the number line. math.ceil() returns the smallest integer greater than or equal to x, which is -2. Thinking of floor as dropping the decimal part gives the wrong answer for negatives.
Answer checked How we write and check questions
Discussion