成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

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

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

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

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

產生整數解的 ME Math.pow() 方法

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

示例 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. }  

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

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

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

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

示例 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 開始遞減,直到找到預期的數字,假定預期數字是一個正整數。這種類型的算法構成了強力試探性方法的基礎。

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

示例 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% 強制遞減。您可能需要根據編程精度要求來改變此值。也可能考慮包括編程邏輯,該邏輯用于將前一迭代解與當前迭代進行比較,然后,如果有改善繼續進行迭代,但是,如果解已回歸,則返回前一個值。

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

處理負指數

要再增加一層復雜度,假定正在向 Math.pow() 方法傳遞負指數。在這種情況下,指數為負,一種簡單的解決方案是將底數轉換為小數,使指數為正。例如,8-2 可轉換為 (1/8)2。我們以可編程的方式用底數 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  

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

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

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

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

自然對數和歐拉 e 的泰勒級數逼近

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

  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 的整個域為這些方程所覆蓋。此交錯級數可以提供對底數對數的非常接近的逼近。用指數乘以此對數將得到 ln(y),方程的右側 ln(y)=b*ln(x)。現在,我們僅需求出 eln(y) 即可完成運算。我們使用另一個泰勒級數擴展來完成此過程: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  

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

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

底數,指數 實際結果 泰勒級數逼近 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

編程注意事項和結論

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

 

【編輯推薦】

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

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

2021-04-13 09:20:21

JavaUnsafejava8

2011-08-29 15:10:19

JAVALua 腳本

2009-06-29 17:57:30

ApplicationJSP

2019-11-07 23:48:12

shell腳本getopts

2022-09-14 08:00:00

區塊鏈加密貨幣挖礦

2015-06-08 09:05:10

Java原型模式

2009-06-08 20:07:44

Eclipse中使用p

2023-06-28 08:34:02

Bind()函數JavaScript

2010-09-29 10:41:18

J2MEJVM

2010-09-29 16:20:06

J2MEWeb服務API

2012-05-10 10:53:10

Linuxhistory

2009-12-02 16:04:44

PHP fsockop

2011-03-28 09:35:06

iBaitsSqlMapClien

2010-01-28 10:31:32

Android使用SD

2009-03-04 13:10:41

SQL語句INSERTDELETE

2016-12-27 10:19:42

JavaScriptindexOf
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: www.99re| 91免费在线 | 成人午夜激情 | 成人国产精品久久 | 久久免费电影 | h视频在线播放 | 91精品国产综合久久精品图片 | 国产激情网 | 久久在视频 | av免费网站在线观看 | 欧美日韩1区2区3区 欧美久久一区 | 亚洲va中文字幕 | 欧美日韩视频 | 久久777| 国产精品中文 | 一区二区三区国产 | 日日日操| 成人免费小视频 | 免费色网址 | 国产成人精品一区二区三区在线 | 一级毛片网 | 亚洲精品久久久蜜桃网站 | 九九九久久国产免费 | 亚洲综合天堂网 | 久久亚洲天堂 | 国产精品久久久久久妇女6080 | 日本精品久久久久久久 | 亚洲成人在线免费 | 啪啪网页 | 国产精品99久久久久久动医院 | 在线观看视频一区二区三区 | 黑人精品欧美一区二区蜜桃 | 天天操网 | 日韩av电影在线观看 | 精品日韩在线 | 亚洲人成在线观看 | 国产欧美一区二区三区国产幕精品 | www.久久.com| 精品久久久久久久久久 | 国产久| 草久久 |