#include<stdio.h>
#include<stdlib.h>
#define abs(a) ((a)>0 ? a: -a)
#define eps_sqrt 0.00000000000001
#define it 100
float sqrt(float x)
/*The Square Root Function using the Newton's Method*/
{
int it_sqrt=0;
float a_sqrt = x/2;
while ((abs((a_sqrt*a_sqrt)-(x))>=eps_sqrt) && (2.0*a_sqrt != 0) && (it_sqrt<=it))
{
a_sqrt = a_sqrt - ((a_sqrt*a_sqrt)-(x)/(2.0*a_sqrt));
it_sqrt++;
}
return a_sqrt;
}
int main()
{
printf("%.5f\n", sqrt(5));
system ("pause");
}
ニュートンの反復法を使用してPythonで平方根を見つけようとしましたが、完全にうまく機能しました。私はCを初めて使用しますが、この関数が機能しなかった理由がわかりません。私がそれを実行するときはいつでも、それは「-1。#INF0A」を返します。どんな助けでもありがたいです。
編集:私はepsをに変更しようとしましたが0.000001
、それも機能しませんでした。