抽象と第一級手続き
-----
const
double fixedPointOfTransform
(const
function<double(double)>& g,
const
function<function<double(double)>(function<double(double)>)>&
transform,
const double guess)
{
return(fixedPoint(transform(g), guess));
}
const
double sqrtFixed2(const double x)
{
return(fixedPointOfTransform([x](const
double y){return(x/y);},
averageDamp,1.0));
}
const
double sqrtNewton2(const double x){
return(fixedPointOfTransform
([x](const double y){return(square(y)-x);},
NewtonTransform,1.0));
}
int
main(int argc, char** argv)
{
cout<<setprecision(16)<<"sqrt(2) with fixed
point="
<<sqrtFixed2(2)<<endl;
cout<<setprecision(16)<<"sqrt(2) with Newton's method="
<<sqrtNewton2(2.0)<<endl;
return(0);
}
----
出力
----
sqrt(2)
with fixed point=1.41421356237469
sqrt(2)
with Newton's method=1.41421356245306
0 件のコメント :
コメントを投稿