import math


def get_val_interpolated(flow, r_float, c_float):
    """
    Returns flow value for float row, column position using bilinear interpolation.
    """
    rows = flow.shape[0]
    cols = flow.shape[1]

    if (r_float > rows - 1 or r_float < 0 or
            c_float > cols - 1 or c_float < 0):
        raise IndexError('Accessing flow of shape %s with float index (%0.3f, %0.3f)'
                         % (str(flow.shape), r_float, c_float))

    def get_neighbors(v_float, v_size):
        v_prev = int(math.floor(v_float))
        v_next = int(math.ceil(v_float))
        v_alpha = v_next - v_float
        if v_next > v_size - 1:
            v_next = v_prev
        return v_prev, v_next, v_alpha

    r_prev, r_next, r_alpha = get_neighbors(r_float, rows)
    c_prev, c_next, c_alpha = get_neighbors(c_float, cols)

    val_prev = flow[r_prev][c_prev] * c_alpha + flow[r_prev][c_next] * (1 - c_alpha)
    val_next = flow[r_next][c_prev] * c_alpha + flow[r_next][c_next] * (1 - c_alpha)

    return val_prev * r_alpha + val_next * (1 - r_alpha)