Square root by hand: Difference between revisions

m
→‎{{header|Phix}}: extended tests
(→‎{{header|Go}}: Adjusted to deal with non-integer cases.)
m (→‎{{header|Phix}}: extended tests)
Line 394:
 
=={{header|Phix}}==
<lang Phix>-- basedBased on https://en.wikipedia.org/wiki/Methods_of_computing_square_roots#Decimal_(base_10)<br>
...whereas this <i>is</i> a spigot algorithm<i>!</i>
The use of string inputs helps guarantee perfect accuracy.
<lang Phix>-- based on https://en.wikipedia.org/wiki/Methods_of_computing_square_roots#Decimal_(base_10)
<lang Phix>requires("0.8.2")
function bcd_sub(string a,b)
-- returns "a"-"b", coping with different lengths
Line 474 ⟶ 475:
return res
end function
 
?spigot_sqrt("152.2756")
procedure spigot_test(string s, integer maxlen=50)
?spigot_sqrt("15241.383936")
constant fmt = "First %d digits (at most) of the square root of %s:%s %s\n"
string r = spigot_sqrt("2"s,500 maxlen),
puts(1,join_by(r,1,100,""))</lang>
lf = iff(length(r)>10?"\n ":"")
if length(r)>100 then
puts(1, r = join_by(r,1,100,"","\n "))</lang>
end if
printf(1,fmt,{maxlen,s,lf,r})
end procedure
 
constant tests = {"152.2756","15241.383936",{"0.2",80},"10.89","625",
"0","0.0001","0.00000009",{"20000",99},{"2",500}}
papply(false,spigot_test,tests)</lang>
{{out}}
<small>(the final "2" was re-joined up by hand)</small>
<pre>
First 50 digits (at most) of the square root of 152.2756: 12.34
"12.34"
First 50 digits (at most) of the square root of 15241.383936: 123.456
"123.456"
First 80 digits (at most) of the square root of 0.2:
1.41421356237309504880168872420969807856967187537694807317667973799073247846210703885038753432764157
0.4472135954999579392818347337462552470881236719223051448541794490821041851275609
2735013846230912297024924836055850737212644121497099935831413222665927505592755799950501152782060571
First 50 digits (at most) of the square root of 10.89: 3.3
4701095599716059702745345968620147285174186408891986095523292304843087143214508397626036279952514079
First 50 digits (at most) of the square root of 625: 25
8968725339654633180882964062061525835239505474575028775996172983557522033753185701135437460340849884
First 50 digits (at most) of the square root of 0: 0
71603868999706990048150305440277903164542478230684929369186215805784631115966687130130156185689872372
First 50 digits (at most) of the square root of 0.0001: 0.01
First 50 digits (at most) of the square root of 0.00000009: 0.0003
First 99 digits (at most) of the square root of 20000:
141.421356237309504880168872420969807856967187537694807317667973799073247846210703885038753432764157
First 500 digits (at most) of the square root of 2:
1.41421356237309504880168872420969807856967187537694807317667973799073247846210703885038753432764157
2735013846230912297024924836055850737212644121497099935831413222665927505592755799950501152782060571
4701095599716059702745345968620147285174186408891986095523292304843087143214508397626036279952514079
8968725339654633180882964062061525835239505474575028775996172983557522033753185701135437460340849884
71603868999706990048150305440277903164542478230684929369186215805784631115966687130130156185689872372
</pre>
 
7,796

edits