C中返回大数的命令行参数

Rav*_*cor 2 c command-line-arguments

我正在编写一个基本程序来使用C命令行参数计算算术几何平均值.但是程序似乎没有识别我输入的任何内容.这是我的代码:

/*
*This program will calculate an arithmetic-geometric mean
*provided two numbers (x,y) and an epsilon (e) are entered.
*/

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

int main (int argc, char **argv) {

    //Check for command line argument.
    if (argc != 4) {
        printf ("Please enter two numbers (x,y) and an epsilon (e)\n");
        printf ("as command line arguments for an AGM calculation\n");
        exit(1);
    }

    double e,x,y,an,gn;

    x = atof (argv[1]); //First number x.
    y = atof (argv[2]); //Second number y.
    e = atof (argv[3]); //Number of repetitions e.

    double absoluteAnMinusGn; //Continuation condition.
    double a = (x + y) / 2; //Normal arithmetic mean.
    double g = sqrt (x * y); //Normal geometric mean.

    an = (a + g) / 2; //Iteration 1 for calculation arithmetic mean.
    gn = sqrt (a * g); //Iteration 1 for calculation geometric mean.
    absoluteAnMinusGn = an - gn; //Calculates continuation condition.
       if (absoluteAnMinusGn < 0) {
           absoluteAnMinusGn = absoluteAnMinusGn * (-1); //Ensures absolute value of continuation condition.
        }

    printf ("DEBUG IN: x%d, y%d, e%d, absoulteAnMinusGn%d, a%d, g%d, an%d, gn%d\n", x,y,e,absoluteAnMinusGn,a,g,an,gn);//DEBUG CODE 

    while (absoluteAnMinusGn > e) {
        an = (a + g) / 2;
        gn = sqrt (a * g);
        a = an;
        g = gn;
        absoluteAnMinusGn = an - gn;
        if (absoluteAnMinusGn < 0) {
            absoluteAnMinusGn = absoluteAnMinusGn * (-1);
        }
    }

    //printf ("The arithmetric-geometric mean is (%d,%d) for %d\n", a,g,e); 
            printf ("DEBUG OUT: x%d, y%d, e%d, absoulteAnMinusGn%d, a%d, g%d, an%d, gn%d\n", x,y,e,absoluteAnMinusGn,a,g,an,gn);//DEBUG CODE 

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

我在命令行中输入以下内容:agm.exe 3 4 5

我得到以下输出:

DEBUG IN: x0, y10742661112, e0, absoluteANMinusGn1074790400, a0, g1075052544, an-171951648, gn1057505593

DEBUG OUT: x0, y10742661112, e0, absoluteANMinusGn1074790400, a0, g1075052544, an-171951648, gn1057505593
Run Code Online (Sandbox Code Playgroud)

我昨天做了一个类似的程序,用于使用命令行输入计算积分,该命令行输入完全符合预期.代码在这里:

/*
*This program will calculate a Riemann sum using the 
*left hand rule for sin(x)/x.
*/

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

int main (int argc, char **argv) {

    //Check for command line argument.
    if (argc != 4) {
        printf ("Please enter integral bounds (a,b) and number of intervals (n)\n");
        printf ("as command line arguments for a Riemann sum calculation \n");
        exit(1);
    }

    double a,b,i,n,h,riemann,rectangles,answer;

    a = atof (argv[1]); //Lower bound of integral.
    b = atof (argv[2]); //Upper bound of integral.
    n = atof (argv[3]); //Number of intervals.

    h = (b - a) / n; //Delta X.
    i = 0; //Counts intervals.

    //Calculation of Left Hand Riemann Sum.
    while (i <= (n - 1)) {
        if (a == 0 && i == 0) { //Stops from dividing by zero.
            rectangles = 1;
            i++;
        }
        riemann = (sin(a + (i * h))) / (a + (i * h));
        rectangles += riemann;
        i++;
    }
    //Finalize answer.
    answer = rectangles * h;

    printf ("Sin(x)/x for bounds (%f , %f) with %f intervals is approximately %f \n", a,b,n,answer);

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

左手Riemann sum的上述程序正确输出,几乎与我对AGM的代码相同.有人可以帮我弄清楚这里出了什么问题吗?我到处搜索过,找不到解决办法.我知道AGM代码可能设置为输出错误答案,但我主要担心的是修复命令行参数识别.我可以稍后重做我的数学.

use*_*738 5

打印double的格式说明符是%f.如果你没有提供正确的格式说明符来处理double,并将double传递给%d格式说明符 - 它会导致未定义的行为.(%d期望积分参数不是double)(在你的情况下输出错误).

从§ 7.21.6.3p9 N1570(C11标)

如果任何参数不是相应转换规范的正确类型,则行为未定义.