为什么我的程序只打印到小数点后6位? [英] Why is my program only printing to 6 decimal places?

查看:113
本文介绍了为什么我的程序只打印到小数点后6位?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了一个程序来计算从零到无穷大的整数值,但是当我打印答案时,它只会将其打印到小数点后6位,有人可以建议这是为什么吗?

Ive created a program to compute the value of an integral from zero to infinity, however when i go to print my answer it only prints it to 6 decimal places, could someone advise on why this is?

非常感谢:)

#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位数。要增加它,只需指定所需的长度即可。

This is because you use string format %f which is by default only print 6 digit after comma. To increase it, simply specify the length that you want:

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

这篇关于为什么我的程序只打印到小数点后6位?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆