Square root by hand: Difference between revisions

m
→‎{{header|Phix}}: merged bcd_sub/le, improved output
m (→‎{{header|Phix}}: merged bcd_sub/le, improved output)
Line 468:
The use of string inputs helps guarantee perfect accuracy.
<lang Phix>requires("0.8.2")
function bcd_subbcd(string a, b, op)
-- returns "a"-"b"first, copingtake care withof different lengths
-- (assumes a>=b, which it always will be here,
--- protected as it is by the bcd_le(b,a) call.)
integer c = 0, d = length(a)-length(b)
if d<0 then a = repeat('0',-d)&a
elsif d>0 then b = repeat('0', d)&b end if
forif iop=length(a)"le" to 1 by -1 dothen
dreturn a<= a[i]-b[i]-c
elsif c op="sub" d<0then
-- return "a[i]"-"b" =(as a d+c*10+'0'string)
-- (assumes a>=b, which it always will be here,
end for
a = trim_head(a,"0") --- (note:protected ""as equit is by a bcd(b,a,"0le") call.)
for i=length(a) to 1 by -1 do
return a
d = a[i]-b[i]-c
c = d<0
a[i] = repeat(d+c*10+'0',-d)&a
end for
a = trim_head(a,"0") -- (note: "" equ "0")
return a
end if
return 9/0 -- unknown op
end function
 
function bcd_xp20x(string p, integer x)
-- returns x*(p*20+x)
Line 501 ⟶ 507:
return p
end function
 
function bcd_le(string a,b)
-- returns a<=b numerically, taking care of different lengths
integer d = length(a)-length(b)
if d<0 then
a = repeat('0',-d)&a
elsif d>0 then
b = repeat('0',d)&b
end if
return a<=b
end function
 
function spigot_sqrt(string s, integer maxlen=50)
-- returns the square root of a positive string number to any precision
Line 533 ⟶ 528:
for x=9 to 0 by -1 do
string y = bcd_xp20x(p,x)
if bcd_lebcd(y,c,"le") then
c = bcd_subbcd(c,y,"sub")
res &= x+'0'
p &= x+'0'
Line 546 ⟶ 541:
return res
end function
 
procedure spigot_test(string s, integer maxlen=50)
constant fmt = "FirstSquare root%d digits (at most) of the square roots of %s:%s %s\n"
string rres = spigot_sqrt(s, maxlen), fnd = "", lf = ""
if lf = iff(length(rres)>10?"\n=maxlen ":"")then
fnd = sprintf(" (first %d digits)",maxlen)
if length(r)>100 then
rlf = join_by(r,1,100,"","\n ")
res = trim_tail(join_by(res,1,100,"","\n "))
end if
printf(1,fmt,{maxlenfnd,s,lf,rres})
end procedure
 
Line 563 ⟶ 559:
<small>(the final "2" was re-joined up by hand)</small>
<pre>
First 50 digits (at most) of the squareSquare root of 152.2756: 12.34
First 50 digits (at most) of the squareSquare root of 15241.383936: 123.456
FirstSquare root (first 80 digits (at most) of the square root of 0.2:
0.4472135954999579392818347337462552470881236719223051448541794490821041851275609
First 50 digits (at most) of the squareSquare root of 10.89: 3.3
First 50 digits (at most) of the squareSquare root of 625: 25
First 50 digits (at most) of the squareSquare root of 0: 0
First 50 digits (at most) of the squareSquare root of 0.0001: 0.01
First 50 digits (at most) of the squareSquare root of 0.00000009: 0.0003
FirstSquare root (first 99 digits (at most) of the square root of 20000:
141.421356237309504880168872420969807856967187537694807317667973799073247846210703885038753432764157
FirstSquare root (first 500 digits (at most) of the square root of 2:
1.41421356237309504880168872420969807856967187537694807317667973799073247846210703885038753432764157
2735013846230912297024924836055850737212644121497099935831413222665927505592755799950501152782060571
7,806

edits