You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用C++求循环小数的平方根?遇sqrt(4/99)返回0该如何解决?

Hey there! Let's break down your two questions one by one—starting with the bug you ran into first, since that's a quick fix, then moving to how to handle repeating decimals in general.

2. Why sqrt(4/99) returns 0 (and how to fix it)

The issue here is integer division in C++. When you write 4/99, both operands are integers, so C++ truncates the result to the nearest integer—since 4 divided by 99 is less than 1, you get 0. Then sqrt(0) naturally returns 0.

To fix this, just make one of the numbers a floating-point value so the division happens in floating-point arithmetic:

#include <cmath>
#include <iostream>
#include <cstdio>

int main() {
    // Wrong: Integer division gives 0
    double wrong = sqrt(4/99);
    std::cout << "Wrong result: " << wrong << "\n";

    // Correct: Use floating-point division
    double correct = sqrt(4.0 / 99); // or 4 / 99.0, or (double)4 / 99
    std::cout << "Full precision result: " << correct << "\n";

    // Truncate to, say, 4 decimal places (since you don't need high precision)
    // Option 1: Use round() to avoid rounding errors
    double truncated = std::round(correct * 10000) / 10000;
    std::cout << "Truncated to 4 decimals (round method): " << truncated << "\n";

    // Option 2: Use printf for simpler formatting
    printf("Truncated to 4 decimals (printf): %.4f\n", correct);
    return 0;
}

Both options will give you the approximate value ~0.2020, which is what you expect.

1. Calculating the square root of repeating decimals in C++

Repeating decimals are just fractions in disguise—so the key is first converting the repeating decimal to a fraction, then computing the square root of that fraction. Here's how to do it:

Step 1: Convert the repeating decimal to a fraction

For pure repeating decimals (where the repeating starts right after the decimal point):

  • Let the repeating part be k (e.g., for 0.(04), k = 4)
  • Let the length of the repeating part be n (e.g., 2 digits for 0.(04))
  • The fraction is k / (10^n - 1) (so 4/(100-1) = 4/99 for 0.(04))

For mixed repeating decimals (e.g., 0.1(6) = 0.16666...), the formula is a bit different, but since your example is pure repeating, we'll focus on that case for simplicity.

Step 2: Compute the square root in C++

Once you have the fraction (numerator and denominator), compute the square root using floating-point division, then truncate as needed. Here's an example for 0.(04):

#include <cmath>
#include <iostream>

int main() {
    // Repeating decimal 0.(04) = 4/99
    int numerator = 4;
    int denominator = 99;

    // Calculate square root of the fraction
    double sqrt_val = sqrt(static_cast<double>(numerator) / denominator);

    // Truncate to 3 decimal places (floor truncates, round rounds to nearest)
    double truncated = std::floor(sqrt_val * 1000) / 1000;
    std::cout << "Truncated to 3 decimals: " << truncated << "\n";
    return 0;
}

If you don't know the fraction upfront, you'd need code to parse the repeating decimal string into a fraction—but since you mentioned precision isn't critical, manually converting the repeating decimal to a fraction is simpler and less error-prone.


内容的提问来源于stack exchange,提问作者Joel

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 06:50:42