2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

ニュートン法による平方根計算

Last updated at Posted at 2012-12-26
# import <Foundation/Foundation.h>

double newton_sqrt(double n)
{
    if(n < 0.0)
    {
        return nan(NULL);
    }
    const double A = n;
    double a = A;
    for(int i = 0 ; i < 10 ; ++i)
    {
        double minus = (a * a - A) / (2.0 * a);
        a = a - minus;
        if(minus <= DBL_EPSILON)
        {
            break;
        }
    }
    return a;
}
int main(int argc, const char * argv[])
{
    for(int i = 0 ; i < 10000 ; ++i)
    {
        double a = sqrt(i);
        double b = newton_sqrt(i);
        double diff = ABS(a - b);
        printf("n = %i, result = %f, %f, diff = %f\n", i, a, b, diff);
    }
    return 0;
}

キチンと考え方を書きたい。そうしないとこんなのはただのマスターベーションで終わってしまう。
ちゃんと図をつくったら書きます。(とかなんとかいってほったらかしてすんません
しかしニュートン法とかいうかっこいい名前の割にコードを見ると拍子抜けするくらい短いものである

2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?