...

/

Feature #7: Exponentiation for Mobile Devices

Feature #7: Exponentiation for Mobile Devices

Implement the "Exponentiation for Mobile Devices" feature for our "Language Compiler" project.

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:

  1. We will call the quickPow() method recursively. The base case is when the exponent is 0 and the answer is 1. In cases other than the base case, we will change the base value to 1 / base and the negative power to positive.

  2. If the given value of the power is zero, then we will return 1. This ...

Access this course and 1400+ top-rated courses and projects.