C语言--[Error] invalid operands to binary ^ (have 'float' and 'float')--对指数运算和乘法运算的一些总结。
2019-03-07 08:52
4283 查看
[Error] invalid operands to binary ^ (have ‘float’ and ‘float’)
#include<stdio.h> int main(void) { int a = 0, b=0, c = 0; float x = 0, y =0; /*注意:如果y = a*x*x*x+b*x*x+c;再这里写,那么输出的y就是0; 因为C语言严格按照代码先后读取,在这里,x=0*/ printf("请依次输入三元一次方程的系数a、b、c;系数之间用空格键隔开:\n"); scanf("%d %d %d", &a, &b, &c); printf("请依次输入三元一次方程的自变量 x:\n"); scanf("%f", &x); y = a*x*x*x+b*x*x+c;/*int型 和 float型变量之间可以相乘。*/ printf("%f", y); return 0; }
由 [Error] invalid operands to binary ^ (have ‘float’ and ‘float’)
得到:
float类型不能使用 ^ 做乘法:
y = a*x^3+b*x^2+c;
但使用 * 相乘就可以:
y = x*x;
同样,我尝试 ^ 做乘法是对所有类型都不行,还是仅仅对浮点型不行;
故,如下,我尝试计算整型a的a^2,结果如下,仍然不可以。
int main(void) { int a = 0,c = 0; printf("输入a:\n") ; scanf("%d", &a); c = a^2; printf("%d",c); return 0; }
查询资料知道:C语言中的指数运算只能用循环;
如下:
PS:
下列代码参考作者:李晓nic;
来源:CSDN
原文:https://blog.csdn.net/weixin_42128813/article/details/81156819
int main(void) { long long a; int i,x,n; while(scanf("%d %d",&x,&n)!=EOF) { a=1; for(i=0;i<n;i++) { a=a*x; } printf("%lld\n",a); } return 0; }
相关文章推荐
- 6410_io_drv.c: error: invalid operands to binary << (have 'char *' and 'int') 错误处理
- ERROR:invalid operands to binary -
- error: invalid operands of types 'int()' and 'int' to binary 'operator%'
- iPhone实例学习之iphone sdk2.0 miss和invalid operands to binary问题
- Swift Compiler Error Binary oprator '+' cannot be applied to operands of type 'UInt16' and 'UInt8'
- Invalid operands to binary expression ('double' and 'double’)
- gcc编译出现 invalid operands to binary -的解决方法
- Swift中推送通知设置:Binary operator '|' cannot be applied to two UIUserNotificationType operands 错误解决
- 【iOS】swift-Binary operator '|' cannot be applied to two UIViewAutoresizing operands
- C语言总结之异或运算的一些特性及巧妙应用
- (位运算总结)A summary: how to use bit manipulation to solve problems easily and efficiently
- The connection to adb is down, and a severe error has occured."错误总结
- Python问题解决:OSError: raw write() returned invalid length 14 (should have been between 0 and 7)
- Error 1 Operator '+' cannot be applied to operands of type 'string' and 'method group'
- "The connection to adb is down, and a severe error has occured."错误总结
- sudo: Sorry, you must have a tty to run sudo Error on a Linux and Unix
- Cannot connect to WMI provider.You do not have permission or the server is unreachable.Note that you can only manager SQL Server 2005 and later version with SQL Server Configuration Manager.Invalid namespace [0x8004100e]
- C语言总结之异或运算的一些特性及巧妙应用
- C语言的一些总结(to be continued)
- error: invalid operands of types ‘double’ and ‘double’ to binary ‘operator^’