自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Java ME中的Math.pow()方法使用詳解

開發(fā) 后端
本文向您介紹Java ME中的Math.pow()方法,包括使用Math.pow()產(chǎn)生整數(shù)解、使用幾何衰變算法作為強(qiáng)力解的ME Math.pow()等。

使用 Java 開發(fā)移動設(shè)備應(yīng)用程序時,可能需要用到特定 Java VM 所沒有的數(shù)學(xué)方法。本文將專門解決 Java ME 沒有“冪”方法 Math.pow() 的問題。我們將演示使用三種不同的方法開發(fā)同一個 ME 應(yīng)用程序,并從中選出***的編程解決方案。

要討論此問題,我們先考察整數(shù)和分?jǐn)?shù)冪參數(shù),將我們的分析限于正實數(shù)。我們將演示求整數(shù)問題和小數(shù)問題的解集相對而言比較容易(而不考慮指數(shù)的符號)。在大多數(shù)情況下,我們將使用示例問題 n = 82/3,其中我們會求出 n 的良好估計或?qū)嶋H解。如果初始指數(shù)事先不可用,則此問題的其他解(包括牛頓法和割線法)不易編程。雖然二分法是可行的解決方案,但我們將關(guān)注傳統(tǒng)上不為人所探究的三個方法。***個是簡單的(不過有時效率低下)幾何衰變算法;而第二個方法將利用 Math.sqrt() 方法并保證在不超過 11 次迭代中收斂到一個近似解。第三個方法將使用泰勒級數(shù)逼近法求對數(shù)并對泰勒級數(shù)進(jìn)行歐拉轉(zhuǎn)換。

產(chǎn)生整數(shù)解的 ME Math.pow() 方法

傳統(tǒng)上,Java Math.pow() 方法包含兩個參數(shù)。這兩個參數(shù)包括底數(shù)和指數(shù)。我們假定(最初)這兩個參數(shù)均為整數(shù),然后求出 ME 中與 Java 方法使用相同參數(shù)的 Math.pow() 方法的可編程解。此處,可編程解相當(dāng)簡單,如示例 1 所示。在本例中,我們僅運(yùn)行以指數(shù)值為指標(biāo)的倍乘循環(huán)。

示例 1

  1. int pow( int x, int y) /*we define the power method with  
  2.     base x and power y (i.e., x^y)*/ 
  3. {  
  4.   int z = x;  
  5.   forint i = 1; i < y; i++ )z *= x;  
  6.   return 
  7. }  

當(dāng)然,有人可能會發(fā)現(xiàn)需要求出非整數(shù)冪的值。正實數(shù)的簡單解(無需訪問 Math.pow() 方法)可能涉及使用 Math.log()。例如,請考慮 82/3 的情況。利用 2/3*ln(8) = 1.386294361 中自然對數(shù)的結(jié)果。要得到最終解,需要利用指數(shù) 1.386294361(特別指出 e1.386294361 = 4)。在這種情況下,可能不需要使用冪函數(shù)。遺憾的是,Java ME 也不支持 Math.log() 方法。沒有 Math.pow() 或 Math.log() 方法時,我們會考慮使用樸素的“強(qiáng)力”試探性方法,應(yīng)用 Math.sqrt() 方法以及自然對數(shù)(和歐拉 e)的泰勒級數(shù)逼近來求得 Java ME 問題的解。

使用幾何衰變算法作為強(qiáng)力解的 ME Math.pow()

Java ME 的早期實現(xiàn)包括浮點(diǎn)主數(shù)據(jù)類型 float 和 double。最近,已添加了這些類型?,F(xiàn)在我們將 Math.pow() 聲明中的整型參數(shù)替換為 double 數(shù)據(jù)類型。

可能需要在 Java ME Math.pow() 冪方法中使用小數(shù)指數(shù)。我們提供的生成 Math.pow() 的***種方法是使用幾何衰變算法的樸素的“強(qiáng)力”試探性方法。簡單而言,衰變算法以一個大于已知解的值開始,然后應(yīng)用某個方法來衰變該值,直到該值非常逼近該解(有關(guān)簡單線性衰變算法的演示,請參見示例 2)。在我們的例子中,將進(jìn)一步演示向上述解收斂的幾何形式。

示例 2

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

在示例 2 中,我們從 1000 開始遞減,直到找到預(yù)期的數(shù)字,假定預(yù)期數(shù)字是一個正整數(shù)。這種類型的算法構(gòu)成了強(qiáng)力試探性方法的基礎(chǔ)。

使用類似的方法,我們可在遇到小數(shù)時應(yīng)用此算法。假定我們需要求出 n 的值,其中 n = 82/3。要使用衰變算法,我們必須首先找到一個合適的起點(diǎn),該點(diǎn)要等于或大于解本身。這對于帶有正指數(shù)的正實數(shù)很容易做到。對于我們的示例,要對此解進(jìn)行編程,對方法兩邊求立方,得到 n3=82 。當(dāng)然,此方程與 n3=64 等效。之后,我們的起始值將變?yōu)?64,我們知道 n 必須小于 64(因為 n3 = 64)。注意,如果限于正實數(shù),則此推導(dǎo)方法同樣適用于任何正指數(shù)值?,F(xiàn)在,我們可能需要設(shè)計一個循環(huán)來產(chǎn)生 n 的“充分接近”預(yù)期數(shù)字的解。我們再來看示例 3,它適合于所有正底數(shù)和正指數(shù)。

示例 3

  1. double pow( double x, double y ) //we define our new power method for fractions  
  2. {  
  3.   int den = 1000// specify arbitrary denominator  
  4.   int num = (int)(y*den); // find numerator  
  5.   int s = (num/den)+1;  
  6.   /***********************************************************************  
  7.   ** Variable 's' provides the power for which we multiply the base to find  
  8.   ** our starting search value. For example, if we seek a solution for  
  9.   ** n = 8^(2/3), then we will use 8^2 or 64 as our starting value (which is  
  10.   ** generated in our next section of code.) Why? The solution for our  
  11.   ** problem (given that the base is positive) will always be less than or  
  12.   ** equal to the base times the numerator power.  
  13.   ************************************************************************/ 
  14.   /***********************************************************************  
  15.   ** Because we set the denominator to an arbitrary high value,  
  16.   ** we must attempt to reduce the fraction. In the example below,  
  17.   ** we find the highest allowable fraction that we can use without  
  18.   ** exceeding the limitation of our primitive data types.  
  19.   ************************************************************************/ 
  20.   double z = Double.MAX_VALUE;  
  21.   while( z >= Double.MAX_VALUE )  
  22.   {  
  23.     den -=1// decrement denominator  
  24.     num = (int)(y*den); // find numerator  
  25.     s = (num/den)+1// adjust starting value  
  26.     // find value of our base number to the power of numerator  
  27.     z = x;  
  28.     forint i = 1; i < num; i++ )z *= x;  
  29.   }  
  30.   /***********************************************************************  
  31.   ** Now we are going to implement the decay algorithm to find  
  32.   ** the value of 'n'.  
  33.   ************************************************************************/ 
  34.   /***********************************************************************  
  35.   ** We now find 'n' to the power of 's'. We will then decrement 'n',  
  36.   ** finding the value of 'n' to the power of the denominator. This  
  37.   ** value, variable 'a', will be compared to 'z'. If the 'a' is nearly  
  38.   ** equal to 'z', then we will return 'n', our desired result.  
  39.   ************************************************************************/ 
  40.   double n = x; // We define 'n' as our return value (estimate) for 'x'.  
  41.   // find 'n' to the power of 's'.  
  42.   forint i = 1; i < s; i++)n *= x;  
  43.   // Begin decay loop  
  44.   while( n > 0 )  
  45.   {  
  46.     double a = n; //proxy for n  
  47.     // find 'a', the value of 'n' to the power of denominator  
  48.     forint i = 1; i < den; i++ )a *= n;  
  49.     // compare 'a' to 'z'. Is the value within the hundred-thousandth?  
  50.     // if so, return 'n'.  
  51.     double check1 = a-z;  
  52.     double check2 = z-a;  
  53.     if( check1 < .00001|| check2 > .00001 ) return n;  
  54.     n *= .999;// We arbitrarily use a decay of .1% per iteration  
  55.   }  
  56.   // value could not be found, return -1.  
  57.   return -1.0;  

本示例演示了衰變算法的使用方法。您會注意到,n 的值(解的估計值)將按 1% 強(qiáng)制遞減。您可能需要根據(jù)編程精度要求來改變此值。也可能考慮包括編程邏輯,該邏輯用于將前一迭代解與當(dāng)前迭代進(jìn)行比較,然后,如果有改善繼續(xù)進(jìn)行迭代,但是,如果解已回歸,則返回前一個值。

這里講述的解只處理正指數(shù)。如果值為負(fù)會出現(xiàn)什么情況呢?下面我們將解決這種意外情況。

處理負(fù)指數(shù)

要再增加一層復(fù)雜度,假定正在向 Math.pow() 方法傳遞負(fù)指數(shù)。在這種情況下,指數(shù)為負(fù),一種簡單的解決方案是將底數(shù)轉(zhuǎn)換為小數(shù),使指數(shù)為正。例如,8-2 可轉(zhuǎn)換為 (1/8)2。我們以可編程的方式用底數(shù) x 來除 1,用 -1 來乘 y(參見示例 6)。

示例 6

  1. if( y < 0 )  
  2. {  
  3.   x = (1/x); // convert base number to fraction  
  4.   y *= -1// make exponent positive  

現(xiàn)在,我們已經(jīng)討論了用于在 Java ME 中估計冪函數(shù)的“強(qiáng)力”幾何衰變算法。讀者會注意到,對于較大的底數(shù)和指數(shù)分子組合,樸素的“強(qiáng)力”試探性方法有性能問題。請考慮示例 85/2。使用此算法的起始值將為 85 = 32,768。如果使用 1% 的衰變,則要求全部 5196 次迭代都求該解,這樣幾乎達(dá)不到***。謹(jǐn)記此事實并且不提供改善的試探性搜索算法,我們轉(zhuǎn)到二次逼近,這會提供更多合理的迭代要求。#p#

使用 Math.sqrt() 方法的 ME Math.pow()

開發(fā)我們的 Math.pow() 方法的第二種方法是通過使用 Math.sqrt() 方法(參見示例 7)。使用Math.sqrt() 方法要求我們對具有偶分母的冪進(jìn)行估計。例如,n = 82/3 => n3 = 82 是一個棘手問題,因為我們需要立方根,而非平方根。為了調(diào)整示例中的此問題,我們我們就對兩端再求一次平方:n3 = 82 => n6 = 84。然后,我們就可以繼續(xù)進(jìn)行,恰好在兩次迭代之內(nèi)求出解。

當(dāng)然,我們的 ME Math.pow() 方法的指數(shù)不會以分子和分母的形式開始,而是向我們傳遞一個實數(shù)。我們將此實數(shù)轉(zhuǎn)換為具有偶分母的小數(shù),然后利用相應(yīng)的平方根數(shù)來對
n 求解。在我們的 n = 82/3 示例中,我們進(jìn)行如下轉(zhuǎn)換:
n = 82/3 => n = 8683/1024 => n1024 = 8683
我們選擇 1024 作為分母,因為對平方根函數(shù)迭代 10 次將得到 n 的值。特別指出,(n1024)(1/(2^10)) = n。當(dāng)然,我們可能需要根據(jù)方程右側(cè)的大小來減少迭代次數(shù)。示例 7 演示了這種方法。

示例 7

  1. ouble pow(double x, double y)  
  2. {       
  3.   //Convert the real power to a fractional form  
  4.   int den = 1024//declare the denominator to be 1024  
  5.   /*Conveniently 2^10=1024, so taking the square root 10  
  6.   times will yield our estimate for n. In our example  
  7.   n^3=8^2  n^1024 = 8^683.*/ 
  8.   int num = (int)(y*den); // declare numerator  
  9.   int iterations = 10; /*declare the number of square root  
  10.     iterations associated with our denominator, 1024.*/ 
  11.   double n = Double.MAX_VALUE; /* we initialize our      
  12.     estimate, setting it to max*/ 
  13.   while( n >= Double.MAX_VALUE && iterations > 1)  
  14.   {  
  15.     /* We try to set our estimate equal to the right  
  16.     hand side of the equation (e.g., 8^2048). If this  
  17.     number is too large, we will have to rescale. */ 
  18.     n = x;  
  19.     forint i=1; i < num; i++ )n*=x;  
  20.     /*here, we handle the condition where our starting  
  21.     point is too large*/ 
  22.     if( n >= Double.MAX_VALUE )  
  23.     {  
  24.       iterations--; /*reduce the iterations by one*/ 
  25.       den = (int)(den / 2); /*redefine the denominator*/ 
  26.       num = (int)(y*den); //redefine the numerator  
  27.     }  
  28.   }  
  29.   /*************************************************  
  30.   ** We now have an appropriately sized right-hand-side.  
  31.   ** Starting with this estimate for n, we proceed.  
  32.   **************************************************/ 
  33.   forint i = 0; i < iterations; i++ )  
  34.   {  
  35.     n = Math.sqrt(n);  
  36.   }  
  37.   // Return our estimate  
  38.   return n;  

自然對數(shù)和歐拉 e 的泰勒級數(shù)逼近

對于正實數(shù)產(chǎn)生 Math.pow() 方法最簡便的方式之一是鏈接幾個方法,包括使用 泰勒級數(shù)。假定我們需要冪 y = xb。該式與 ln(y) = b*ln(x) 等價。進(jìn)而,我們可以使用泰勒級數(shù)擴(kuò)展估算 x 的自然對數(shù),如下所示。

  1. ln(x) = (x-1) –(x-1)2/2 + (x-1)3/3 - (x-1)4/4….if |x-1|<=1 OR  
  2. ln(x) = 1/(x/(x-1)) + 1/(x/(x-1))2 + 1/(x/(x-1))3… if |x|>1 

由于 x 為正,因而 x 的整個域為這些方程所覆蓋。此交錯級數(shù)可以提供對底數(shù)對數(shù)的非常接近的逼近。用指數(shù)乘以此對數(shù)將得到 ln(y),方程的右側(cè) ln(y)=b*ln(x)?,F(xiàn)在,我們僅需求出 eln(y) 即可完成運(yùn)算。我們使用另一個泰勒級數(shù)擴(kuò)展來完成此過程:ex = 1 + x + x2 / 2! + x3 / 3! + … 使用這兩個公式,即可求得問題的解,如示例 8 所示。

示例 8

  1. double pow(double a, double b)  
  2. {  
  3.   // true if base is greater than 1  
  4.   boolean gt1 = (Math.sqrt((a-1)*(a-1)) <= 1)? false:true;  
  5.   int oc = -1// used to alternate math symbol (+,-)  
  6.   int iter = 20// number of iterations  
  7.   double p, x, x2, sumX, sumY;  
  8.   // is exponent a whole number?  
  9.   if( (b-Math.floor(b)) == 0 )  
  10.   {  
  11.     // return base^exponent  
  12.     p = a;  
  13.     forint i = 1; i < b; i++ )p *= a;  
  14.     return p;  
  15.   }  
  16.   x = (gt1)?  
  17.       (a /(a-1)): // base is greater than 1  
  18.       (a-1); // base is 1 or less  
  19.   sumX = (gt1)?  
  20.       (1/x): // base is greater than 1  
  21.       x; // base is 1 or less  
  22.   forint i = 2; i < iter; i++ )  
  23.   {  
  24.     // find x^iteration  
  25.     p = x;  
  26.     forint j = 1; j < i; j++)p *= x;  
  27.     double xTemp = (gt1)?  
  28.         (1/(i*p)): // base is greater than 1  
  29.         (p/i); // base is 1 or less  
  30.     sumX = (gt1)?  
  31.         (sumX+xTemp): // base is greater than 1  
  32.         (sumX+(xTemp*oc)); // base is 1 or less  
  33.     oc *= -1// change math symbol (+,-)  
  34.   }  
  35.   x2 = b * sumX;  
  36.   sumY = 1+x2; // our estimate  
  37.   forint i = 2; i <= iter; i++ )  
  38.   {  
  39.     // find x2^iteration  
  40.     p = x2;  
  41.     forint j = 1; j < i; j++)p *= x2;  
  42.     // multiply iterations (ex: 3 iterations = 3*2*1)  
  43.     int yTemp = 2;  
  44.     forint j = i; j > 2; j-- )yTemp *= j;  
  45.     // add to estimate (ex: 3rd iteration => (x2^3)/(3*2*1) )  
  46.     sumY += p/yTemp;  
  47.   }  
  48.   return sumY; // return our estimate  

幾乎在所有情況下,由泰勒級數(shù)逼近返回的估計比衰變算法方法更為精確,而 Math.sqrt() 也可以產(chǎn)生更好的結(jié)果。泰勒級數(shù)方法所使用的計算周期較少, 但在值趨于 0 時會不穩(wěn)定。Math.sqrt() 結(jié)果可以提供良好的逼近,通常到第三位數(shù)字。有關(guān)使用多個任意分配的正實型變量的方法的比較,請參見表 1。我們可以看到,對于實際應(yīng)用, Math.sqrt() 或泰勒級數(shù)方法對于是大多數(shù)值都比較優(yōu)良。

表 1:衰變算法和平方根方法的比較

底數(shù),指數(shù) 實際結(jié)果 泰勒級數(shù)逼近 Math.sqrt() 估計值 衰變算法估計值

8.0, 0.75 4.75682846 4.7423353221144557 4.75682846 4.751286816
8.0, 0.667 4.002774 3.9919355054959973 3.994588452 3.994453662
16.0, 8.0 4294967296 4294967296 4294967296 4294752931
32.0, 5.0 33554432 33554432 33554432 33553177.47
11.0, 3.0 1331 1331 1331 1330.967224
10.0, 10.0 10000000000 10000000000 10000000000 9999699608
77.0, 3.0 456533 456533 456533 456527.6254
5.0, 15.0 30517578125 30517578125 30517578125 30516279235
15.0, 9.0 38443359375 38443359375 38443359375 38440083836
3.0, 21.0 10460353203 10460353203 10460353203 10459907131
5.0, 0.05 1.083798387 1.0837883791740017 1.083457755 1.08205432
7.0, 0.37 2.054406 2.0529191207908064 2.050973357 2.051043668
1.5, 0.789 1.377006542 1.377006541546755 1.376496289 1.376798426
1.5, 3.789 4.647397078 4.647381683179335 4.64015972 4.644836289
0.06282, 0.325784 0.405919146 0.41327102396968585 0 0.06282
0.7261, 0.20574 0.936270645 0.9362706445348806 0.93646901 0.7261
0.903272, 0.48593 0.951767579 0.951767579257642 0.951823588 0.903272
0.821111, 0.767392 0.85963221 0.8596322100794738 0.859766145 0.821111
0.24352, .004322 0.99391353 0.9939136545397182 0.994497397 0.24352
0.000125, .99556 0.000130089 627097113.1963351 0 0.000125

編程注意事項和結(jié)論

本文已經(jīng)解決了在 Java ME 中開發(fā) Math.pow() 方法的三種途徑。雖然樸素的“強(qiáng)力”幾何衰變試探性方法比較不錯,而且對于小問題可能很有用處,但是 Math.sqrt() 改寫對于大多數(shù)范圍的應(yīng)用可能要好一些。***方法可能是泰勒級數(shù)逼近。顯然,這三個示例均未包括完成該任務(wù)的特有方法(如二分法及參考資料中介紹的其他方法),并且我們希望其他方法可以提供占用較少資源的更為高效的技巧。***需要注意的一點(diǎn)是:如果要求您開發(fā)此類方法,務(wù)必考慮其應(yīng)用,所需的參數(shù)以及預(yù)期的結(jié)果和精度。

 

【編輯推薦】

  1. 碰撞檢測算法在Java ME中的實現(xiàn)
  2. Java ME多模搜索技術(shù)初探
  3. 淺談Java SE、Java EE、Java ME三者的區(qū)別
  4. 如何解決Java ME設(shè)備碎片問題
  5. Java ME平臺中的URLEncoder實現(xiàn)類
責(zé)任編輯:佚名 來源: IT168
相關(guān)推薦

2009-06-17 11:27:00

setClip方法J2ME

2016-09-18 16:58:09

JavaProperties

2010-09-30 12:53:00

J2MECSS

2010-10-09 10:30:03

JS event

2011-08-29 15:10:19

JAVALua 腳本

2021-04-13 09:20:21

JavaUnsafejava8

2022-09-14 08:00:00

區(qū)塊鏈加密貨幣挖礦

2009-06-29 17:57:30

ApplicationJSP

2019-11-07 23:48:12

shell腳本getopts

2015-06-08 09:05:10

Java原型模式

2009-06-08 20:07:44

Eclipse中使用p

2023-06-28 08:34:02

Bind()函數(shù)JavaScript

2012-05-10 10:53:10

Linuxhistory

2010-09-29 16:20:06

J2MEWeb服務(wù)API

2010-09-29 10:41:18

J2MEJVM

2009-12-02 16:04:44

PHP fsockop

2009-03-04 13:10:41

SQL語句INSERTDELETE

2010-01-28 10:31:32

Android使用SD

2011-03-28 09:35:06

iBaitsSqlMapClien

2025-02-17 08:00:00

DeepSeek模型AI
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號