c++ - 错误 : invalid operands of types 'float' and 'int' to binary 'operator^'

标签 c++ operands

<分区>

我收到错误 invalid operands of types 'float' and 'int' to binary 'operator^' 我不知道如何修复它

错误发生在函数f,在最后一行

非常感谢任何帮助

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>


using namespace std;
float f(float x);

int main()
{

    float a;
    float b;
    int n;
    float h;
    float x;
    float area;

    cout << "Please input the first limit: ";
    cin >> a;
    cout << "Please input the second limit: ";
    cin >> b;
    cout << "How many rectangles do you want to use? ";
    cin >> n;

    h = (b-a)/n;

    area = (f(a)+f(b))/2;

    for (int i=1;i<n;i++) {
        area += f(a+i*h);
    }

    area = area*h;
    cout << "The area under the curve of f(x) = (2/sqrt(3.14))(exp(-x^2)) is ";
    cout << area;
}

float f(float x){
     return (exp(-x^2))(2/sqrt(3.14));
}

最佳答案

x 的数据类型为 float。您已对其应用逻辑 XOR 运算符。 XOR 需要整数操作数。

不过,我怀疑您正在寻找指数。 C++ 没有指数运算符。相反,尝试这样的事情:

float f(float x){
     return (exp(-(x*x)))*(2/sqrt(3.14));
}

我假设您的意思是将 exp(-(x*x)) 乘以 (2/sqrt(3.14),但我没有看到乘法那里的接线员。

关于c++ - 错误 : invalid operands of types 'float' and 'int' to binary 'operator^' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36209026/

相关文章:

objective-c - '==' 的左操作数是垃圾值

linux - 在两个日期变量之间插入文本

c++ - Ogre3d/通过合成器监听器访问传递并传递标识符

c++ - 如何使简单的循环更通用

ios - Obj-c 表达式错误 - 二进制表达式 ('CGFloat' 的无效操作数(又名 'double’ ))

c - 强制编译器使用 Intrinsics 中的内存操作数

python - 一元操作数类型错误 + : 'str'

c++ - undefined object 引用错误

html - 适合显示静态 html 页面的 C++ 小部件

c++ - Keil uVision4 armcc : Using C++ standard includes <cstdint>