计算数的平方
在vc的mfc里,要计算一个数的平方该怎么表达?比如b=a^2,在编译时为什么会出现
error C2296: '^' : illegal, left operand has type 'float'这个错误!我的a定义为float型,b定义为float型,这是为何?我该怎么解决这个问题???
还请高手指点迷津!
[解决办法]
基本语法中没有^操作符,可以用<math.h>下的pow函数实现
给个例子
#include <math.h> #include <stdio.h> void main( void ) { double x = 2.0, y = 3.0, z; z = pow( x, y ); printf( "%.1f to the power of %.1f is %.1f\n", x, y, z ); }
[解决办法]
^是异或操作符,计算一个数的平方要用库函数pow()
[解决办法]
或者自己写一个:
template <typename _Ty>
_Ty X2X2( _Ty thing )
{
return thing * thing;
}
[解决办法]
楼主受BASIC影响了。