Create Java ME Math.pow () method

Source: Internet
Author: User

When you use Java to develop a mobile device application, you may need to use mathematical methods that are not available for a particular Java VM. This article will specifically address the problem that Java ME does not have a "power" method Math.pow (). We will demonstrate the development of the same ME application using three different methods and choose the best programming solution from it.

To discuss this issue, we first examine the integer and fractional power parameters and confine our analysis to positive real numbers. We will demonstrate that it is relatively easy to find the solution set for both integer and decimal problems (without regard to the exponential notation). In most cases, we'll use the example problem n = 82/3, where we'll find a good estimate or a real solution for N. If the initial index is not available beforehand, the other solutions to this problem, including the Newton method and the Secant method, are not easy to program. Although the binary approach is a viable solution, we will focus on the three methods that are not traditionally explored. The first is a simple (but sometimes inefficient) geometric decay algorithm, while the second method uses the Math.sqrt () method and guarantees convergence to an approximate solution in no more than 11 iterations. The third method uses Taylor series approximation to find the logarithm and Euler-transform the Taylor series.

The ME Math.pow () method for generating integer solutions

Traditionally, the Java Math.pow () method contains two parameters. These two parameters include the base and exponent. We assume (initially) that both parameters are integers and then find the programmable solution of the Math.pow () method that uses the same parameters as the Java method in ME. Here, the programmable solution is fairly straightforward, as shown in Example 1. In this case, we only run the multiplication cycle with exponential values.

示例 1
int pow( int x, int y) /*we define the power method with
     base x and power y (i.e., x^y)*/
{
   int z = x;
   for( int i = 1; i < y; i++ )z *= x;
   return
}

Of course, one might find that the value of a non integer power needs to be obtained. A simple solution of a positive real number (without access to the Math.pow () method) may involve the use of Math.log (). For example, consider the case of 82/3. The result of using the natural logarithm of 2/3*ln (8) = 1.386294361. To get the final solution, you need to use index 1.386294361 (especially e1.386294361 = 4). In this case, you might not need to use a power function. Unfortunately, the Java ME does not support the Math.log () method either. When there is no Math.pow () or Math.log () method, we consider using the simple "brute force" heuristic method, applying the Math.sqrt () method and the Taylor series approximation of the natural logarithm (and Euler e) to obtain the solution of the Java ME problem.

ME Math.pow () using the geometric decay algorithm as a brute force solution

Early implementations of the Java ME include floating-point main data types float and double. Recently, these types have been added. Now we replace the integer argument in the Math.pow () declaration with the double data type.

You may need to use decimal indices in the Java ME Math.pow () Power method. The first method we provide for generating Math.pow () is a simple "brute force" exploratory approach using the geometric decay algorithm. In simple terms, the decay algorithm starts with a value greater than the known solution, and then applies a method to decay the value until the value is very close to the solution (see Example 2 for a demonstration of a simple linear decay algorithm). In our example, we will further demonstrate the geometric form that converges to the above solution.

示例 2
/* This example illustrates a simplistic decay algorithm that we will assume
converges to our desired solution (a positive integer) */
int n; // assume that n is the solution to the number we are trying to find
int varX = 1000; //assume that we know the solution is less than or equal to 1000
while( varX > 0 )
{
   varX -= 1; // decrement by 1
   if( varX == n)return varX;
}

In Example 2, we started descending from 1000 until we found the expected number, assuming the expected number was a positive integer. This type of algorithm forms the basis of a powerful exploratory approach.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.