Check Machin-like formulas: Difference between revisions

Line 782:
0</lang>
'''Notes''': The function <tt>machin</tt> compares the results of each formula to π/4 (expressed as <tt>1r4p1</tt> in J's numeric notation). The first example above shows the results of these comparisons for each formula (with 1 for true and 0 for false). In J, arctan is expressed as <tt>_3 o. ''values''</tt> and the function <tt>x:</tt> coerces values to exact representation; thereafter J will maintain exactness throughout its calculations, as long as it can.
 
=={{header|Julia}}==
<lang>
using AbstractAlgebra // implements BigRat
 
tanplus(x,y) = (x + y) / (1 - x * y)
 
function taneval(coef, frac)
if coef == 0
return 0
elseif coef < 0
return -taneval(-coef, frac)
elseif isodd(coef)
return tanplus(frac, taneval(coef - 1, frac))
else
x = taneval(div(coef, 2), frac)
return tanplus(x, x)
end
end
 
taneval(tup::Tuple) = taneval(tup[1], tup[2])
 
tans(v::Vector{Tuple{BigInt, Rational{BigInt}}}) = foldl(tanplus, map(taneval, v), init=0)
 
const testmats = Dict{Vector{Tuple{BigInt, Rational{BigInt}}}, Bool}([
([(1, 1//2), (1, 1//3)], true), ([(2, 1//3), (1, 1//7)], true),
([(12, 1//18), (8, 1//57), (-5, 1//239)], true),
([(88, 1//172), (51, 1//239), (32, 1//682), (44, 1//5357), (68, 1//12943)], true),
([(88, 1//172), (51, 1//239), (32, 1//682), (44, 1//5357), (68, 1//12944)], false)])
 
function runtestmats()
println("Testing matrices:")
for (k, m) in testmats
ans = tans(k)
println((ans == 1) == m ? "Verified as $m: " : "Not Verified as $m: ", "tan $k = $ans")
end
end
 
runtestmats()
</lang> {{output}} <pre>
Testing matrices:
Verified as true: tan Tuple{BigInt,Rational{BigInt}}[(1, 1//2), (1, 1//3)] = 1//1
Verified as true: tan Tuple{BigInt,Rational{BigInt}}[(2, 1//3), (1, 1//7)] = 1//1
Verified as true: tan Tuple{BigInt,Rational{BigInt}}[(88, 1//172), (51, 1//239), (32, 1//682), (44, 1//5357), (68, 1//12943)] = 1//1
Verified as true: tan Tuple{BigInt,Rational{BigInt}}[(12, 1//18), (8, 1//57), (-5, 1//239)] = 1//1
Verified as false: tan Tuple{BigInt,Rational{BigInt}}[(88, 1//172), (51, 1//239), (32, 1//682), (44, 1//5357), (68, 1//12944)] = 1009288018000944050967896710431587186456256928584351786643498522649995492271475761189348270710224618853590682465929080006511691833816436374107451368838065354726517908250456341991684635768915704374493675498637876700129004484434187627909285979251682006538817341793224963346197503893270875008524149334251672855130857035205217929335932890740051319216343365800342290782260673215928499123722781078448297609548233999010983373327601187505623621602789012550584784738082074783523787011976757247516095289966708782862528690942242793667539020699840402353522108223//1009288837315638583415701528780402795721935641614456853534313491853293025565940011104051964874275710024625850092154664245109626053906509780125743180758231049920425664246286578958307532545458843067352531217230461290763258378749459637420702619029075083089762088232401888676895047947363883809724322868121990870409574061477638203859217672620508200713073485398199091153535700094640095900731630771349477187594074169815106104524371099618096164871416282464532355211521113449237814080332335526420331468258917484010722587072087349909684004660371264507984339711
</pre>
 
 
=={{header|Kotlin}}==
4,106

edits