SyntaxBomb - Indie Coders

Languages & Coding => Blitz Code Archives => Algorithms => Topic started by: BlitzBot on June 29, 2017, 12:28:42 AM

Title: [bmx] an interesting method of calculating the squareroot by Nate the Great [ 1+ years ago ]
Post by: BlitzBot on June 29, 2017, 12:28:42 AM
Title : an interesting method of calculating the squareroot
Author : Nate the Great
Posted : 1+ years ago

Description : This calculates the squareroot of any number to a given number of decimal places.  It uses strings to calculate more decimal places than ints or longs or doubles will allow.

Code :
Code: BlitzMax
1. Local sp:String = Input("Calculate the Squareroot of... ")
2.
3. Local dec:Int = Int(Input("Number of decimal places?"))
4.
5. Local pairs:Int[dec]
6. Local i:Int
7.
8. Local places:Int = Ceil((Instr(sp,".")-1)/2.0)
9. If Instr(sp,".") = 0 Then places = Ceil((Len(sp))/2.0)
10. Print places
11.
12. If Instr(sp\$,".") Then
13.         If (Instr(sp\$,".")/2.0) = Int(Instr(sp\$,".")/2.0) Then
14.                 sp = "0" + sp
15.         EndIf
16.         If ((Len(sp)-Instr(sp,"."))/2.0) <> Int((Len(sp)-Instr(sp,"."))/2.0) Then
17.                 sp = sp + "0"
18.         EndIf
19.         sp = Left(sp,Instr(sp\$,".")-1) + "" + Right(sp,Len(sp)-Instr(sp\$,"."))
20.         For i = 0 Until Len(sp)/2
21.                 pairs[i] = Int(Mid(sp,i*2+1,2))
22.         Next
23. Else
24.         If (Len(sp)/2.0) >< Int(Len(sp)/2.0)
25.                 sp\$ = "0" + sp\$
26.         EndIf
27.         For i = 0 Until Len(sp)/2
28.                 pairs[i] = Int(Mid(sp,i*2+1,2))
29.         Next
30. EndIf
31.
32. i = 0
33.
34. Local ANS\$
35. Local Remain\$
36. Local x\$
37.
38. Print "Decimals Calculated:"
39.
40. For i = 0 Until dec
41.
42.         remain = mul("100",remain)
44.         x = "0"
45.         Repeat
48.         x = sub(x,"1")
50.         ans = mul("10",ans)
52.         Print i
53. Next
55. Print Left(ans,places)+"."+Right(ans,Len(ans)-places)
56.
57.
58.
59. Function Mul:String(a\$,b\$)
60.         Local c\$
61.
62.         While a <> "0"
64.                 a = sub(a,"1")
65.         Wend
66.
67.         Return c
68. End Function
69.
71.         Local ans\$
72.         a = "0" + a
73.         b = "0" + b
74.         While Len(a) < Len(b)
75.                 a = "0" + a
76.         Wend
77.         While Len(b) < Len(a)
78.                 b = "0" + b
79.         Wend
80.
81.         Local dec:Int = Len(b)
82.         Local carry:Int
83.         For Local i:Int = dec To 1 Step -1
84.                 Local c:Int = 0
85.                 c = Int(Mid(a,i,1)) + Int(Mid(b,i,1)) + carry
86.                 carry = 0
87.                 While c > 9
88.                         c = c - 10
89.                         carry :+1
90.                 Wend
91.
92.                 ans = c + ans
93.         Next
94.
95.         While Left(ans,1) = "0" And ans.length > 1
96.                 ans = Right(ans,Len(ans)-1)
97.         Wend
98.         If ans = "-0" Then Return "0"
99.         Return ans
100. End Function
101.
102. Function sub\$(a\$,b\$)
103.         Local ans\$
104.         While Len(a) < Len(b)
105.                 a = "0" + a
106.         Wend
107.         While Len(b) < Len(a)
108.                 b = "0" + b
109.         Wend
110.
111.         Local dec:Int = Len(b)
112.
113.         Local exmp:Int
114.         For Local i:Int = 1 To dec
115.                 If Int(Mid(a,i,1)) > Int(Mid(b,i,1)) Then
116.                         exmp = True
117.                         Exit
118.                 ElseIf Int(Mid(a,i,1)) < Int(Mid(b,i,1)) Then
119.                         exmp = False
120.                         Exit
121.                 EndIf
122.         Next
123.         If exmp = False Then
124.                 Local tmp\$ = a
125.                 a = b
126.                 b = tmp
127.         EndIf
128.
129.         Local take:Int
130.         For i = dec To 1 Step -1
131.                 c = Int(Mid(a,i,1)) - Int(Mid(b,i,1)) - take
132.                 take = 0
133.                 While c < 0
134.                         c = c + 10
135.                         take :+ 1
136.                 Wend
137.
138.                 ans = c + ans
139.         Next
140.
141.         While Left(ans,1) = "0" And ans.length > 1
142.                 ans = Right(ans,Len(ans)-1)
143.         Wend
144.         If exmp = False Then ans = "-"+ans
145.         If ans = "-0" Then Return "0"
146.         Return ans
147. End Function

Warner(Posted 1+ years ago)

That's cool. Say, it answers without a decimal point?

Nate the Great(Posted 1+ years ago)

oh yeah it doesn't put a decimal in but that should be easy to figure out.

Nate the Great(Posted 1+ years ago)

okay, updated it so it puts a decimal point in. (doesnt work if its below 1 though.)