尝试近似欧拉';C中的s数

尝试近似欧拉';C中的s数,c,function,pow,math.h,C,Function,Pow,Math.h,我试图使用公式(1+(1/n))^n来近似欧拉数。 编译器告诉我“在'double'之前有一个预期表达式” 代码如下: #include <stdio.h> #include <math.h> int main() { int x, y, power; int num = 1; int position = 1; while (position <= 100) { num = 1/num; n

我试图使用公式
(1+(1/n))^n
来近似欧拉数。 编译器告诉我“在'double'之前有一个预期表达式” 代码如下:

#include <stdio.h>
#include <math.h>

int main()
{
    int x, y, power;
    int num = 1;
    int position = 1;
    while (position <= 100)
    {
        num = 1/num;
        num = num + 1;
        x = num;
        power = double pow(x, x); //here
        printf("%f", power);
        position += 1;
        num = position;
    }
}
#包括
#包括
int main()
{
int x,y,幂;
int num=1;
int位置=1;

虽然(position我不是C大师,但我不只是将double本身称为类型声明,而不是类型转换?如果你是类型转换,它不是
power=(double)pow(x,x);
吗?请看:

我修改了你代码中的一些错误,认为它现在应该可以工作了;但是,我没有涉及的样式令人困惑

#include <stdio.h>
#include <math.h>

int main()
{
    double power;  //stores floating point numbers!
    double num = 1;//stores floating point numbers!
    int position = 1;
    while (position <= 100)
    {
        num = 1/num;
        num = num + 1;
        power = pow(num, position); //x not needed, this is what you ment
        printf("%f\n", power); //%d outputs decimal numbers, f is for floats
        position += 1;
        num = position;
    }
}
#包括
#包括
int main()
{
双幂;//存储浮点数!
double num=1;//存储浮点数!
int位置=1;

while(position如果您希望一个数字是双精度的(带小数的数字),则需要将其定义为双精度,而不是整数。我有这段代码可以解决您的问题。如果您使用UNIX,请确保编译
gcc FILEPATH-lm-o OUTPUTPATH

#include <stdio.h>
#include <math.h>

int main()
{
    double x, y, power, num = 1; //doubles allow for decimal places so declare it as double
    int position = 1; //Position seems to only be an integer, so declare it as an int.
    while (position <= 100)
    {
        num = 1/num;
        num++;
        x = num;
        power = pow(x, x);
        printf("%f", power);
        position += 1;
        num = position;
    }
}

我只是简单地纠正了您程序中的语法错误,但我不认为这会让您真正得到E。请参阅关于在C中计算E的内容。

我可以看出,您的代码存在多个问题。我不太清楚您的代码应该如何工作。“power”应该存储什么?y代表什么?好吧,首先您应该注意的是,您没有您不能用整数执行这样的计算。您是正确的,它不是类型强制转换,但也不是类型声明:它被称为语法错误,这并不能回答问题。此更改不足以修复代码;请注意,原始代码中的power是int类型。我想我只是在考虑解释器正在发疯。如果我有误导的话,很抱歉。据我所知,这段代码并没有计算出预期的公式。我认为仅仅打印它可能没有用,他可能想在以后的工作中实际使用它program@Asadefa谢谢,我修改了我的答案。
#include <stdio.h>
#include <math.h>

int main()
{
    double x, y, power, num = 1; //doubles allow for decimal places so declare it as double
    int position = 1; //Position seems to only be an integer, so declare it as an int.
    while (position <= 100)
    {
        num = 1/num;
        num++;
        x = num;
        power = pow(x, x);
        printf("%f", power);
        position += 1;
        num = position;
    }
}
#include <stdio.h>
#include <math.h>

int main()
{
    double x, y, power, num = 1;
    for (int i = 1; i <= 100; i++) {
        num = 1/num;
        num = num + 1;
        x = num;
        power = pow(x, x);
        printf("%f", power);
        position += 1;
        num = i;
    }
}
static const double E = 2.718281828459045;