Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

# 69. Sqrt(x) #287

Open
Woodyiiiiiii opened this issue Aug 29, 2023 · 0 comments
Open

# 69. Sqrt(x) #287

Woodyiiiiiii opened this issue Aug 29, 2023 · 0 comments

Comments

@Woodyiiiiiii
Copy link
Owner

Woodyiiiiiii commented Aug 29, 2023

69. Sqrt(x)

69. Sqrt(x)

如何求平方根?二分,但如果带精度呢?用左右边界去逼近。

某du外包题。唉,我没考虑到精度,感觉还是不善于思考。

public double mySqrt(double val, double e) {
        double l = 0, r = val;
        while (r - l > e) {
            double m = (l + r) / 2.0;
            if (m * m < val) {
                l = m;
            } else if (m * m > val) {
                r = m;
            } else {
                return m;
            }
        }
        return (l + r) / 2.0;
    }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant