...
/Feature #7: Exponentiation for Mobile Devices
Feature #7: Exponentiation for Mobile Devices
Implement the "Exponentiation for Mobile Devices" feature for our "Language Compiler" project.
We'll cover the following...
Description
Mobile phones are widely used in the whole world. They also provide basic functionalities, like calculators, to ease everyday tasks. However, most mobile devices do not provide all of the functionalities of a calculator, because their processor does not support them. This is where our compiler comes in. First, we will discuss one of those unsupported functions named exponentiation, which computes any integer number raised to a specific power. Then, we will implement compiler functionality that does exponentiation in software.
Let’s see the following illustration to understand what a power function does.
Solution
Whenever our compiler encounters an expression involving exponentiation, it replaces the exponentiation part of the expression with a method call that calculates the result.
Here is how we will implement this feature:
-
We will call the
quick_pow()
method recursively. The base case is when the exponent is0
and the answer is1
. In cases other than the base case, we will change the base value to1 / base
and the negativepower
to positive. -
If the given value of the
power
is zero, then we will return1
. This ...