与在c中使用pow时的结果与预期无关

问题描述 投票:0回答:1
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <conio.h>
#include <math.h>
#include <string.h>
#define PI 3.141592654
load();
main()
{
    int i = 1;
    int Reuse;
    int Opt;
    float Num1, Num2 ,ans;
    char oper;
    while (i < 20){
    load();
    i++;
}
    printf("Loading done!!");
    Sleep(2000);
    system("cls");
    printf("Welcome to our calculator.\n");
    Reuse = 1;
    while (Reuse == 1){
        printf("\n\nWhich mode do you want to use?\n1.Normal maths operations\n2.Trigonometric functions\n3.Exit\nYour input: ");
        scanf("%d", &Opt);
        if (Opt == 1){
            printf("Your two numbers: ");
            scanf("%f%f", &Num1, &Num2);
            printf("Your operation symbol: ");
            scanf(" %c", &oper);
            if (oper == '+'){
                ans = (Num1 + Num2);
                printf("Here is your answer:\n%f  %c %f = %.5f (To 5 decimal places)\n\n", Num1, oper, Num2, ans);
                Sleep(2450);
                } else if (oper == '-'){
                ans = (Num1 - Num2);
                printf("Here is your answer:\n%f  %c %f = %.5f (to 5 decimal places)\n\n", Num1, oper, Num2, ans);
                Sleep(2450);
                } else if (oper == '/'){
                ans = (Num1 / Num2);
                printf("Here is your answer:\n%f  %c %f = %.5f (to 5 decimal places)\n\n", Num1, oper, Num2, ans);
                Sleep(2450);
                } else if (oper == '*'){
                ans = (Num1 * Num2);
                printf("Here is your answer:\n %f  %c %f = %.5f (to 5 decimal places)\n\n", Num1, oper, Num2, ans);
                Sleep(2450);
                } else if (oper == '^'){
                ans = (pow (Num1 , Num2));
                printf("Here is your answer:\n %f  %c %f = %.5f (to 5 decimal places)\n\n", Num1, oper, Num2, ans);
                Sleep(2450);
                } else{
                    printf("\n\nYour input operator is incorrect; ERROR 1 Sintek");
                    Sleep(2450);
                    system("cls");
                }
        }
            if (Opt == 2){
                printf("Input your angle in degrees: ");
                scanf("%f", &Num1);
                printf("The trigo you are going to use\ns for sine\nc for cosine\nt for tangent\nYour input: ");
                scanf(" %c", &oper);
                if (oper == 's'){
                ans = (sin (Num1 * PI/180));
                printf("\nHere is your answer:\nAngle: %f\nSin%f = %f", Num1, Num1, ans);
                Sleep(2450);
                } else if (oper == 'c'){
                ans = (cos (Num1 * PI/180));
                printf("\nHere is your answer:\nAngle: %f\nCos%f = %f", Num1, Num1, ans);
                Sleep(2450);
                } else if (oper == 't'){
                ans = (tan (Num1 * PI/180));
                printf("\nHere is your answer:\nAngle: %f\nTan%f = %f", Num1, Num1, ans);
                Sleep(2450);
                } else{
                    printf("\n\nWrong operator used for Trigo; ERROR 1 Sintek");
                }
            }
            if (Opt == 3){
                printf("Thank you for visiting my calculator, wish you come back soon!!");
                Sleep(3990);
                system("cls");
                exit(0);
            }

        }
}




load(){
    system("cls");
    printf("\\ LOADING /");
    Sleep(15);
    system("cls");
    printf("| LOADING -");
    Sleep(15);
    system("cls");
    printf("/ LOADING \\");
    Sleep(15);
    system("cls");
    printf("- LOADING |");
    Sleep(15);
    system("cls");
    printf("  LOADING  ");
    Sleep(3);
    system("cls");
}

以上是我的代码。运行幂函数时,在while循环内部的if-else循环之一中的pow()不输出任何数值,甚至不输出错误的数字,而是输出短语1.#Info。除此之外,代码可以正常且平稳地运行。如果你们有什么改进可以使我的代码出色,请也声明。

Which mode do you want to use?
1.Normal maths operations
2.Trigonometric functions
3.Exit
Your input: 1
Your two numbers: 432 543
Your operation symbol: ^
Here is your answer:
 432.000000  ^ 543.000000 = 1.#INF0 (to 5 decimal places)
c
1个回答
0
投票

432.0 ^ 543.0溢出。确保结果在浮动范围内。我建议您使用double而不是float,double的范围更大。

© www.soinside.com 2019 - 2024. All rights reserved.