在c中使用pow时,结果与预期无关
432.0^543.0溢出。确保结果在浮动范围内。在c中使用pow时,结果与预期无关,c,C,432.0^543.0溢出。确保结果在浮动范围内。 我建议您使用double而不是float,double的范围更大。提示:432^543的结果是什么,float的范围是多少?这是否回答了您的问题?432^543->约1.168e+1431. 可能需要长的double.,但即使这样也可能不够。代码使用float,但调用了双精度函数,如sin、tan、pow UnfreeHeX,您使用的是什么C编译器?因为代码是进行双精度计算的,所以最好使用更精确的PI,如3.1415926535897932
我建议您使用double而不是float,double的范围更大。提示:432^543的结果是什么,float的范围是多少?这是否回答了您的问题?432^543->约1.168e+1431. 可能需要长的double.,但即使这样也可能不够。代码使用float,但调用了双精度函数,如sin、tan、pow UnfreeHeX,您使用的是什么C编译器?因为代码是进行双精度计算的,所以最好使用更精确的PI,如3.1415926535897932384626433832795. 在代码中有一个过于精确的常量是无害的。
#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");
}
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)