小编cod*_*ght的帖子

大浮点和的精度

我试图总结一个正向递减浮点的排序数组.我已经看到,总结它们的最佳方法是开始将数字从最低到最高加起来.我写这个代码的例子是,但是,从最高数字开始的总和更精确.为什么?(当然,总和1/k ^ 2应为f = 1.644934066848226).

#include <stdio.h>
#include <math.h>

int main() {

    double sum = 0;
    int n;
    int e = 0;
    double r = 0;
    double f = 1.644934066848226;
    double x, y, c, b;
    double sum2 = 0;

    printf("introduce n\n");
    scanf("%d", &n);

    double terms[n];

    y = 1;

    while (e < n) {
        x = 1 / ((y) * (y));
        terms[e] = x;
        sum = sum + x;
        y++;
        e++;
    }

    y = y - 1;
    e = e - …
Run Code Online (Sandbox Code Playgroud)

c floating-point precision numerical-methods

6
推荐指数
1
解决办法
179
查看次数

标签 统计

c ×1

floating-point ×1

numerical-methods ×1

precision ×1