使用 Java 開發移動設備應用程序時
要討論此問題
產生整數解的 ME Math
傳統上
示例
int pow( int x
base x and power y (i
{
int z = x;
for( int i =
return
}
當然
使用幾何衰變算法作為強力解的 ME Math
Java ME 的早期實現包括浮點主數據類型 float 和 double
可能需要在 Java ME Math
示例
/* 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 =
while( varX >
{
varX
if( varX == n)return varX;
}
在示例
使用類似的方法
示例
double pow( double x
{
int den =
int num = (int)(y*den); // find numerator
int s = (num/den)+
/***********************************************************************
** Variable
** our starting search value
** n =
** generated in our next section of code
** problem (given that the base is positive) will always be less than or
** equal to the base times the numerator power
************************************************************************/
/***********************************************************************
** Because we set the denominator to an arbitrary high value
** we must attempt to reduce the fraction
** we find the highest allowable fraction that we can use without
** exceeding the limitation of our primitive data types
************************************************************************/
double z = Double
while( z >= Double
{
den
num = (int)(y*den); // find numerator
s = (num/den)+
// find value of our base number to the power of numerator
z = x;
for( int i =
}
/***********************************************************************
** Now we are going to implement the decay algorithm to find
** the value of
************************************************************************/
/***********************************************************************
** We now find
** finding the value of
** value
** equal to
************************************************************************/
double n = x; // We define
// find
for( int i =
// Begin decay loop
while( n >
{
double a = n; //proxy for n
// find
for( int i =
// compare
// if so
double check
double check
if( check
n *=
}
// value could not be found
return
}
本示例演示了衰變算法的使用方法
這裡講述的解只處理正指數
處理負指數
要再增加一層復雜度
示例
if( y <
{
x = (
y *=
}
現在
From:http://tw.wingwit.com/Article/program/Java/hx/201311/26818.html