Python math.floor, import math ExamplesUse the math.floor function to take floors of numbers. Remove a number's fractional part.
dot net perls
Floor. With math.floor we reduce a number so that the fractional part is removed. Floor() will make all numbers smaller. Negative values will become more negative.
With floor, we get integers from floating-point numbers. Floor has no effect on integers. We must "import math" at the top of our programs.
Initial example. Here we use math.floor on some numbers. Note that the first number 100 is returned unchanged. Floor() does not modify integers.
Rounding Floor does not round upon the value 100.9. Instead it always reduces the number to be less.
Python program that uses math.floor
import math # Some numbers to take floors of. value0 = 100 value1 = 100.1 value2 = 100.5 value3 = 100.9 # Take floor of number. floor0 = math.floor(value0) print(value0, ":", floor0) # Take other floors. print(value1, ":", math.floor(value1)) print(value2, ":", math.floor(value2)) print(value3, ":", math.floor(value3))
100 : 100 100.1 : 100 100.5 : 100 100.9 : 100
NameError. Sometimes when writing Python programs I make this error. There is no "floor" in Python. We must use math.floor to use the floor function.
Python program that causes NameError
number = 78.6 # This will not work. result = floor(number)
Traceback (most recent call last): File "C:\programs\file.py", line 5, in <module> result = floor(number) NameError: name 'floor' is not defined
Negative floors. With negative numbers, math.floor has the same logical result as with positive ones. Numbers are always reduced. Negative numbers will become more negative.
Python program that uses math.floor on negative numbers
import math # Use math.floor on a negative number. result = math.floor(-1.1) print(result) result = math.floor(-1.9) print(result)
-2 -2
Benchmark, floor dictionary. I wanted to test whether a dictionary lookup could be faster than a math.floor call. I found math.floor is fast—much faster than calling get().
Version 1 This version of the code uses math.floor to get the floor of each number.
Version 2 Here we look up a value in a dictionary to get a cached floor value for a number.
Result Using a dictionary to memoize (cache) the result of math.floor is a big slow down. Just use math.floor directly.
Python program that benchmarks math.floor, dictionary
import time, math # Floor dictionary. floor_dict = {100.5: 100} print(time.time()) # Version 1: use math.floor. for i in range(0, 100000000): y = 100.5 z = math.floor(y) if z != 100: print(z) break print(time.time()) # Version 2: use dictionary lookup, get method. for i in range(0, 100000000): y = 100.5 z = floor_dict.get(y) if z != 100: print(z) break print(time.time())
1454633830.142 1454633830.727 math.floor = 0.59 s 1454633839.844 floor_dict.get = 9.12 s PyPy3 used
A review. In Python 3 we find many built-in functions like abs and round. With floor, though, we have to use the math module with an import statement.
© 2007-2021 sam allen. see site info on the changelog