import math


def _floor(x, n=10, m=None):
    """Approximates the floor function using gekko.

    Parameters
    ----------
    x : gekko variable or intermediate
        The value of which to take the floor.
    n : int
        The precision of the floor operation. The larger, the more precise.
    m : GEKKO
        A gekko model.

    Returns
    -------
    fx : gekko intermediate
        A gekko intermediate with an approximation of floor(x).
    """
    if m is None:
        raise Exception('Missing GEKKO model definition.')

    intermediates = [x - 0.5]
    pi = math.pi
    for k in range(1, n):
        intermediates.append((1 / pi) * m.sin(2 * pi * k * x) / k)

    return m.Intermediate(sum(intermediates))