2017-03-07 1 views
0

J'essaye d'écrire un code manuel pour Math.pow et cela aussi en utilisant le type de données BigDecimal comme je travaillerais avec de très petites valeurs plus tard.Manuel Math.pow utilisant BigDecimal en Java

J'ai obtenu ce code pour math.pow que j'ai ensuite essayé de convertir en 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; 
} 
} 

I converti le type de données double et int BigDecimal et a essayé de changer le code en conséquence aussi, mais en quelque sorte je ne reçois pas les résultats corrects.

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; 
} 

Je suis en train de l'exécuter sur

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

    powerBig(Euler,check); 

Mais tout ce que je reçois en tant que sortie est la valeur Teh Euler. Quelqu'un peut-il m'aider avec l'erreur que j'ai dans mon code?

Le code fonctionne maintenant après avoir changé le type d'exposant 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' - que pensez-vous que cela fait? Même «equals» ne fonctionne pas avec «BigDecimal», pourquoi croiriez-vous que '==' le ferait? –

+0

Je l'ai changé en exponent.signum()> 0 mais cela ne fonctionne toujours pas – Saad

+0

Étant donné que ['BigDecimal.sigNum()' renvoie '{-1,0,1}'] (http://docs.oracle.com /javase/7/docs/api/java/math/BigDecimal.html#signum()) Que diable fait 'pour (int i = 1; i <= absExponent.signum(); i ++)' faire? En bref, déboguez votre code - les problèmes viennent tous d'un manque d'attention ... –

Répondre

1

Votre problème est que BigDecimal.sigNum() retourne 1, 0 ou -1 si le nombre est positif, nul ou négatif si absExponent. Signum() allways retour 1 une boucle votre se terminera à la première fois qu'il est exécuté

Cette version a travaillé avec l'exemple euler

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; 
} 

également BigDecimal cl ass ont une fonction de pow donc si vous voulez garder simple vous pouvez simplement mettre

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

Cela va se casser horriblement si l'exposant n'est pas un nombre entier. Alors .. pourquoi ne pas utiliser BigDecimal.pow? –

+0

Etes-vous sûr qu'il y a une chance que la clause else soit exécutée? Tant que 'no' est créé avec un nouvel objet, 'no! = Exponent' sera toujours vrai. –