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
--
-- (assumes a>=b, which it always will be here,▼
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
elsif
-- return "a
▲ -- (assumes a>=b, which it always will be here,
end for▼
for i=length(a) to 1 by -1 do
return a▼
d = a[i]-b[i]-c
c = d<0
▲ 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
▲ a = repeat('0',-d)&a
▲ end if
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
c =
res &= x+'0'
p &= x+'0'
Line 546 ⟶ 541:
return res
end function
procedure spigot_test(string s, integer maxlen=50)
constant fmt = "
string
if
fnd = sprintf(" (first %d digits)",maxlen)
res = trim_tail(join_by(res,1,100,"","\n "))
end if
printf(1,fmt,{
end procedure
Line 563 ⟶ 559:
<small>(the final "2" was re-joined up by hand)</small>
<pre>
0.4472135954999579392818347337462552470881236719223051448541794490821041851275609
141.421356237309504880168872420969807856967187537694807317667973799073247846210703885038753432764157
1.41421356237309504880168872420969807856967187537694807317667973799073247846210703885038753432764157
2735013846230912297024924836055850737212644121497099935831413222665927505592755799950501152782060571
|