我有以下代码,我在这个等式中得到错误:
v=p*(1+r)^n.
Run Code Online (Sandbox Code Playgroud)
请帮我找出这个错误的原因.
# include <iostream>
# include <limits>
using namespace std;
int main()
{
float v,p,r;
int n;
cout<<"Enter value of p:";
cin>>p;
cout<<"Enter value of r:";
cin>>r;
cout<<"Enter value of n:";
cin>>n;
v=(p)*(1+r)^n; // here i am getting error message as "expression must have integral or enum type"
cout<<"V="<<v;
std::cin.ignore();
std::cin.get();
}
Run Code Online (Sandbox Code Playgroud)
C++ 11 5.12 - 按位异或运算符
exclusive-or-expression:and-expression exclusive-or-expression and-expression 1执行通常的算术转换; 结果是操作数的按位异或功能.运算符仅适用于整数或无范围的枚举操作数.
如果要计算v =(p)*(1 + r)n,则需要更改
v=(p)*(1+r)^n;
Run Code Online (Sandbox Code Playgroud)
至
v = p * powf(1+r, n); // powf: exponential math operator in C++
Run Code Online (Sandbox Code Playgroud)
In C++,^是XOR(独占或)运营商,例如a = 2 ^ 3; // a will be 1.
点击这里了解更多信息.
问题是它^不是C++中的指数数学运算符,而是一个按位xor运算.按位运算只能在积分/枚举值上完成.
如果要将浮点提升到特定功率,请使用该powf功能
powf(p * (1 + r), n)
// Or possibly the following depending on how you want the
// precedence to shake out
p * powf(1 + r, n)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
18823 次 |
| 最近记录: |