我正在这一段简单的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