我正在這一段簡單的C代碼你怎麼能在C
#include "stdafx.h"
#include "math.h"
int main()
{
float i = 5.5;
float score = 0;
score=i/(i+(2^i));
}
使用浮點數作爲指數和編輯說,浮動我「必須是整數或無範圍的枚舉值」 ,而且我仍然是一個漂浮者是至關重要的。我如何使用float作爲c中的指數?
我正在這一段簡單的C代碼你怎麼能在C
#include "stdafx.h"
#include "math.h"
int main()
{
float i = 5.5;
float score = 0;
score=i/(i+(2^i));
}
使用浮點數作爲指數和編輯說,浮動我「必須是整數或無範圍的枚舉值」 ,而且我仍然是一個漂浮者是至關重要的。我如何使用float作爲c中的指數?
更改此:
score=i/(i+(2^i));
這樣:
score = i/(i + pow(2, i));
^
是XOR運算符,你需要pow(double base, double exponent);把一切融合在一起:
#include "math.h"
#include "stdio.h"
int main()
{
float i = 5.5;
float score = 0;
score = i/(i + pow(2, i));
printf("%f\n", score);
return 0;
}
輸出:
[email protected]:~$ gcc -Wall main.c -lm -o main
[email protected]:~$ ./main
0.108364
截至c99,如njuffa提到的,你可以使用exp2(float n):
計算2提升到給定的n次方。並且代替
:
pow(2, i)
使用:
exp2f(i)
在C表達
2^i
使用按位XOR運算符^
,這不是一個指數,因此建議i
必須是整數類型。
嘗試使用數學函數pow
如用
score = i/(i + pow(2,i));
''^是不是在C冪!在做出任何未假設的假設之前閱讀關於C的教程! – fuz