Chinaunix首页 | 论坛 | 博客
  • 博客访问: 262654
  • 博文数量: 170
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 1709
  • 用 户 组: 普通用户
  • 注册时间: 2014-05-06 18:01
文章分类

全部博文(170)

文章存档

2016年(11)

2015年(130)

2014年(29)

分类: Java

2015-03-22 17:01:18

public class Pow {


public static void main(String[] args) {
// TODO 自动生成的方法存根
System.out.print(pow(0.00001,2147483647));


}
public static double pow(double x, int n) {

//        int flag=n>0?1:0;
//        int c=Math.abs(n);
//        if(c==0)
//         return 1;
//        if(c==1)
//         return c;
//        if(flag>0)
//         if(c%2==0){
//         return pow(x, c/2)*pow(x, c/2);
//         }
//         else {
//         return pow(x, c/2)*pow(x, c/2)*x;
// }
//        else {
//         if(c%2==0){
//             return 1/pow(x, c/2)*pow(x, c/2);
//             }
//             else {
//             return 1/pow(x, c/2)*pow(x, c/2)*c;
//     }
// }
// consider the binary representation of n. For example, if it is "10001011", 


then x^n = x^(1+2+8+128) = x^1 * x^2 * x^8 * x^128. Thus, we don't want to loop n times to 


calculate x^n. To speed up, we loop through each bit, if the i-th bit is 1, then we add x^


(1 << i) to the result. Since (1 << i) is a power of 2, x^(1<<(i+1)) = square(x^(1<

The loop executes for a maximum of log(n) times.
if(n<0)  
if(n<0)  
       {  
           if(n==Integer.MIN_VALUE)  
               return 1.0 / (pow(x,Integer.MAX_VALUE)*x);  
           else  
               return 1.0 / pow(x,-n);  
       }  
       if(n==0)  
           return 1.0;  
       double ans = 1.0 ;  
       for(;n>0; x *= x, n>>=1)  
       {  
           if((n&1)>0)  
               ans *= x;  
       }  
       return ans;     
        }
       
    }
阅读(251) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~