C中计算器中的数字越大

时间:2014-12-18 10:44:55

标签: c calculator

我用C语言编写了一个计算器,除了当我尝试乘以99999999 * 99999999 [八个九个八个九]时,一切运行良好。它会自动将它们舍入到100 000 000.0,显然,我不希望这种情况发生。我使用float来表示数字和精度达到.1。我尝试将浮动更改为双倍,但无论如何它都无法工作。我听说过关于%g的一些信息,但我没有找到关于它的信息,我不知道如何使它工作。任何帮助都非常感谢!

修改 这是我的代码:

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

int main()
{
char op;
float numberone,numbertwo;
printf("Enter the operation: ");
scanf("%f%c%f\n", &numberone, &op, &numbertwo);
switch(op) {
    case '+':
        printf("%.1f + %.1f = %.1f",numberone, numbertwo, numberone+numbertwo);
        break;
    case '-':
        printf("%.1f - %.1f = %.1f",numberone, numbertwo, numberone-numbertwo);
        break;
    case '*':
        printf("%.1f * %.1f = %.1f",numberone, numbertwo, numberone*numbertwo);
        break;
    case '/':
        if(numbertwo==0){
            printf("You cannot divide by zero.");
        }
        else{
        printf("%.1f / %.1f = %.1f",numberone, numbertwo, numberone/numbertwo);}
        break;
    case'^':
        printf("%f ^ %f = %f", numberone, numbertwo, pow(numberone,numbertwo));
        break;

    default:
        printf("Error! operator is not correct");
        break;
}
return 0;
}

当我乘以99999999 * 99999999时,结果是:100 000 000.0 * 100 000 000.0 = 10 000 000 000 000 000.0。

5 个答案:

答案 0 :(得分:0)

    #include <stdio.h>

    int main(void)
    {
            float x, y;
            scanf("%f %f", &x, &y);
            printf("%f\n", x*y);
            return 0;
    }

    $ ./c
    99999999 99999999
    10000000272564224.000000

你的意思是价值不准确吗?这总是发生在浮点数上。 32位浮点数精确到最多约6位小数,而双精度数将是〜15位十进制数。

http://en.wikipedia.org/wiki/Floating_point#Accuracy_problems

答案 1 :(得分:0)

浮点数(floatdoublelong double)将数字存储为尾数和指数(例如,3x10 ^ 15,但有点不同,以二进制形式存储)。因此,如果存储过大的整数,则必然会丢失一些大于小数的精度(如果将其转换回来,则为不同的整数)。

如果你想处理非常大的数字,你可以使用像GMP这样的任意精度算术库。

答案 2 :(得分:0)

您遇到了所有标准类型固有的精度问题。它们以定义的位数表示。对于整数(char,short,int,long,long long),当溢出时,传递负数(或0表示无符号)。 char(8位)的ex:126 + 3给出...... -127

对于浮点数(float,double,long double),它可能更精细,因为它们表示为有效位数部分的位数和指数部分的其他位(请参阅{{3}的更多详细信息)实际上,单个浮点数可以表示大小为10 38 的数字,但精度不超过7或8个十进制数字。

如果您真的想要使用大数字进行精确计算,则应使用多精度技术或直接使用优秀的Wikipedia库。

答案 3 :(得分:0)

您的程序将使用这些值,只需使用double精度,scanf必须使用%lf格式说明符。如果你想要更大的价值,你将需要学习其他答案中提到的libgmp

答案 4 :(得分:0)

根据建议,如果需要处理大数字,可以使用libgmp。基于代码的简化示例:

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

int main(void){
    char op;

    mpz_t x;
    mpz_t y;
    mpz_t result;
    printf("Enter the operation: ");
    scanf("%c\n", &op);

    char x_str[256];
    char y_str[256];

    fgets(x_str, sizeof(x_str), stdin);
    fgets(y_str, sizeof(y_str), stdin);

    mpz_init(x);
    mpz_init(y);
    mpz_init(result);
    mpz_set_str(x, x_str, 10);
    mpz_set_str(y, y_str, 10);

    switch(op) {
        case '+':
            mpz_add(result, x, y);
            break;
        case '*':
            mpz_mul(result, x, y);
            break;
        default:
            printf("Error! operator is not correct");
            break;
    }
    gmp_printf("%Zd", x);
    printf(" %c ", op);
    gmp_printf("%Zd", y);
    gmp_printf(" = %Zd\n", result);

    mpz_clear(x);
    mpz_clear(y);
    mpz_clear(result);
    return EXIT_SUCCESS;
}

编译并链接到库:

/tmp$ gcc big.c -lgmp -o big 

然后运行:

/tmp$ ./big
Enter the operation: *
99999999
99999999
99999999 * 99999999 = 9999999800000001
相关问题