floor Algorithm

The floor algorithm, often referred to as the floor function, is a mathematical operation that takes a real number as input and returns the largest integer that is less than or equal to the given number. In other words, it rounds down the input number to the nearest whole number. The floor function is widely used in various fields of mathematics, computer science, and engineering, as it provides a simple way to perform truncation, rounding, and discretization operations. For example, the floor of 5.7 is 5, the floor of -2.3 is -3, and the floor of 4 is 4. The floor function is often denoted by the symbol "⌊x⌋" or "floor(x)". In programming languages, the floor function is commonly available as a built-in function, allowing developers to easily perform calculations that involve rounding down real numbers. Applications of the floor algorithm include solving problems that involve discrete steps, such as scheduling tasks, indexing arrays, or optimizing resource allocation.
def floor(x) -> int:
    """
    Return the floor of x as an Integral.

    :param x: the number
    :return: the largest integer <= x.

    >>> import math
    >>> all(floor(n) == math.floor(n) for n in (1, -1, 0, -0, 1.1, -1.1, 1.0, -1.0, 1_000_000_000))
    True
    """
    return (
        x if isinstance(x, int) or x - int(x) == 0 else int(x) if x > 0 else int(x - 1)
    )


if __name__ == "__main__":
    import doctest

    doctest.testmod()

LANGUAGE:

DARK MODE: