簡體   English   中英

為什么我的程序只能打印到小數點后 6 位?

[英]Why is my program only printing to 6 decimal places?

我創建了一個程序來計算從零到無窮大的積分值,但是當我打印我的答案時,它只打印到小數點后 6 位,有人能告訴我這是為什么嗎?

首先十分感謝 :)

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

double integral_Function(double x){
    double value;
    value = 1/((1+ pow(x, 2))*(pow(x, 2/3)));
    return value;
}

double trapezium_Rule(double lower, double upper, int n){

    double h;
    double total;

    h = (upper - lower)/n;

    total = (h/2) * (integral_Function(upper) + integral_Function(lower));

    for(int i = 1; i < n; i++){
        total = total + (h * integral_Function(lower + (i * h)));
    }

    return total;

}

int main() {

    double sum = 0;
    double upper = DBL_MIN * 1.001;
    double lower = DBL_MIN;

    while (upper <= DBL_MAX){
        sum +=  trapezium_Rule(lower, upper, 5000) * 2;
        lower = upper;
        upper = upper * 1.02;
    }


    printf("%f", sum);

    return 0;
}

這是因為您使用字符串格式%f ,默認情況下僅在逗號后打印 6 位數字。 要增加它,只需指定所需的長度:

printf("%.9f", sum); //note the 9 - it is the length.

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM