小编Jai*_*hra的帖子

C - long double和printf问题

我是新的C程序员,我正在研究一个学校项目,我必须估算值或pi.我的教授表示我们必须使用声明所有整数项long double.控制台显示我询问用户在给定函数时接近pi的项数.我输入1作为术语数,但代码返回-0.00000000而不是4.00000000.

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

long double approx1(int terms)
{
    long double pi = 0;
    long double num = 4;
    long double denom = 1;
    int i;

    for(i=1; i <= terms; i++)
    {
        if(i%2 != 0)
        {
            pi=pi+(num/denom);
        }
        else
        {
            pi=pi-(num/denom);
        }
        denom = denom + 2;
    }
     printf("%.8Lf\n", pi);
}

int main()
{
    int terms;
    long double pie;

    printf("input number of terms, input 0 to cancel\n");
    scanf("%d", &terms);

    while(terms != 0)
    {
        if(terms > 0) …
Run Code Online (Sandbox Code Playgroud)

c printf long-double

2
推荐指数
1
解决办法
286
查看次数

标签 统计

c ×1

long-double ×1

printf ×1