题练(四):求一个数字的平方根

//输出一个数字的平方根
double SqrtOfNum(double number)
{
    double epsilon = 0.00000001, start = 0, end = 0, middle = 0;
    if (number < 1)
    {
        start = number;
        end = 1;
    }
    else
    {
        start = 1;
        end = number;
    }

    while (end - start >= epsilon)
    {
        middle = start + (end - start) / 2;
        if (middle * middle == number)
            return middle;
        else if (middle * middle < number)
        {
            start = middle;
        }
        else
        {
            end = middle;
        }
    }

    return start;
}

你可能感兴趣的:(题练(四):求一个数字的平方根)