c programme功課.......急

2006-10-30 9:10 am
如果我想計 x = (10x+2)^(1/3) 於c中可以點樣計?
因為我寫 x=pow(10x+2,(float)1/3)
計得唔o岩ge佢
出哩ge ans係:26815615873518931787257526688330301811796810000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.000000????
點解ge?
麻煩幫幫忙.....功課......急
唔該晒
更新1:

我用o左你ge 建議=~="都係error........

更新2:

#include #include main() { float x,y,i,a,b,r; i=0; printf("Find the root of the following equation: x^3-10x-2=0 \n"); printf("Please Enter a number x between 0 to 10: ");

更新3:

scanf("%f",&x); printf("Please Enter the Relative Error of x:"); scanf("%f",&y); a=pow(10*2+2,double(1.f/3.f)); b=fabs(a-x)/x;

更新4:

while (b y) { i=i+1; x=a; a=pow(10*x+2,(float) 1/3); b=fabs(a-x)/x;} r=a; printf("The Root of the eqaution is:"); printf("%f",&r); }

回答 (2)

2006-10-31 6:16 am
✔ 最佳答案
double x = pow ((double)(10*2 + 2) , (double)(1.0f / 3.0f));

剛剛試左, work先卑你

問題係:
pow() does not have an overloaded function for (int and double) as parameters. You must explicitly cast both parameters to double. You did it for the second parameter, double(1.0f/3.0f) but the first one remained to be an int.

explaination:
pow(int a, double b) Wrong
pow(double a, double b) YES

As to why you got a garbage answer to be the first place I cannot explain. The compiler should had caught the function overloaded error.
2006-10-30 10:27 am
x=pow(10x+2,(float)1/3)
改做:
x = pow( ( 10 * 2) + 2 ), double(1.f/3.f))
參考: my programming knowledge


收錄日期: 2021-04-25 13:02:56
原文連結 [永久失效]:
https://hk.answers.yahoo.com/question/index?qid=20061030000051KK00353

檢視 Wayback Machine 備份