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:adalid.commons.util.ObjUtils.java

public static BigDecimal xMinusY(Object x, Object y) {
    BigDecimal bx = NumUtils.numberToBigDecimal(x);
    BigDecimal by = bx == null ? null : NumUtils.numberToBigDecimal(y);
    return bx == null || by == null ? null : bx.subtract(by);
}

From source file:Main.java

/**
 * Compute e^x to a given scale. Break x into its whole and fraction parts
 * and compute (e^(1 + fraction/whole))^whole using Taylor's formula.
 * //ww w  . ja va2s.  com
 * @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:org.cirdles.ambapo.UTMToLatLong.java

/**
 * //from www. j  a va2  s . c om
 * @param xiNorth
 * @param etaEast
 * @param betaSeries
 * @return eta prime
 */
private static BigDecimal calcEtaPrime(BigDecimal xiNorth, BigDecimal etaEast, double[] betaSeries) {

    double xiNorthDouble = xiNorth.doubleValue();
    double etaEastDouble = etaEast.doubleValue();

    BigDecimal cosOfXiNorth;
    BigDecimal sinhOfEtaEast;

    BigDecimal subtrahend = new BigDecimal(0.0);
    int multiplicand = 2;

    for (double beta : betaSeries) {

        cosOfXiNorth = new BigDecimal(Math.cos(multiplicand * xiNorthDouble));
        sinhOfEtaEast = new BigDecimal(Math.sinh(multiplicand * etaEastDouble));

        subtrahend.add(new BigDecimal(beta).multiply(cosOfXiNorth).multiply(sinhOfEtaEast));

        multiplicand += 2;

    }

    BigDecimal etaPrime = etaEast.subtract(subtrahend);

    return etaPrime;

}

From source file:org.csware.ee.utils.Tools.java

public static double subtract(double v1, double v2)

{

    BigDecimal b1 = new BigDecimal(Double.toString(v1));

    BigDecimal b2 = new BigDecimal(Double.toString(v2));

    return b1.subtract(b2).doubleValue();

}

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

/**
 * //  w  ww  .  j a  v  a 2 s.c o m
 * @param xiNorth
 * @param etaEast
 * @param betaSeries
 * @return xi prime
 */
private static BigDecimal calcXiPrime(BigDecimal xiNorth, BigDecimal etaEast, double[] betaSeries) {

    double xiNorthDouble = xiNorth.doubleValue();
    double etaEastDouble = etaEast.doubleValue();

    BigDecimal sinOfXiNorth;
    BigDecimal coshOfEtaEast;

    BigDecimal subtrahend = new BigDecimal(0.0);
    int multiplicand = 2;

    for (double beta : betaSeries) {

        sinOfXiNorth = new BigDecimal(Math.sin(multiplicand * xiNorthDouble));
        coshOfEtaEast = new BigDecimal(Math.cosh(multiplicand * etaEastDouble));

        subtrahend.add(new BigDecimal(beta).multiply(sinOfXiNorth).multiply(coshOfEtaEast));

        multiplicand += 2;

    }

    BigDecimal xiPrime = xiNorth.subtract(subtrahend);

    return xiPrime;

}

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

/**
 * Converts BigDecimal latitude longitude to UTM 
 * /*ww  w.j  av a2 s .  c  o m*/
 * @param latitude
 * @param longitude
 * @param datumName
 * @return UTM
 * @throws java.lang.Exception
 * 
 * 
 */
public static UTM convert(BigDecimal latitude, BigDecimal longitude, String datumName) throws Exception {

    Datum datum = Datum.valueOf(datumName);

    BigDecimal meridianRadius = new BigDecimal(datum.getMeridianRadius());
    BigDecimal eccentricity = new BigDecimal(datum.getEccentricity());

    BigDecimal latitudeRadians = latitude.abs().multiply(new BigDecimal(Math.PI)).divide(new BigDecimal(180.0),
            PRECISION, RoundingMode.HALF_UP);

    int zoneNumber = calcZoneNumber(longitude);

    BigDecimal zoneCentralMeridian = calcZoneCentralMeridian(zoneNumber);

    BigDecimal changeInLongitudeDegree = (longitude.subtract(zoneCentralMeridian)).abs().setScale(PRECISION,
            RoundingMode.HALF_UP);

    BigDecimal changeInLongitudeRadians = (changeInLongitudeDegree.multiply(new BigDecimal(Math.PI)))
            .divide(new BigDecimal(180), PRECISION, RoundingMode.HALF_UP);

    BigDecimal conformalLatitude = calcConformalLatitude(eccentricity, latitudeRadians).setScale(PRECISION,
            RoundingMode.HALF_UP);

    BigDecimal tauPrime = (new BigDecimal(Math.tan(conformalLatitude.doubleValue()))).setScale(PRECISION,
            RoundingMode.HALF_UP);

    BigDecimal xiPrimeNorth = calcXiPrimeNorth(changeInLongitudeRadians, tauPrime).setScale(PRECISION,
            RoundingMode.HALF_UP);

    BigDecimal etaPrimeEast = calcEtaPrimeEast(changeInLongitudeRadians, tauPrime).setScale(PRECISION,
            RoundingMode.HALF_UP);

    double[] alphaSeries = datum.getAlphaSeries();

    BigDecimal xiNorth = calcXiNorth(xiPrimeNorth, etaPrimeEast, alphaSeries).setScale(PRECISION,
            RoundingMode.HALF_UP);

    BigDecimal etaEast = calcEtaEast(xiPrimeNorth, etaPrimeEast, alphaSeries).setScale(PRECISION,
            RoundingMode.HALF_UP);

    BigDecimal easting = calcEasting(meridianRadius, etaEast, longitude, zoneCentralMeridian)
            .setScale(PRECISION, RoundingMode.HALF_UP);
    BigDecimal northing = calcNorthing(meridianRadius, xiNorth, latitude).setScale(PRECISION,
            RoundingMode.HALF_UP);

    char zoneLetter = calcZoneLetter(latitude);
    char hemisphere = calcHemisphere(latitude);

    if (easting.doubleValue() > UTM.MAX_EASTING)
        easting = new BigDecimal(UTM.MAX_EASTING);
    if (easting.doubleValue() < UTM.MIN_EASTING)
        easting = new BigDecimal(UTM.MIN_EASTING);

    if (northing.doubleValue() > UTM.MAX_NORTHING)
        northing = new BigDecimal(UTM.MAX_NORTHING);
    if (northing.doubleValue() < UTM.MIN_NORTHING)
        northing = new BigDecimal(UTM.MIN_NORTHING);

    UTM utm = new UTM(easting.setScale(SCALE, RoundingMode.HALF_UP),
            northing.setScale(SCALE, RoundingMode.HALF_UP), hemisphere, zoneNumber, zoneLetter);

    return utm;
}

From source file:org.cirdles.geoapp.LatLongToUTM.java

public static UTM convert(BigDecimal latitude, BigDecimal longitude, String datumName) {

    DatumEnum datumEnum = DatumEnum.valueOf(datumName);
    BigDecimal flattening3D = new BigDecimal(datumEnum.getFlattening3D());
    BigDecimal meridianRadius = new BigDecimal(datumEnum.getMeridianRadius());
    BigDecimal eccentricity = new BigDecimal(datumEnum.getEccentricity());

    BigDecimal latitudeRadians = latitude.abs().multiply(new BigDecimal(Math.PI)).divide(new BigDecimal(180.0),
            precision, RoundingMode.HALF_UP);

    //System.out.println("Latitude Radians: " + latitudeRadians);

    int zoneNumber = calcZoneNumber(longitude);

    BigDecimal zoneCentralMeridian = calcZoneCentralMeridian(zoneNumber);

    BigDecimal changeInLongitudeDegree = (longitude.subtract(zoneCentralMeridian)).abs().setScale(precision,
            RoundingMode.HALF_UP);
    //System.out.println("Change in Long Degree: " + changeInLongitudeDegree);

    BigDecimal changeInLongitudeRadians = (changeInLongitudeDegree.multiply(new BigDecimal(Math.PI)))
            .divide(new BigDecimal(180), precision, RoundingMode.HALF_UP);
    //System.out.println("Change In Longitude Radians: " + changeInLongitudeRadians);

    BigDecimal conformalLatitude = calcConformalLatitude(eccentricity, latitudeRadians).setScale(precision,
            RoundingMode.HALF_UP);
    //System.out.println("Conformal Latitude: " + conformalLatitude);

    BigDecimal tauPrime = (new BigDecimal(Math.tan(conformalLatitude.doubleValue()))).setScale(precision,
            RoundingMode.HALF_UP);
    //System.out.println("Tau Prime: " + tauPrime);

    BigDecimal xiPrimeNorth = calcXiPrimeNorth(changeInLongitudeRadians, tauPrime).setScale(precision,
            RoundingMode.HALF_UP);
    //System.out.println("xi Prime North: " + xiPrimeNorth);

    BigDecimal etaPrimeEast = calcEtaPrimeEast(changeInLongitudeRadians, tauPrime).setScale(precision,
            RoundingMode.HALF_UP);
    //System.out.println("Eta Prime East: " + etaPrimeEast);

    BigDecimal[] alphaSeries = { KrugerSeries.alpha1(flattening3D).setScale(precision, RoundingMode.HALF_UP),
            KrugerSeries.alpha2(flattening3D.setScale(precision, RoundingMode.HALF_UP)),
            KrugerSeries.alpha3(flattening3D).setScale(precision, RoundingMode.HALF_UP),
            KrugerSeries.alpha4(flattening3D).setScale(precision, RoundingMode.HALF_UP),
            KrugerSeries.alpha5(flattening3D).setScale(precision, RoundingMode.HALF_UP),
            KrugerSeries.alpha6(flattening3D).setScale(precision, RoundingMode.HALF_UP),
            KrugerSeries.alpha7(flattening3D).setScale(precision, RoundingMode.HALF_UP) };

    BigDecimal xiNorth = calcXiNorth(xiPrimeNorth, etaPrimeEast, alphaSeries).setScale(precision,
            RoundingMode.HALF_UP);
    //System.out.println("xi North: " + xiNorth);

    BigDecimal etaEast = calcEtaEast(xiPrimeNorth, etaPrimeEast, alphaSeries).setScale(precision,
            RoundingMode.HALF_UP);
    //System.out.println("Eta East: " + etaEast);

    BigDecimal easting = calcEasting(meridianRadius, etaEast, longitude, zoneCentralMeridian)
            .setScale(precision, RoundingMode.HALF_UP);
    BigDecimal northing = calcNorthing(meridianRadius, xiNorth, latitude).setScale(precision,
            RoundingMode.HALF_UP);

    char zoneLetter = calcZoneLetter(latitude);
    char hemisphere = calcHemisphere(latitude);

    return new UTM(easting, northing, hemisphere, zoneNumber, zoneLetter);

}

From source file:eionet.cr.util.Util.java

/**
 * Algorithm calculates the estimated number of hashes.
 *
 * @param minHash/*from   w w w  .j ava2  s .  c o  m*/
 * @param maxHash
 * @return
 */
public static int calculateHashesCount(long minHash, long maxHash) {

    BigDecimal minValue = new BigDecimal(Long.MIN_VALUE);
    BigDecimal maxValue = new BigDecimal(Long.MAX_VALUE);
    BigDecimal lowKey = new BigDecimal(minHash);
    BigDecimal highKey = new BigDecimal(maxHash);
    BigDecimal distance = maxValue.subtract(highKey).add(lowKey).subtract(minValue);
    BigDecimal hitCount = new BigDecimal(2).pow(64).divide(distance, 0, BigDecimal.ROUND_HALF_UP);

    return hitCount.intValue();
}

From source file:org.apache.hive.storage.jdbc.spitter.DecimalIntervalSplitter.java

@Override
public List<MutablePair<String, String>> getIntervals(String lowerBound, String upperBound, int numPartitions,
        TypeInfo typeInfo) {/* w ww  . jav  a  2s .c  o  m*/
    List<MutablePair<String, String>> intervals = new ArrayList<>();
    DecimalTypeInfo decimalTypeInfo = (DecimalTypeInfo) typeInfo;
    int scale = decimalTypeInfo.getScale();
    BigDecimal decimalLower = new BigDecimal(lowerBound);
    BigDecimal decimalUpper = new BigDecimal(upperBound);
    BigDecimal decimalInterval = (decimalUpper.subtract(decimalLower)).divide(new BigDecimal(numPartitions),
            MathContext.DECIMAL64);
    BigDecimal splitDecimalLower, splitDecimalUpper;
    for (int i = 0; i < numPartitions; i++) {
        splitDecimalLower = decimalLower.add(decimalInterval.multiply(new BigDecimal(i))).setScale(scale,
                RoundingMode.HALF_EVEN);
        splitDecimalUpper = decimalLower.add(decimalInterval.multiply(new BigDecimal(i + 1))).setScale(scale,
                RoundingMode.HALF_EVEN);
        if (splitDecimalLower.compareTo(splitDecimalUpper) < 0) {
            intervals.add(new MutablePair<String, String>(splitDecimalLower.toPlainString(),
                    splitDecimalUpper.toPlainString()));
        }
    }
    return intervals;
}

From source file:org.atomspace.ultrahouse3000.translator.Weather2DocumentTranslator.java

@Override
public void process(Exchange exchange) throws Exception {

    JsonObject weather = (JsonObject) new JsonParser().parse(exchange.getIn().getBody(String.class));
    BigDecimal temperatur = weather.get("main").getAsJsonObject().get("temp").getAsBigDecimal();
    temperatur = temperatur.subtract(new BigDecimal("272.15"));
    exchange.getIn().setHeader("temperatur", temperatur);

    super.process(exchange);

}