The problem is with negative values — when one of them is negative, holderNum
will always take on the negative value (being the min); holderDem
will become postive, so delta
equal to a negative less a positive equals a lesser negative. Then holderDem = abs(delta)
is a greater positive and keeps increasing. You should take the absolute value of both of them before entering the loop.
E.g.:
holderNum = -1
and holderDem = 6
Iteration 1:
delta = holderNum - holderDem = -1 - 6 = -7
holderNum = Math.min(holderNum, holderDem) = Math.min(-1, 6) = -1
holderDem = Math.abs(delta) = Math.abs(-7) = 7
Iteration 2:
delta = holderNum - holderDem = -1 - 7 = -8
holderNum = Math.min(holderNum, holderDem) = Math.min(-1, 7) = -1
holderDem = Math.abs(delta) = Math.abs(-7) = 8
etc., etc., etc.