-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathD71_Implement Pow.cpp
More file actions
44 lines (38 loc) · 972 Bytes
/
D71_Implement Pow.cpp
File metadata and controls
44 lines (38 loc) · 972 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
// Implement the function power(b, e), which calculates b raised to the power of e (i.e. be).
// Examples:
// Input: b = 3.00000, e = 5
// Output: 243.00000
// Input: b = 0.55000, e = 3
// Output: 0.16638
// Input: b = -0.67000, e = -7
// Output: -16.49971
class Solution
{
public:
double power(double b, int e)
{
// Handle the case when exponent is 0
if (e == 0)
{
return 1.0;
}
// Handling the case of negative exponent
if (e < 0)
{
b = 1 / b;
e = -e; // Make exponent positive
}
// Perform exponentiation by squaring
double result = 1.0;
while (e > 0)
{
if (e % 2 == 1)
{ // If e is odd
result *= b;
}
b *= b; // Square the base
e /= 2; // Divide exponent by 2
}
return result;
}
};