`

java-71-数值的整数次方.实现函数double Power(double base, int exponent),求base的exponent次方

 
阅读更多

public class Power {

	/**
	 *Q71-数值的整数次方
	 *实现函数double Power(double base, int exponent),求base的exponent次方。不需要考虑溢出。
	 */
	private static boolean InvalidInput=false;
	public static void main(String[] args) {
		double result=power(3,15);
		System.out.println(result);
	}

	public static double power(double base,int exponent){
		if(base==0.0){
			if(exponent==0){
				InvalidInput=true;
			}
			return 0.0;
		}
		double result=1;
		int unsignedExponent =exponent;
		if(exponent<0){
			unsignedExponent=-exponent;
		}
		result=powerWithUnsignedExponent(base,unsignedExponent);
		if(exponent<0){
			result=1/result;
		}
		return result;
	}
	/*
	 * in Java,integer is 32 bits
	 * we store 
	 * base^(2^0),base^(2^1),base^(2^2),...base^(2^31) or
	 * base^1,    base^2,    base^4,...    base^(2^31) 
	 * in 
	 * a[0],a[1],a[2],...a[31]
	 * obviously,a[i+1]=a[i]*a[i] (0<=i<=30)
	 * we calculate like this:
	 *  3^7=(3^1)*(3^2)*(3^4)=a[0]*a[1]*a[2]
	 */
	public static double powerWithUnsignedExponent(double base,int exponent){
		double result=1.0;
		int len=Integer.SIZE;//Integer.SIZE=32
		double[] a=new double[len];
		int count=numberOf1(exponent);
		double curPower=0.0;
		for(int i=0,j=0;i<len&&j<count;i++){
			if(i==0){
				curPower=base;
			}else{
				curPower=curPower*curPower;
			}
			if((exponent&(1<<i))!=0){//if exponent has '1' at i-th bit
				a[i]=curPower;
				count++;
			}
		}
		for(int i=0;i<len;i++){
			if(a[i]!=0){
				result*=a[i];
			}
		}
		return result;
	}
	/*
	 * a^n=a^(n/2)*a^(n/2)	(when a is even number)
	 * a^n=a^(n/2)*a^(n/2)*a (when a is odd number)
	 */
	public static double powerWithUnsignedExponent2(double base,int exponent){
		double result=1.0;
		if(exponent==0){
			return 1;
		}
		if(exponent==1){
			return base;
		}
		result=powerWithUnsignedExponent2(base,exponent/2)
				*powerWithUnsignedExponent2(base,exponent/2);
		if((exponent&(0x01))!=0){
			result*=base;
		}
		return result;
	}
	/*
	 * return how many '1' in x (binary)
	 * e.g. (15)D=(1111)B,then we get 4
	 */
	public static int numberOf1(int x){
		int count=0;
		while(x!=0){
			x&=x-1;
			count++;
		}
		return count;
	}
}

分享到:
评论

相关推荐

    剑指Offer(Python多种思路实现):数值的整数次方

    题:实现函数double Power(double base, int exponent),求base的exponent次方、不得使用库函数,同时不需要考虑大数问题。 解题思路一: # 一般方法 class SolutionOne: def __init__(self): self.gInvalidInput...

    剑指offer 题16——数值的整数次方

    题目: 实现函数double Power(double base,int exponent), 求base得exponent次方。不得使用库函数,同时不需要考虑大数问题 因为不需要考虑大数问题,所以看起来似乎很简单,只需要累加就好 double Power(double ...

    世界500强面试题.pdf

    1.4.2. 请修改 append 函数,利用这个函数实现............................................. 78 1.4.3. 有 n 个长为 m+1 的字符串 ................................................................ 82 1.4.4. n...

    最新 Expo Client - Exponent Android apk 安装包 2.14.0 2020.01.11

    在react native开发中使用的工具,由于谷歌play下载访问受限,特在此提供最新的expo Exponent 2.14.0 apk安装包,Expo SDK 版本 36.0.0, React Native 版本 0.61.4

    Matlab时频分析工具箱及函数应用说明

    很实用的时频分析工具箱,包括短时傅里叶正反变换等,很好,很强大哦~~分享给大家,包括以下函数的源代码: % sigmerge - Add two signals with given energy ratio in dB. % % Choice of the Instantaneous ...

    Exponent-2.21.3.tar.gz

    Expo Go模拟器的离线安装包 图文教程:https://blog.csdn.net/lxyoucan/article/details/119781682

    react native 开发工具, 最新Exponent-2.5.2.apk

    react native 开发工具, 最新Exponent-2.5.2.apk,Run your projects before you deploy. Open projects by scanning QR codes

    面试题16:数值的整数次方

    一、虽然计算能够显示正确结果,但是对无效输入(即基数为0且指数为... double g_Pow(double base, int exponent); private: double _g_Pow(double base, int exponent); }; double c_Pow::g_Pow(double base, int exp

    host.exp.exponent_2019-09-19.apk

    expo是用于调试exp SDK reactnative在手机上,安装以后,第一次打开的时候可能时间长,反正我的是这样的。多试几次就可以了

    算法面试题

    :实现函数double Power(double base,int exponent),求base的exponent次方。不得使用库 函数,同时不需要考虑大数问题。:请实现一个函数,输入一个整数,输出该数二进制表示中1的个数。:给定单向链表的头指针和一...

    trollyxia#CodingInterviews#012-数值的整数次方1

    保证base和exponent不同时为0思路要注意特殊情况:指数为0的情况,不管底数是多少都应该是1指数为负数的情况,求出的应该是其倒数幂的倒数指数为负数的情况

    Exponent-2.15.4.apk

    可以使用该客户端直接从手机端预览 app。 仅使用Android设备和计算机开始构建丰富的体验。 Expo是一个开发人员工具,用于使用JavaScript和React Native创建具有交互手势和图形的体验。

    混沌时间序列分析与预测工具箱 开源版本.zip

    感谢陆老师,这一版主要是实现了开源,非常好的学习资料。 混沌时间序列分析与预测工具箱 Version3.0 chaotic time series analysis and prediction matlab toolbox - trial version 3.0 (1)产生混沌时间序列...

    C语言如何计算三角函数有多少种方法计算复杂函数.docx

    double power_result = pow(base, exponent); 双曲函数: 双曲正弦(sinh): C double sinh_result = sinh(x); 双曲余弦(cosh): C double cosh_result = cosh(x); 双曲正切(tanh): C double tanh_result = tanh...

    混沌计算工具箱 matlab代码

    (7)求最大Lyapunov指数(largest Lyapunov exponent) 小数据量法 - \LargestLyapunov_Rosenstein\Main_LargestLyapunov_Rosenstein1.m \LargestLyapunov_Rosenstein\Main_LargestLyapunov_Rosenstein2.m \...

    compute-lyapunov-exponent.rar_Lyapunov_Lyapunov exponent_Lyapuno

    可以方便的计算混沌系统的李雅普诺诺夫指数,只要改变其动力方程即可。

    java中一个数的大数次方算法

    java中虽然有BigInteger(subStr).pow(exponent)可以对一个数进行幂计算,但是由于内存大小的限制,即使内存为2GB,对于幂数在100000以上的计算,结果会溢出,本例利用java数组解决了这个问题。

    host_exp_exponent_2.14.0_12_09_2019.apk

    expo2020年最新apk安装包,版本名称为 host_exp_exponent_2.14.0_12_09_2019.apk

    java噪声函数库

    LibNoise分形噪声函数库的JAVA翻译版,个人开发,仅供参考。 包中包含: 异常模块: noise.Exception noise.ExceptionInvalidParam 无效的参数异常。 noise.ExceptionNoModule 无模块异常,无法检索到该源模块 noise...

    New-folder-(4).rar_The Common

    random integer e is the encryption exponent. Let n = p*q. Using Euclid s greatest common divisor algorithm, one can compute d, the decryption exponent, such that: e*d = 1 (mod (p-1)*(q-1)) Both ...

Global site tag (gtag.js) - Google Analytics