Example usage for java.math MathContext UNLIMITED

List of usage examples for java.math MathContext UNLIMITED

Introduction

In this page you can find the example usage for java.math MathContext UNLIMITED.

Prototype

MathContext UNLIMITED

To view the source code for java.math MathContext UNLIMITED.

Click Source Link

Document

A MathContext object whose settings have the values required for unlimited precision arithmetic.

Usage

From source file:com.idylwood.utils.MathUtils.java

static final double multiplyAndSumSlow(final double[] values, final double scale) {
    BigDecimal ret = new BigDecimal(0);
    for (double x : values)
        ret = ret.add(new BigDecimal(x), MathContext.UNLIMITED);
    return ret.multiply(new BigDecimal(scale), MathContext.UNLIMITED).doubleValue();
}

From source file:com.idylwood.utils.MathUtils.java

final public static double meanArbPrec(final double data[]) {
    BigDecimal mean = new BigDecimal(0);
    for (double x : data)
        mean = mean.add(new BigDecimal(x), MathContext.UNLIMITED);
    mean = mean.divide(new BigDecimal(data.length), MathContext.UNLIMITED);
    return mean.doubleValue();
}

From source file:com.idylwood.utils.MathUtils.java

private final static double varianceSlow(final double[] data) {
    BigDecimal mean = new BigDecimal(0);
    for (double x : data)
        mean = mean.add(new BigDecimal(x), MathContext.UNLIMITED);
    mean = mean.divide(new BigDecimal(data.length), MathContext.UNLIMITED);
    //mean = new BigDecimal(mean(data));
    BigDecimal ret = new BigDecimal(0);
    for (double x : data) {
        //BigDecimal summand = ret.add(new BigDecimal(x),MathContext.UNLIMITED);
        BigDecimal summand = new BigDecimal(x).subtract(mean, MathContext.UNLIMITED);
        ret = ret.add(summand.pow(2));//w w w  .j  av  a  2s .  c  o  m
    }
    ret = ret.divide(new BigDecimal(data.length - 1), MathContext.DECIMAL128);
    return ret.doubleValue();
}

From source file:br.com.fidias.chance4j.Chance.java

/**
 * Return a random BigDecimal number./* w w w .  j  a  va 2s  .c o m*/
 * <pre>
 * chance.getBigDecimal(-10, 100, 2);
 * => 45.89
 * </pre>
 *
 * @param min Minimum value to choose from
 * @param max Maximum value to choose from
 * @param fixed Specify a fixed precision
 * @return A random BigDecimal
 * @throws ChanceException
 */
public BigDecimal getBigDecimal(Integer min, Integer max, int fixed) throws ChanceException {
    if (min != null && max != null && max < min) {
        throw new ChanceException("Max must be greater than min.");
    }

    int num;
    int localFixed = (int) Math.pow(10, fixed + FORCE_INCREASE_FIXED);
    int localMax = (int) (Integer.MAX_VALUE / localFixed);
    int localMin = -localMax;
    if (max == null) {
        max = localMax;
    } else if (max > localMax) {
        final String message = "Max specified (%d) is out of range with fixed. " + "Max should be, at most, %d";
        throw new ChanceException(String.format(message, max, localMax));
    }
    if (min == null) {
        min = localMin;
    } else if (min < localMin) {
        final String message = "Min specified (%d) is out of range with fixed. "
                + "Min should be, at least, %d";
        throw new ChanceException(String.format(message, min, localMin));
    }
    num = integer(min * localFixed, max * localFixed);
    BigDecimal bd = new BigDecimal(num).divide(new BigDecimal(localFixed), MathContext.UNLIMITED);
    return bd.setScale(fixed, RoundingMode.UP);
}

From source file:com.liferay.util.FileUtil.java

public static String getsize(File fileName) {
    String finalVal;/*from   w  w  w  .j a v  a  2  s  .  c  o m*/
    long filesize = fileName.length();
    BigDecimal size = new BigDecimal(filesize);
    BigDecimal byteVal = null;
    BigDecimal changedByteVal = null;
    finalVal = "";
    if (filesize <= 0) {
        finalVal = "";
    } else if (filesize < MEGA_BYTE) {
        byteVal = new BigDecimal(KILO_BYTE);
        if (size != null) {
            changedByteVal = size.divide(byteVal, MathContext.UNLIMITED);
            finalVal = Long.toString(Math.round(Math.ceil(changedByteVal.doubleValue()))) + " KB";
        }
    } else if (filesize < GIGA_BYTE) {
        byteVal = new BigDecimal(MEGA_BYTE);
        if (size != null) {
            changedByteVal = size.divide(byteVal, MathContext.UNLIMITED);
            finalVal = Long.toString(Math.round(Math.ceil(changedByteVal.doubleValue()))) + " MB";
        }
    } else if (filesize < TERA_BYTE) {
        byteVal = new BigDecimal(GIGA_BYTE);
        if (size != null) {
            changedByteVal = size.divide(byteVal, MathContext.UNLIMITED);
            finalVal = Long.toString(Math.round(Math.ceil(changedByteVal.doubleValue()))) + " GB";
        }
    } else {
        byteVal = new BigDecimal(TERA_BYTE);
        if (size != null) {
            changedByteVal = size.divide(byteVal, MathContext.UNLIMITED);
            finalVal = Long.toString(Math.round(Math.ceil(changedByteVal.doubleValue()))) + " TB";
        }
    }
    return finalVal;
}

From source file:com.idylwood.utils.MathUtils.java

public static final double sumArbitraryPrecision(final double... values) {
    BigDecimal sum = new BigDecimal(0);
    for (int i = values.length; i-- != 0;)
        sum = sum.add(new BigDecimal(values[i]), MathContext.UNLIMITED);
    return sum.doubleValue();
}

From source file:com.idylwood.utils.MathUtils.java

static final double linearCombinationArbitraryPrecision(final double[] x, final double[] y) {
    final int len = Math.min(x.length, y.length);
    //final double [][] ret = new double[len][2];
    BigDecimal ret = new BigDecimal(0);
    for (int i = len; 0 != i--;) {
        BigDecimal product = new BigDecimal(x[i]).multiply(new BigDecimal(y[i]), MathContext.UNLIMITED);
        ret = ret.add(product, MathContext.UNLIMITED);
    }//from   w w w  .  j  a  v  a  2  s  . co  m
    return ret.doubleValue();
}

From source file:jp.furplag.util.commons.NumberUtils.java

/**
 * {@code (type) (o % divisor)}./*from  w w  w  . jav  a2  s.  co  m*/
 *
 * @param o the object, number or string.
 * @param divisor value by which 'o' is to be divided.
 * @param type return type.
 * @return {@code (type) (o % divisor)}.
 */
public static <T extends Number> T remainder(final Object o, final Number divisor, Class<T> type) {
    if (type == null)
        return null;
    NumberObject nO = NumberObject.of(type);
    Number n = valueOf(o);
    if (n == null)
        return nO.valueOf(null);
    if (divisor == null)
        return nO.valueOf(n);
    if (ClassUtils.isPrimitiveWrapper(n.getClass()) && ClassUtils.isPrimitiveWrapper(divisor.getClass())) {
        return nO.valueOf(valueOf(n, double.class) % valueOf(divisor, double.class));
    }
    if (isInfiniteOrNaN(n) || isInfiniteOrNaN(divisor))
        return nO.valueOf(Double.NaN);

    return nO.valueOf(
            valueOf(n, BigDecimal.class).remainder(valueOf(divisor, BigDecimal.class), MathContext.UNLIMITED));
}

From source file:org.multibit.utils.CSMiscUtils.java

@Deprecated
public static BigInteger calcRawPercentageCharge(CSAsset asset, BigInteger transferAmount) {
    CoinSparkGenesis genesis = asset.getGenesis();
    int chargeBasisPoints = genesis.getChargeBasisPoints();
    if (chargeBasisPoints == 0)
        return BigInteger.ZERO;
    BigDecimal d = new BigDecimal(chargeBasisPoints);
    d = d.movePointLeft(2 + 2); // 2 to get whole percent number, another 2 to get fraction
    d = d.multiply(new BigDecimal(transferAmount), MathContext.UNLIMITED);
    return d.toBigInteger();
}

From source file:org.polymap.kaps.ui.form.NHK2010BewertungFormEditorPage.java

public static void main(String[] args) {
    System.out.println(Math.floor(9.5d + 0.5d));
    System.out.println(Math.floor(9.4d + 0.55d));
    System.out.println(Math.floor(9.49d + 0.55d));
    System.out.println(Math.floor(9.8d + 0.5d));
    System.out.println(Math.round(9.5d));
    System.out.println(Math.round(9.4d));
    System.out.println(Math.round((9.49d * 100) / 100));
    System.out.println(Math.round(9.8d));
    System.out.println(new DecimalFormat("###").format(9.49d));
    BigDecimal bd = new BigDecimal(9.49d);
    bd = bd.round(MathContext.UNLIMITED);
    System.out.println(bd.toString());

}