knakul853

Untitled

Jun 25th, 2020
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.52 KB | None | 0 0
  1. /**
  2. knakul853
  3. **/
  4. class Solution {
  5. public:
  6.     double myPow(double x, int n) {
  7.        
  8.         long p = (long)n;
  9.        
  10.         if( n < 0 )
  11.             return 1.0 / binPow(x, -p);
  12.         else
  13.             return binPow(x, p);
  14.        
  15.     }
  16.    
  17.     double binPow( double x, long p )
  18.     {
  19.         double res = 1.0;
  20.        
  21.         while( p )
  22.         {
  23.             if( p&1 )
  24.                 res = res * x;
  25.             p = p/2;
  26.             x = x * x;
  27.         }
  28.        
  29.         return res;
  30.     }
  31. };
Add Comment
Please, Sign In to add comment