Error: invalid operands of types 'float' and 'int' to binary 'operator^'
Error: invalid operands of types 'float' and 'int' to binary 'operator^'
我收到错误 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)
,但我在那里没有看到乘法运算符。
我收到错误 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)
,但我在那里没有看到乘法运算符。