相关疑难解决方法(0)

为什么浮点数中从大到小相加会引入更多误差?

不知道为什么,但如果我将大到小的 fp 数字相加,似乎增量误差会更大

#include <iostream>
#include <math.h>

int main() {
    std::cout.precision(50);

    const int numLoops = 1000;
    const long length = 10000;
    const double rate = 0.1;

    long totalLength = length * numLoops;
    long long steps = (long long)(totalLength / rate);

    double sum = 0.0;
    double sumRemainder = 0.0;
    for (long long step = 0; step < steps; step++) {
        if (sumRemainder >= length) {
            sumRemainder = fmod(sumRemainder, length);
        }

        sum += rate;
        sumRemainder += rate;
    }

    std::cout << …
Run Code Online (Sandbox Code Playgroud)

c++ floating-point rounding-error

-1
推荐指数
1
解决办法
1457
查看次数

标签 统计

c++ ×1

floating-point ×1

rounding-error ×1