What is the output of following Python code?
import math
print(math.floor(-2.8))
print(math.trunc(-2.8))
print(math.ceil(-2.8))
Solution and Explanation:
The output of the provided Python code is:
-3
-2
-2
Here's a breakdown of each line:
math.floor(-2.8): This line uses the floor function from the math module to round down -2.8 to the nearest integer. Since the largest integer less than or equal to -2.8 is -3, the output is -3.
math.trunc(-2.8): Similar to floor, trunc also rounds down towards zero. However, unlike floor, it truncates the decimal part of the number instead of rounding it. Therefore, math.trunc(-2.8) also outputs -2.
math.ceil(-2.8): This line uses the ceil function, which rounds numbers up to the nearest integer. The smallest integer greater than or equal to -2.8 is -2, so math.ceil(-2.8) outputs -2.
0 Comments:
Post a Comment