2017-03-07 29 views
0

我正在嘗試爲Math.pow編寫一個手動代碼,並且也使用BigDecimal數據類型,因爲稍後我將使用非常小的值。在Java中使用BigDecimal手動Math.pow

我得到了這個代碼爲math.pow,然後我試圖轉換爲BigDecimal。

public static double power(double base, int exponent) { 
double ans = 1; 
if (exponent != 0) { 
    int absExponent = exponent > 0 ? exponent : (-1) * exponent; 
    for (int i = 1; i <= absExponent; i++) { 
     ans *= base; 
    } 

    if (exponent < 0) { 
     // For negative exponent, must invert 
     ans = 1.0/ans; 
    } 
} else { 
    // exponent is 0 
    ans = 1; 
} 

return ans; 
} 
} 

我轉換的雙和int數據類型爲BigDecimal,並試圖改變代碼也相應但不知何故,我沒有得到正確的結果。

public static BigDecimal powerBig(BigDecimal base, BigDecimal exponent) { 

    BigDecimal ans= new BigDecimal(1.0); 
    BigDecimal k= new BigDecimal(1.0); 
    BigDecimal t= new BigDecimal(-1.0); 
    BigDecimal no= new BigDecimal(0.0); 

    if (exponent != no) { 
     BigDecimal absExponent = exponent.signum() > 0 ? exponent : t.multiply(exponent); 
     for (int i = 1 ; i <= absExponent.signum(); i++) { 
      ans =ans.multiply(base); 
     } 

     if (exponent.signum() < 0) { 
      // For negative exponent, must invert 
      ans = k.divide(ans); 
     } 
    } else { 
     // exponent is 0 
     ans = k; 
    } 

    return ans; 
} 

我想對

BigDecimal check = new BigDecimal (4.0); 
BigDecimal Euler = new BigDecimal (2.7182818); 

    powerBig(Euler,check); 

運行它,但我得到的輸出是德歐拉值。有人能幫我解決我在代碼中出現的錯誤嗎?

代碼運行正在改變的指數型後爲int

public static BigDecimal powerBig(BigDecimal base, int exponent) { 

    BigDecimal ans= new BigDecimal(1.0); 
    BigDecimal k= new BigDecimal(1.0); 
    //BigDecimal t= new BigDecimal(-1.0); 
    //BigDecimal no= new BigDecimal(0.0); 

    if (exponent != 0) { 
     int absExponent = exponent > 0 ? exponent : (-1)*exponent; 
     for (int i = 1 ; i <= absExponent; i++) { 
      ans =ans.multiply(base); 
     } 

     if (exponent < 0) { 
      // For negative exponent, must invert 
      ans = k.divide(ans); 
     } 
    } else { 
     // exponent is 0 
     ans = k; 
    } 

    return ans; 
} 
+2

'exponent!= no' - 你認爲這樣做?即使'equals'與'BigDecimal'一起使用,你爲什麼會認爲'=='會呢? –

+0

我將它更改爲exponent.signum()> 0,但它仍然不起作用 – Saad

+0

鑑於['BigDecimal.sigNum()'返回的是'{-1,0,1}'](http://docs.oracle.com /javase/7/docs/api/java/math/BigDecimal.html#signum())在地球上做了什麼for(int i = 1; i <= absExponent.signum(); i ++)'do?總之,調試你的代碼 - 問題都來自缺乏關注... –

回答

1

您的問題是BigDecimal.sigNum()返回1,0或-1,如果數是正的,零或負,從而absExponent。西格納()將返回百達1中的循環將在第一時間它的執行

這個版本與歐拉例如工作結束

public static BigDecimal powerBig(BigDecimal base, BigDecimal exponent) { 

    BigDecimal ans= new BigDecimal(1.0); 
    BigDecimal k= new BigDecimal(1.0); 
    BigDecimal t= new BigDecimal(-1.0); 
    BigDecimal no= new BigDecimal(0.0); 

    if (exponent != no) { 
     BigDecimal absExponent = exponent.signum() > 0 ? exponent : t.multiply(exponent); 
     while (absExponent.signum() > 0){ 
      ans =ans.multiply(base); 
      absExponent = absExponent.subtract(BigDecimal.ONE); 
     } 

     if (exponent.signum() < 0) { 
      // For negative exponent, must invert 
      ans = k.divide(ans); 
     } 
    } else { 
     // exponent is 0 
     ans = k; 
    } 

    return ans; 
} 

也BigDecimal的CL屁股有pow功能,所以如果你想保持簡單,你可以放

BigDecimal Euler = new BigDecimal (2.7182818); 
Euler.pow(4); 
+1

如果指數不是整數,這將會非常糟糕。所以..爲什麼不使用BigDecimal.pow? –

+0

您確定有可能會執行'else-clause'嗎?只要'no'用新對象創建,'no!= exponent'將始終成立。 –