Compute the integral root of x to a given scale, x >= 0. - Java java.lang

Java examples for java.lang:Math Operation

Description

Compute the integral root of x to a given scale, x >= 0.

Demo Code

/*/*www. j  a  v  a2 s .com*/
 Anders H?fft, note: This class was downloaded as a quick, and temprory, way of getting a BigDecimal ln() method. 
 The code belongs to Cyclos. See comment below:

 This file is part of Cyclos (www.cyclos.org).
 A project of the Social Trade Organisation (www.socialtrade.org).
 Cyclos is free software; you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation; either version 2 of the License, or
 (at your option) any later version.
 Cyclos is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 GNU General Public License for more details.
 You should have received a copy of the GNU General Public License
 along with Cyclos; if not, write to the Free Software
 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */
//package com.java2s;
import java.math.BigDecimal;

public class Main {
    public static void main(String[] argv) throws Exception {
        BigDecimal x = new BigDecimal("1234");
        long index = 2;
        int scale = 2;
        System.out.println(intRoot(x, index, scale));
    }

    /**
     * Compute the integral root of x to a given scale, x >= 0. Use Newton's algorithm.
     * @param x the value of x
     * @param index the integral root value
     * @param scale the desired scale of the result
     * @return the result value
     */
    public static BigDecimal intRoot(BigDecimal x, final long index,
            final int scale) {
        // Check that x >= 0.
        if (x.signum() < 0) {
            throw new IllegalArgumentException("x < 0");
        }

        final int sp1 = scale + 1;
        final BigDecimal n = x;
        final BigDecimal i = BigDecimal.valueOf(index);
        final BigDecimal im1 = BigDecimal.valueOf(index - 1);
        final BigDecimal tolerance = BigDecimal.valueOf(5).movePointLeft(
                sp1);
        BigDecimal xPrev;

        // The initial approximation is x/index.
        x = x.divide(i, scale, BigDecimal.ROUND_HALF_EVEN);

        // Loop until the approximations converge
        // (two successive approximations are equal after rounding).
        do {
            // x^(index-1)
            final BigDecimal xToIm1 = intPower(x, index - 1, sp1);

            // x^index
            final BigDecimal xToI = x.multiply(xToIm1).setScale(sp1,
                    BigDecimal.ROUND_HALF_EVEN);

            // n + (index-1)*(x^index)
            final BigDecimal numerator = n.add(im1.multiply(xToI))
                    .setScale(sp1, BigDecimal.ROUND_HALF_EVEN);

            // (index*(x^(index-1))
            final BigDecimal denominator = i.multiply(xToIm1).setScale(sp1,
                    BigDecimal.ROUND_HALF_EVEN);

            // x = (n + (index-1)*(x^index)) / (index*(x^(index-1)))
            xPrev = x;
            x = numerator.divide(denominator, sp1, BigDecimal.ROUND_DOWN);

            Thread.yield();
        } while (x.subtract(xPrev).abs().compareTo(tolerance) > 0);

        return x;
    }

    /**
     * Compute x^exponent to a given scale.
     * @param x the value x
     * @param exponent the exponent value
     * @param scale the desired scale of the result
     * @return the result value
     */
    public static BigDecimal intPower(BigDecimal x, long exponent,
            final int scale) {
        // If the exponent is negative, compute 1/(x^-exponent).
        if (exponent < 0) {
            return BigDecimal.valueOf(1).divide(
                    intPower(x, -exponent, scale), scale,
                    BigDecimal.ROUND_HALF_EVEN);
        }

        BigDecimal power = BigDecimal.valueOf(1);

        // Loop to compute value^exponent.
        while (exponent > 0) {

            // Is the rightmost bit a 1?
            if ((exponent & 1) == 1) {
                power = power.multiply(x).setScale(scale,
                        BigDecimal.ROUND_HALF_EVEN);
            }

            // Square x and shift exponent 1 bit to the right.
            x = x.multiply(x).setScale(scale, BigDecimal.ROUND_HALF_EVEN);
            exponent >>= 1;

            Thread.yield();
        }

        return power;
    }
}

Related Tutorials