def contd_fraction(x):
    """
    Method to determine the continued fraction expansion of an arbitrary real
    number implemented by using the continued fractions algorithm.
    It describes a real number x in terms of integers [a_0, ..., a_M] so that:
    x = a_0 + 1 / (a_1 + 1 / (a_2 + 1 / (... + 1 / a_M )))
    """
    # Threshold value needed because of the imprecision of floats so that
    # dividing would stop at most likely after the last integer a_M
    threshold = 10. ** (-5)
    # Split the number into its integer and fractional parts
    if x < 1:
        a = [0]
    else:
        a = [int(x)]
        x -= a[0]
    # Find all a values
    while x > threshold:
        # Invert the fractional part
        x = 1 / x
        # Split the inverted number
        a.append(int(x))
        x -= a[-1]
    return a