------------------------------- MODULE Roots -------------------------------
EXTENDS DecimalMultiplier, DecimalExponentiation, DecimalDivider
half == DecimalRepresentation(5, 0, -1)
ITERS == 5
InitialEstimate(S) == LET power == ToInt(SubSeq(S, 2, 4)) - 499 IN
IF power < 0 THEN DecimalRepresentation(1, 0, 0)
ELSE LET NormalisedExp == BuildList(power \div 2 + 499)
builded == <<S[1]>> \o Zeros(3 - Len(NormalisedExp))
\o NormalisedExp \o SubSeq(S, 5, 22)
IN DivideR(2, 0, 0, builded)
CONSTANT num_before, num_after, num_exp
VARIABLE S, xn, DD, i
Init ==
/\ S = DecimalRepresentation(num_before, num_after, num_exp)
/\ xn = half
/\ DD = DecimalRepresentation(0, 0, 0)
/\ i = 0
Next ==
IF i >= ITERS THEN
UNCHANGED << S, xn, DD, i >>
ELSE IF i = 0 THEN
/\ xn' = InitialEstimate(S)
/\ i' = i + 1
/\ UNCHANGED << S, DD >>
ELSE
LET newDD == DivideB(S, xn)
additive == DecimalAdd(xn, DD)
new_xn == MultiplyDecimalRep(half, additive)
IN
/\ DD' = DivideB(S, xn)
/\ xn' = new_xn
/\ i' = i + 1
/\ UNCHANGED << S, DD >>
invariant == i < ITERS
=============================================================================