Python math.floor syntax
math.floor(x)
In Python, math.floor rounds toward negative infinity. The floor of 2.3 is 2, the floor of −2.3 is −3, and 2 stays 2.
math.floor
import math
print(math.floor(2.8))
2
math.floor(x)
| Input | A finite real number in these examples. Import math first. |
|---|---|
| Result | An int for built-in integers and finite floats. Infinity raises OverflowError and NaN raises ValueError. |
A test bench increases a reading by one tenth of a unit at a time. Watch where the rounded display changes as the sample passes from one integer toward the next.
import math
class MathExample:
def __init__(self):
pass
def getFloorValue(self):
num1 = math.floor(1.0);
num2 = math.floor(1.1);
num3 = math.floor(1.2);
num4 = math.floor(1.3);
num5 = math.floor(1.4);
num6 = math.floor(1.5);
num7 = math.floor(1.6);
num8 = math.floor(1.7);
num9 = math.floor(1.8);
num10 = math.floor(1.9);
num11 = math.floor(2.0);
print("The floor value of 1.0 is " + str(num1));
print("The floor value of 1.1 is " + str(num2));
print("The floor value of 1.2 is " + str(num3));
print("The floor value of 1.3 is " + str(num4));
print("The floor value of 1.4 is " + str(num5));
print("The floor value of 1.5 is " + str(num6));
print("The floor value of 1.6 is " + str(num7));
print("The floor value of 1.7 is " + str(num8));
print("The floor value of 1.8 is " + str(num9));
print("The floor value of 1.9 is " + str(num10));
print("The floor value of 2.0 is " + str(num11));
obj = MathExample()
obj.getFloorValue()
The floor value of 1.0 is 1 The floor value of 1.1 is 1 The floor value of 1.2 is 1 The floor value of 1.3 is 1 The floor value of 1.4 is 1 The floor value of 1.5 is 1 The floor value of 1.6 is 1 The floor value of 1.7 is 1 The floor value of 1.8 is 1 The floor value of 1.9 is 1 The floor value of 2.0 is 2
A scheduling display groups positive measurements into whole-unit intervals. Compare readings just on either side of an integer to see which interval the display would select.
import math
class MathExample:
def __init__(self):
pass
def getFloorValue(self):
num1 = math.floor(1.5698);
num2 = math.floor(1.3093);
num3 = math.floor(1.0087);
num4 = math.floor(1.1038);
num5 = math.floor(1.9573);
num6 = math.floor(1.7462);
num7 = math.floor(1.6427);
num8 = math.floor(1.4128);
num9 = math.floor(1.8099);
num10 = math.floor(1.2743);
num11 = math.floor(2.0001);
print("The floor value of 1.5698 is " + str(num1));
print("The floor value of 1.3093 is " + str(num2));
print("The floor value of 1.0087 is " + str(num3));
print("The floor value of 1.1038 is " + str(num4));
print("The floor value of 1.9573 is " + str(num5));
print("The floor value of 1.7462 is " + str(num6));
print("The floor value of 1.6427 is " + str(num7));
print("The floor value of 1.4128 is " + str(num8));
print("The floor value of 1.8099 is " + str(num9));
print("The floor value of 1.2743 is " + str(num10));
print("The floor value of 2.0001 is " + str(num11));
obj = MathExample()
obj.getFloorValue()
The floor value of 1.5698 is 1 The floor value of 1.3093 is 1 The floor value of 1.0087 is 1 The floor value of 1.1038 is 1 The floor value of 1.9573 is 1 The floor value of 1.7462 is 1 The floor value of 1.6427 is 1 The floor value of 1.4128 is 1 The floor value of 1.8099 is 1 The floor value of 1.2743 is 1 The floor value of 2.0001 is 2
A temperature logger records values just below zero or another integer boundary. Follow the rounding direction on the negative side before using the result as the label of a chart interval.
import math
class MathExample:
def __init__(self):
pass
def getFloorValue(self):
num1 = math.floor(-1.5698);
num2 = math.floor(-1.3093);
num3 = math.floor(-1.0087);
num4 = math.floor(-1.1038);
num5 = math.floor(-1.9573);
num6 = math.floor(-1.7462);
num7 = math.floor(-1.6427);
num8 = math.floor(-1.4128);
num9 = math.floor(-1.8099);
num10 = math.floor(-1.2743);
num11 = math.floor(-2.0001);
print("The floor value of -1.5698 is " + str(num1));
print("The floor value of -1.3093 is " + str(num2));
print("The floor value of -1.0087 is " + str(num3));
print("The floor value of -1.1038 is " + str(num4));
print("The floor value of -1.9573 is " + str(num5));
print("The floor value of -1.7462 is " + str(num6));
print("The floor value of -1.6427 is " + str(num7));
print("The floor value of -1.4128 is " + str(num8));
print("The floor value of -1.8099 is " + str(num9));
print("The floor value of -1.2743 is " + str(num10));
print("The floor value of -2.0001 is " + str(num11));
obj = MathExample()
obj.getFloorValue()
The floor value of -1.5698 is -2 The floor value of -1.3093 is -2 The floor value of -1.0087 is -2 The floor value of -1.1038 is -2 The floor value of -1.9573 is -2 The floor value of -1.7462 is -2 The floor value of -1.6427 is -2 The floor value of -1.4128 is -2 The floor value of -1.8099 is -2 The floor value of -1.2743 is -2 The floor value of -2.0001 is -3
A sensor can use infinity to mark a reading outside its measuring range. Pass both signs through this operation so the display can handle the result or the reported error deliberately.
import math
class MathExample:
def __init__(self):
pass
def getFloorValue(self):
# Positive ∞ infinite value
n = math.floor(float("inf"))
print("The floor value of inf is " + str(n))
# Negative ∞ infinite value
x = math.floor(-float("inf"))
print("The floor value of -inf is " + str(x))
obj = MathExample()
obj.getFloorValue()
OverflowError: cannot convert float infinity to integer
The costume room has 8.5 metres of ribbon left before a performance. Each bag needs one unbroken 1.2-metre strap. The volunteer needs the number of bags she can finish from this roll.
import math
metres, per_strap = 8.5, 1.2
print(f"{math.floor(metres / per_strap)} straps")
7 straps
Seven straps use 8.4 metres. The last tenth of a metre cannot make another strap.
Reference: Python numeric operations