PowerWithUnsignedExponent
来源:互联网 发布:淘宝买家账号注册 编辑:程序博客网 时间:2024/06/03 02:28
//考虑Exponent周全#include<stdio.h>bool invalidInput=false;bool equal(double num1,double num2){if(num1-num2>-0.0000001&&num1-num2<0.0000001)return true;else return false;}double PowerWithUnsignedExponent(double base,unsigned int exp){if(exp==0)return 1;if(exp==1)return base;double result=PowerWithUnsignedExponent(base,exp>>1);result*=result;if(exp&0x1==1)result=result*base;return result;}double Power(double base,int exponent){if(equal(base,0.0)&&exponent<0){invalidInput=true;return 0.0;}unsigned int exp=(unsigned int)exponent;if(exponent<0)exp=(unsigned int)(-exponent);double result=PowerWithUnsignedExponent(base,exp);if(exponent<0)result=1/result;return result;}void main(){double result=Power(2,3);if(!invalidInput) printf("%3.2f\n",result);else printf("输入无效的数字\n");result=Power(-2,3);if(!invalidInput) printf("%3.2f\n",result);else printf("输入无效的数字\n");result=Power(2,0);if(!invalidInput) printf("%3.2f\n",result);else printf("输入无效的数字\n");result=Power(0,0);if(!invalidInput) printf("%3.2f\n",result);else printf("输入无效的数字\n");result=Power(0,4);if(!invalidInput)printf("%3.2f\n",result);else printf("输入无效的数字\n");result=Power(0,-4);if(!invalidInput)printf("%3.2f\n",result);else printf("输入无效的数字\n");}
0 0
- PowerWithUnsignedExponent
- 小甲鱼PE详解之区块描述、对齐值以及RVA详解(PE详解06)
- java微信硬件设备授权案例
- leetcode Find the Celebrity
- POJ2311 Cutting Game SG函数
- 互联网七年老兵:实战讲解营销定位、挖词、文案、解析对手的重要性。
- PowerWithUnsignedExponent
- JAVA技术点学习-纯技术面试题
- Python练习100例-25
- java_opts 参数的说明
- Android Studio代码编辑界面 错误代码不提示问题
- 两个独立项目之间的页面访问
- history.pushState() 和 history.replaceState() 方法
- 多变的互联网营销思维:做不好SEO的原因竟是因为它
- 将字母和数字生成一个随机字符串