Root with Bigdecimal

Asked

Viewed 348 times

6

I’m racking my brain to turn a root into a code. Segue a foto da raiz com índice

I know it works kind of like this:

Math.pow(625, (1.0/4)) = raiz 4ª de 625 = 5

My headache is that my dice are BigDecimals

Sting prazoIn = idtText.getText();
BigDecimal prazo = new BigDecimal(prazoIn);

String valorIn = idtText.getText2();
BigDecimal valor = new BigDecimal(valorIn);


BigDecimal umPonto = new BigDecimal(1.0);

//Não sei como fazer com valores BigDecimais
BigDecimal raiz = Math.pow(valor, (umPonto.divide(prazo);
  • Avoid using Math.pow to work with BigDecimal. You lose the desired properties

  • How would I make the calculation in question with Bigdecimal values?

  • a mathematical bullshit involving logarithms and exponentials, I’m remembering here how

  • Or use an external library: https://github.com/eobermuhlner/big-math

  • I’m trying to remember the API of BigDecimal to view available operations

  • Well, I used a library to do the calculations of logarithms and exponentials, so I’m going to owe =\

Show 1 more comment

2 answers

5


Unfortunately you don’t have a solution ready. If converting to another type is a valid option, then you have no reason to use BigDecimal. The conversion will cause loss of value, and worse, so that often naive tests will not detect.

Since Java does not provide anything ready you have to do a function of your own. There’s one in the O.R.:

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

I put in the Github for future reference.

Also has square root option.

Have libraries ready:

  • I believe that instead of comparing the references of exponent and no whether to use a comparison of the content with equals; get out of if (exponent != no) for if (!exponente.equals(no))

  • 1

    @Jeffersonquesado is possible, I just took what’s in the OS, I didn’t check if it’s correct.

0

Do it this way, depending on the size of the accuracy you need, solve..

public BigDecimal raiz() {
    return new BigDecimal(Math.pow(new BigDecimal(1.172544).floatValue(), (1.0 / 7)));
}

//retorno 1.022999903326536230707688446273095905780792236328125
  • 2

    Really insist on the Math.pow moving everything to floating point is a valid option, although I believe that the loss of precision may escape the purist intention of doing operations with BigDecimal. And if it’s done with doubleValue you maintain a higher calculation accuracy

  • Thanks for the tip

Browser other questions tagged

You are not signed in. Login or sign up in order to post.