dc, 53 bytes
Fd^sr[d*v]sA[ddlr+0=Asr]sR[dlAxlrlAx!<Rs_z0<M]dsMxlrp
Try it online!
We're going to use the register r
to hold our final result, so the first thing we need to do is put a too-big number in there so that our first comparison always succeeds. I think the largest number we can push in two bytes is 165 (which is less than the required 5526), but Fd^
gives us 15^15 which is plenty large. Macro A
, [d*v]sA
just does the square root of the square to give us absolute value.
Macro R
handles putting a value into register r
; we'll come back to it in a second. Macro M
, [dlAxlrlAx!<Rs_z0<M]dsMx
is our main macro. Duplicates the value on the stack, runs A
to get ABS, then pushes the value in r
and does the same. !<R
checks to see if ABS(r
) is greater than or equal to not less than ABS(top of stack), and if so it runs R
. Since we had to leave behind a copy of the original top of stack for R
to potentially use, we store to a useless register (s_
) to pop it. z0<M
just loops M
until the stack is empty.
Macro R
, [ddlr+0=Asr]sR
duplicates the top of stack twice (we need to leave something behind for M
to gobble up when we return). At this point we know that of the two absolute values, our new value is less than or equal to our old value. If they're equal, and one is positive and the other is negative, we always need the positive. Fortunately, knowing this we can easily check for the case of one being the inverse of the other by simply adding them and comparing to 0. If so, we run A
, and no matter what else happened we know we're storing whatever is left in r
.
After M
has gone through the whole stack, we l
oad r
and p
rint it.