[ create a new paste ] login | about

Link: http://codepad.org/03ZdGSRe    [ raw code | output | fork ]

C++, pasted on Dec 27:
float foo(float n)
{
    float f1=0.0, f2=n, fm=(f1+f2)/2.0,differ=1.0;
    while (differ > 0.0001)
    {
        if (fm*fm>n) {
            f2=fm;
            differ=fm*fm-n;
        }
        else {
            f1=fm;
            differ=n-fm*fm;
        }
        fm=(f1+f2)/2.0;
    }
    return fm;
}

int main()
{
    float x = 2.0;
    printf("%f", foo(x));
}


Output:
1
1.414246


Create a new paste based on this one


Comments: