Continued fraction: Difference between revisions

Updated D entry
m (→‎Version 1: added note about omitted ß are assumed to be equal to unity; carefully clarified a cosmetic comment. -- ~~~~)
(Updated D entry)
Line 192:
=={{header|D}}==
<lang d>import std.typecons;
 
alias Tuple!(int,"a", int,"b") Pair;
 
FP calc(FP, F)(in F fun, in int n) pure nothrow {
FP temp = 0.0;
 
foreach_reverse (ni; 1 .. n+1) {
immutable a_bp = fun(ni);
temp = cast(FP)a_b[1]p.b / (cast(FP)a_b[0]p.a + temp);
}
return cast(FP)fun(0)[0].a + temp;
}
 
// int[2] fsqrt2(in int n) pure nothrow {
Tuple!(int,int)Pair fsqrt2(in int n) pure nothrow {
return tuplePair(n > 0 ? 2 : 1,
1);
}
 
Tuple!(int,int)Pair fnapier(in int n) pure nothrow {
return tuplePair(n > 0 ? n : 2,
n > 1 ? (n - 1) : 1);
}
 
Tuple!(int,int)Pair fpi(in int n) pure nothrow {
return tuplePair(n > 0 ? 6 : 3,
(2 * n - 1) ^^ 2);
}
 
void main() {
import std.stdio;
 
writefln("%.19f", calc!real(&fsqrt2, 200));
writefln("%.19f", calc!real(&fnapier, 200));