Note: This site is currently "Under construction". I'm migrating to a new version of my site building software. Lots of things are in a state of disrepair as a result (for example, footnote links aren't working). It's all part of the process of building in public. Most things should still be readable though.

Python's floor, ceil, fabs, and trunc Functions

This is a scratchpad post look at the different python math functions

Code

import math

nums = [23, 37.1, 48.9, -7, -12.3, -26.9]

print("original  floor   ceil   fabs  trunc")

for num in nums:
    floored = math.floor(num)
    ceiled = math.ceil(num)
    fabsed = math.fabs(num)
    trunced = math.trunc(num)
    print(str(num).rjust(8, ' '), end=" ")
    print(str(floored).rjust(6, ' '), end=" ")
    print(str(ceiled).rjust(6, ' '), end=" ")
    print(str(fabsed).rjust(6, ' '), end=" ")
    print(str(trunced).rjust(6, ' '), end=" ")
    print()

Results

original  floor   ceil   fabs  trunc
      23     23     23   23.0     23 
    37.1     37     38   37.1     37 
    48.9     48     49   48.9     48 
      -7     -7     -7    7.0     -7 
   -12.3    -13    -12   12.3    -12 
   -26.9    -27    -26   26.9    -26

References