Saturday, November 17, 2012

A Slope Subroutine

Another subroutine; this time with exceptions.

    subroutine slope
        given
            Point 1st
            Point 2nd
        returns
            Number slope
        except
            when infinite slope
            when points too close to determine slope

    begin

        Boolean overflowed ← FALSE
        Number underflowed ← 0

        Number Δy ← 2nd'y - 1st'y
            when overflow
                overflowed ← TRUE
            when underflow
                increment underflowed

        Number Δx ← 2nd'x - 1st'x
            when overflow
                overflowed ← TRUE
            when underflow
                increment underflowed

        if underflowed = 2
            declare points too close to determine slope

        if overflowed
            Δy ← 2nd'y ÷ 2 - 1st'y ÷ 2
            Δx ← 2nd'x ÷ 2 - 1st'x ÷ 2

        slope ← Δy ÷ Δx
            when divide by zero
                declare infinite slope
            when overflow
                declare infinite slope

    return slope

No comments:

Post a Comment