Adapted the C version of sqrt to python
authorAleksandar Kostovic <alexandar.kostovic@gmail.com>
Sun, 28 Apr 2019 09:52:36 +0000 (11:52 +0200)
committerAleksandar Kostovic <alexandar.kostovic@gmail.com>
Sun, 28 Apr 2019 09:52:36 +0000 (11:52 +0200)
src/add/fsqrt.py

index bec42185873bb8d25f1e93744f10d5fe6eba000d..3a2e999e8436291e69b4e2ee890c775df8a9954c 100644 (file)
@@ -1,5 +1,5 @@
 def sqrt(num):
-
+    """
     res = 0
     bit = 1 << 14
 
@@ -15,6 +15,38 @@ def sqrt(num):
         bit >>= 2
 
     return res
+    """
+    r = None
+    D = None
+    Q = 0
+    R = 0
+    for i in range(15):
+        i -= 1
+
+        if (R>=0):
+        
+            R = (R<<2)|((D>>(i+i))&3)
+            R = R-((Q<<2)|1) #/*-Q01*/
+         
+        else:
+
+            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:*/
+    
+
+    if (R<0):
+        R = R+((Q<<1)|1)
+        r = R
+    return Q
+
+
+for Q in range(1,20):
+    print(sqrt(Q))
 
 """
 //This is the main code of integer sqrt function found here:http://verilogcodes.blogspot.com/2017/11/a-verilog-function-for-finding-square-root.html