Example usage for java.math BigDecimal subtract

List of usage examples for java.math BigDecimal subtract

Introduction

In this page you can find the example usage for java.math BigDecimal subtract.

Prototype

public BigDecimal subtract(BigDecimal subtrahend) 

Source Link

Document

Returns a BigDecimal whose value is (this - subtrahend) , and whose scale is max(this.scale(), subtrahend.scale()) .

Usage

From source file:org.cirdles.ambapo.UTMToLatLong.java

/**
 * //w ww.j a v  a2  s  .c o m
 * @param originalTau
 * @param sigma
 * @param eccentricity
 * @param hemisphere
 * @return latitude
 */
private static BigDecimal calcLatitude(BigDecimal originalTau, BigDecimal sigma, BigDecimal eccentricity,
        char hemisphere) {

    BigDecimal funcOfTau = functionOfTau(originalTau, sigma, originalTau).setScale(PRECISION,
            RoundingMode.HALF_UP);

    BigDecimal changeInTau = changeInTau(eccentricity, originalTau, sigma);

    BigDecimal newTau = originalTau.subtract(funcOfTau.divide(changeInTau, PRECISION, RoundingMode.HALF_UP));

    BigDecimal latitude = (new BigDecimal(Math.atan(newTau.doubleValue())))
            .multiply(new BigDecimal(180.0 / Math.PI));

    if (hemisphere == 'S')
        latitude = latitude.multiply(new BigDecimal(-1));

    return latitude;

}

From source file:org.kalypso.ui.wizards.results.ResultSldHelper.java

private static void configurePolygonSymbolizer(final SurfacePolygonSymbolizer symbolizer,
        final BigDecimal minValue, final BigDecimal maxValue) throws FilterEvaluationException {
    final PolygonColorMap templateColorMap = symbolizer.getColorMap();
    final PolygonColorMap newColorMap = new PolygonColorMap_Impl();

    // retrieve stuff from template-entries
    final PolygonColorMapEntry fromEntry = templateColorMap.findEntry("from", null); //$NON-NLS-1$
    final PolygonColorMapEntry toEntry = templateColorMap.findEntry("to", null); //$NON-NLS-1$

    // Fill/*www .  j  a  va  2s.co m*/
    final Color fromPolygonColor = fromEntry.getFill().getFill(null);
    final Color toPolygonColor = toEntry.getFill().getFill(null);
    final double polygonOpacity = fromEntry.getFill().getOpacity(null);

    // Stroke
    final Color fromLineColor = fromEntry.getStroke().getStroke(null);
    final Color toLineColor = toEntry.getStroke().getStroke(null);
    final double lineOpacity = fromEntry.getStroke().getOpacity(null);

    // step width
    final double stepWidth = fromEntry.getTo(null);

    // scale of the step width
    final BigDecimal setScale = new BigDecimal(fromEntry.getFrom(null)).setScale(0, BigDecimal.ROUND_FLOOR);
    final int stepWidthScale = setScale.intValue();

    // get rounded values below min and above max (rounded by first decimal)
    // as a first try we will generate isareas by using class steps of 0.1
    // later, the classes will be created by using user defined class steps.
    // for that we fill an array of calculated (later user defined values) from max to min
    final BigDecimal minDecimal = minValue.setScale(1, BigDecimal.ROUND_FLOOR);
    final BigDecimal maxDecimal = maxValue.setScale(1, BigDecimal.ROUND_CEILING);

    final BigDecimal polygonStepWidth = new BigDecimal(stepWidth).setScale(stepWidthScale,
            BigDecimal.ROUND_FLOOR);
    int numOfClasses = (maxDecimal.subtract(minDecimal).divide(polygonStepWidth)).intValue();
    // set to provide more them 1 or 0 classes. in such cases the color map will not be created, that results error.  
    if (numOfClasses < 2) {
        numOfClasses = (maxDecimal.subtract(minDecimal).divide(polygonStepWidth.divide(new BigDecimal(4))))
                .intValue();
    }
    for (int currentClass = 0; currentClass < numOfClasses; currentClass++) {
        final double fromValue = minDecimal.doubleValue() + currentClass * polygonStepWidth.doubleValue();
        final double toValue = minDecimal.doubleValue() + (currentClass + 1) * polygonStepWidth.doubleValue();

        // Stroke
        Color lineColor;
        if (fromLineColor == toLineColor)
            lineColor = fromLineColor;
        else
            lineColor = interpolateColor(fromLineColor, toLineColor, currentClass, numOfClasses);

        // Fill
        final Color polygonColor = interpolateColor(fromPolygonColor, toPolygonColor, currentClass,
                numOfClasses);
        lineColor = polygonColor;

        final Stroke stroke = StyleFactory.createStroke(lineColor, lineOpacity, 1);

        final Fill fill = StyleFactory.createFill(polygonColor, polygonOpacity);

        final ParameterValueType label = StyleFactory.createParameterValueType("Isoflche " + currentClass); //$NON-NLS-1$
        final ParameterValueType from = StyleFactory.createParameterValueType(fromValue);
        final ParameterValueType to = StyleFactory.createParameterValueType(toValue);

        final PolygonColorMapEntry colorMapEntry = new PolygonColorMapEntry_Impl(fill, stroke, label, from, to);
        newColorMap.addColorMapClass(colorMapEntry);
    }

    symbolizer.setColorMap(newColorMap);
}

From source file:com.prowidesoftware.swift.utils.SwiftFormatUtils.java

/**
 * Return the number of decimals for the given number, which can be <code>null</code>, in which case this method returns zero.
 * //from   w w w  .j  a va 2  s  .c o m
 * @return the number of decimal in the number or zero if there are none or the amount is <code>null</code>
 * @since 7.8
 */
public static int decimalsInAmount(final BigDecimal amount) {
    if (amount != null) {
        BigDecimal d = new BigDecimal(amount.toString());
        BigDecimal result = d.subtract(d.setScale(0, RoundingMode.FLOOR)).movePointRight(d.scale());
        if (result.intValue() != 0) {
            return result.toString().length();
        }
    }
    return 0;
}

From source file:Main.java

public static BigDecimal sine(BigDecimal x) {
    BigDecimal lastVal = x.add(BigDecimal.ONE);
    BigDecimal currentValue = x;
    BigDecimal xSquared = x.multiply(x);
    BigDecimal numerator = x;//  w  w w  .  ja va  2  s. com
    BigDecimal denominator = BigDecimal.ONE;
    int i = 0;

    while (lastVal.compareTo(currentValue) != 0) {
        lastVal = currentValue;

        int z = 2 * i + 3;

        denominator = denominator.multiply(BigDecimal.valueOf(z));
        denominator = denominator.multiply(BigDecimal.valueOf(z - 1));
        numerator = numerator.multiply(xSquared);

        BigDecimal term = numerator.divide(denominator, SCALE + 5, ROUNDING_MODE);

        if (i % 2 == 0) {
            currentValue = currentValue.subtract(term);
        } else {
            currentValue = currentValue.add(term);
        }

        i++;
    }
    return currentValue;
}

From source file:Main.java

public static BigDecimal cosine(BigDecimal x) {

    BigDecimal currentValue = BigDecimal.ONE;
    BigDecimal lastVal = currentValue.add(BigDecimal.ONE);
    BigDecimal xSquared = x.multiply(x);
    BigDecimal numerator = BigDecimal.ONE;
    BigDecimal denominator = BigDecimal.ONE;
    int i = 0;// w w  w  .  java2 s.  c  o  m

    while (lastVal.compareTo(currentValue) != 0) {
        lastVal = currentValue;

        int z = 2 * i + 2;

        denominator = denominator.multiply(BigDecimal.valueOf(z));
        denominator = denominator.multiply(BigDecimal.valueOf(z - 1));
        numerator = numerator.multiply(xSquared);

        BigDecimal term = numerator.divide(denominator, SCALE + 5, ROUNDING_MODE);

        if (i % 2 == 0) {
            currentValue = currentValue.subtract(term);
        } else {
            currentValue = currentValue.add(term);
        }
        i++;
    }

    return currentValue;
}

From source file:Main.java

/**
 * Compute e^x to a given scale./*from w  w  w  .j a  v  a  2  s  . c  o  m*/
 * Break x into its whole and fraction parts and
 * compute (e^(1 + fraction/whole))^whole using Taylor's formula.
 * @param x the value of x
 * @param scale the desired scale of the result
 * @return the result value
 */
public static BigDecimal exp(BigDecimal x, int scale) {
    // e^0 = 1
    if (x.signum() == 0) {
        return BigDecimal.valueOf(1);
    }

    // If x is negative, return 1/(e^-x).
    else if (x.signum() == -1) {
        return BigDecimal.valueOf(1).divide(exp(x.negate(), scale), scale, BigDecimal.ROUND_HALF_EVEN);
    }

    // Compute the whole part of x.
    BigDecimal xWhole = x.setScale(0, BigDecimal.ROUND_DOWN);

    // If there isn't a whole part, compute and return e^x.
    if (xWhole.signum() == 0)
        return expTaylor(x, scale);

    // Compute the fraction part of x.
    BigDecimal xFraction = x.subtract(xWhole);

    // z = 1 + fraction/whole
    BigDecimal z = BigDecimal.valueOf(1).add(xFraction.divide(xWhole, scale, BigDecimal.ROUND_HALF_EVEN));

    // t = e^z
    BigDecimal t = expTaylor(z, scale);

    BigDecimal maxLong = BigDecimal.valueOf(Long.MAX_VALUE);
    BigDecimal result = BigDecimal.valueOf(1);

    // Compute and return t^whole using intPower().
    // If whole > Long.MAX_VALUE, then first compute products
    // of e^Long.MAX_VALUE.
    while (xWhole.compareTo(maxLong) >= 0) {
        result = result.multiply(intPower(t, Long.MAX_VALUE, scale)).setScale(scale,
                BigDecimal.ROUND_HALF_EVEN);
        xWhole = xWhole.subtract(maxLong);

        Thread.yield();
    }
    return result.multiply(intPower(t, xWhole.longValue(), scale)).setScale(scale, BigDecimal.ROUND_HALF_EVEN);
}

From source file:com.vsthost.rnd.commons.math.ext.linear.DMatrixUtils.java

/**
 * Returns the closest rounded value of the given value for the given steps.
 *
 * @param value The original value to be rounded.
 * @param steps The steps./* w w  w. j  a va  2s .co m*/
 * @return The closest rounded value of the given value for the given steps.
 */
public static BigDecimal roundToClosest(double value, double steps) {
    final BigDecimal down = DMatrixUtils.roundDownTo(value, steps);
    final BigDecimal up = DMatrixUtils.roundUpTo(value, steps);
    final BigDecimal orig = new BigDecimal(String.valueOf(value));
    if (orig.subtract(down).abs().compareTo(orig.subtract(up).abs()) < 0) {
        return down;
    }
    return up;
}

From source file:Main.java

public static BigDecimal sine(BigDecimal x) {
    BigDecimal lastVal = x.add(BigDecimal.ONE);
    BigDecimal currentValue = x;
    BigDecimal xSquared = x.multiply(x);
    BigDecimal numerator = x;/* ww w. j a  v a 2 s  .  c  o  m*/
    BigDecimal denominator = BigDecimal.ONE;
    int i = 0;

    while (lastVal.compareTo(currentValue) != 0) {
        lastVal = currentValue;

        int z = 2 * i + 3;

        denominator = denominator.multiply(BigDecimal.valueOf(z));
        denominator = denominator.multiply(BigDecimal.valueOf(z - 1));
        numerator = numerator.multiply(xSquared);

        BigDecimal term = numerator.divide(denominator, SCALE + 5, ROUNDING_MODE);

        if (i % 2 != 0) {
            currentValue = currentValue.add(term);
        } else {
            currentValue = currentValue.subtract(term);
        }

        i++;
    }
    return currentValue;
}

From source file:engine.Pi.java

/**
     * Compute the value, in radians, of the arctangent of 
     * the inverse of the supplied integer to the specified
     * number of digits after the decimal point.  The value
     * is computed using the power series expansion for the
     * arc tangent:/*from   w  w w  .  j  a va 2 s .c  om*/
     *
     * arctan(x) = x - (x^3)/3 + (x^5)/5 - (x^7)/7 + 
     *     (x^9)/9 ...
     */
    public static BigDecimal arctan(int inverseX, int scale) {
        BigDecimal result, numer, term;
        BigDecimal invX = BigDecimal.valueOf(inverseX);
        BigDecimal invX2 = BigDecimal.valueOf(inverseX * inverseX);

        numer = BigDecimal.ONE.divide(invX, scale, roundingMode);

        result = numer;
        int i = 1;
        do {
            numer = numer.divide(invX2, scale, roundingMode);
            int denom = 2 * i + 1;
            term = numer.divide(BigDecimal.valueOf(denom), scale, roundingMode);
            if ((i % 2) != 0) {
                result = result.subtract(term);
            } else {
                result = result.add(term);
            }
            i++;
        } while (term.compareTo(BigDecimal.ZERO) != 0);
        return result;
    }

From source file:Main.java

public static BigDecimal cosine(BigDecimal x) {

    BigDecimal currentValue = BigDecimal.ONE;
    BigDecimal lastVal = currentValue.add(BigDecimal.ONE);
    BigDecimal xSquared = x.multiply(x);
    BigDecimal numerator = BigDecimal.ONE;
    BigDecimal denominator = BigDecimal.ONE;
    int i = 0;//  w w w .  java  2 s .com

    while (lastVal.compareTo(currentValue) != 0) {
        lastVal = currentValue;

        int z = 2 * i + 2;

        denominator = denominator.multiply(BigDecimal.valueOf(z));
        denominator = denominator.multiply(BigDecimal.valueOf(z - 1));
        numerator = numerator.multiply(xSquared);

        BigDecimal term = numerator.divide(denominator, SCALE + 5, ROUNDING_MODE);

        if (i % 2 != 0) {
            currentValue = currentValue.add(term);
        } else {
            currentValue = currentValue.subtract(term);
        }
        i++;
    }

    return currentValue;
}