minor code-shuffle on sqrt() fn
authorLuke Kenneth Casson Leighton <lkcl@lkcl.net>
Tue, 30 Apr 2019 01:58:21 +0000 (02:58 +0100)
committerLuke Kenneth Casson Leighton <lkcl@lkcl.net>
Tue, 30 Apr 2019 01:58:21 +0000 (02:58 +0100)
src/add/fsqrt.py

index c88aea3991f119b29a9415396292ddf64d626773..a87af61db4ded516df1f93d6cb3af933d6318af3 100644 (file)
@@ -23,30 +23,24 @@ def sqrtsimple(num):
 def sqrt(num):
     D = num # D is input (from num)
     Q = 0
 def sqrt(num):
     D = num # D is input (from num)
     Q = 0
-    R = 0
-    r = 0 # remainder
+    R = 0 # remainder
     for i in range(64, -1, -1): # negative ranges are weird...
 
     for i in range(64, -1, -1): # negative ranges are weird...
 
-        if (R>=0):
-
-            R = (R<<2)|((D>>(i+i))&3)
-            R = R-((Q<<2)|1) #/*-Q01*/
+        R = (R<<2)|((D>>(i+i))&3)
 
 
+        if R >= 0:
+            R -= ((Q<<2)|1) # -Q01
         else:
         else:
+            R += ((Q<<2)|3) # +Q11
 
 
-            R = (R<<2)|((D>>(i+i))&3)
-            R = R+((Q<<2)|3) #/*+Q11*/
-
-        if (R>=0):
-            Q = (Q<<1)|1 #/*new Q:*/
-        else:
-            Q = (Q<<1)|0 #/*new Q:*/
+        Q <<= 1
+        if R >= 0:
+            Q |= 1 # new Q
 
 
+    if R < 0:
+        R = R + ((Q<<1)|1)
 
 
-    if (R<0):
-        R = R+((Q<<1)|1)
-        r = R
-    return Q, r
+    return Q, R
 
 
 # grabbed these from unit_test_single (convenience, this is just experimenting)
 
 
 # grabbed these from unit_test_single (convenience, this is just experimenting)