java.math.MathContext类的使用及代码示例

x33g5p2x  于2022-01-24 转载在 其他  
字(11.2k)|赞(0)|评价(0)|浏览(325)

本文整理了Java中java.math.MathContext类的一些代码示例,展示了MathContext类的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。MathContext类的具体详情如下:
包路径:java.math.MathContext
类名称:MathContext

MathContext介绍

[英]Immutable objects describing settings such as rounding mode and digit precision for the numerical operations provided by class BigDecimal.
[中]描述设置的不可变对象,例如BigDecimal类提供的数值运算的舍入模式和数字精度。

代码示例

代码示例来源:origin: prestodb/presto

public static BigDecimal readBigDecimal(DecimalType type, Block block, int position)
{
  BigInteger unscaledValue = type.isShort()
      ? BigInteger.valueOf(type.getLong(block, position))
      : decodeUnscaledValue(type.getSlice(block, position));
  return new BigDecimal(unscaledValue, type.getScale(), new MathContext(type.getPrecision()));
}

代码示例来源:origin: plantuml/plantuml

public BigDecimal eval(List<? extends Number> parameters) {
    assertNotNull(parameters.get(0));
    /*
     * From The Java Programmers Guide To numerical Computing (Ronald Mak, 2003)
     */
    BigDecimal x = (BigDecimal) parameters.get(0);
    if (x.compareTo(BigDecimal.ZERO) == 0) {
      return new BigDecimal(0);
    }
    if (x.signum() < 0) {
      throw new ExpressionException("Argument to SQRT() function must not be negative");
    }
    BigInteger n = x.movePointRight(mc.getPrecision() << 1).toBigInteger();
    int bits = (n.bitLength() + 1) >> 1;
    BigInteger ix = n.shiftRight(bits);
    BigInteger ixPrev;
    BigInteger test;
    do {
      ixPrev = ix;
      ix = ix.add(n.divide(ix)).shiftRight(1);
      // Give other threads a chance to work;
      Thread.yield();
      test = ix.subtract(ixPrev).abs();
    } while (test.compareTo(BigInteger.ZERO) != 0 && test.compareTo(BigInteger.ONE) != 0);
    return new BigDecimal(ix, mc.getPrecision());
  }
});

代码示例来源:origin: robovm/robovm

int mcPrecision = mc.getPrecision();
if (approxPrecision() < mcPrecision || mcPrecision == 0) {
  return;
int discardedPrecision = precision() - mcPrecision;
  smallRound(mc, discardedPrecision);
  return;
BigInteger[] integerAndFraction = getUnscaledValue().divideAndRemainder(sizeOfFraction);
long newScale = (long)scale - discardedPrecision;
int compRem;
BigDecimal tempBD;
if (integerAndFraction[1].signum() != 0) {
  compRem = (integerAndFraction[1].abs().shiftLeftOneBit().compareTo(sizeOfFraction));
  compRem =  roundingBehavior( integerAndFraction[0].testBit(0) ? 1 : 0,
      integerAndFraction[1].signum() * (5 + compRem),
      mc.getRoundingMode());
  if (compRem != 0) {
    integerAndFraction[0] = integerAndFraction[0].add(BigInteger.valueOf(compRem));

代码示例来源:origin: plantuml/plantuml

/**
 * Sets the rounding mode for expression evaluation.
 * 
 * @param roundingMode
 *            The new rounding mode.
 * @return The expression, allows to chain methods.
 */
public Expression setRoundingMode(RoundingMode roundingMode) {
  this.mc = new MathContext(mc.getPrecision(), roundingMode);
  return this;
}

代码示例来源:origin: robovm/robovm

/**
 * Returns true if x is a {@code MathContext} with the same precision
 * setting and the same rounding mode as this {@code MathContext} instance.
 *
 * @param x
 *            object to be compared.
 * @return {@code true} if this {@code MathContext} instance is equal to the
 *         {@code x} argument; {@code false} otherwise.
 */
@Override
public boolean equals(Object x) {
  return ((x instanceof MathContext)
      && (((MathContext) x).getPrecision() == precision) && (((MathContext) x)
      .getRoundingMode() == roundingMode));
}

代码示例来源:origin: robovm/robovm

compRem = longCompareTo(Math.abs(fraction) * 2, sizeOfFraction);
  integer += roundingBehavior( ((int)integer) & 1,
      Long.signum(fraction) * (5 + compRem),
      mc.getRoundingMode());
  if (Math.log10(Math.abs(integer)) >= mc.getPrecision()) {
    integer /= 10;
    newScale--;
scale = safeLongToInt(newScale);
precision = mc.getPrecision();
smallValue = integer;
bitLength = bitLength(integer);

代码示例来源:origin: deeplearning4j/nd4j

if (x.compareTo(BigDecimal.ZERO) < 0) {
  return sinh(x.negate()).negate();
} else if (x.compareTo(BigDecimal.ZERO) == 0) {
  return BigDecimal.ZERO;
    double eps = Math.tanh(x.doubleValue());
    MathContext mc = new MathContext(err2prec(0.5 * x.ulp().doubleValue() / eps));
    int k = (int) (x.precision() / Math.log10(1.0 / xhighpr.doubleValue())) / 2;
    MathContext mcTay = new MathContext(err2prec(x.doubleValue(), xUlpDbl / k));
      ifac = ifac.multiply(BigInteger.valueOf(2 * i));
      ifac = ifac.multiply(BigInteger.valueOf(2 * i + 1));
      xpowi = xpowi.multiply(xhighpr).multiply(xhighpr);
      BigDecimal corr = xpowi.divide(new BigDecimal(ifac), mcTay);
    MathContext mc = new MathContext(x.precision());

代码示例来源:origin: stackoverflow.com

if(denominator == null)
 throw new IllegalArgumentException("Denominator is null");
if(denominator.equals(BigInteger.ZERO))
 throw new ArithmeticException("Divide by zero.");
if(denominator.signum() < 0)
 numerator = numerator.negate();
 denominator = denominator.negate();
this(d.scale() < 0 ? d.unscaledValue().multiply(BigInteger.TEN.pow(-d.scale())) : d.unscaledValue(),
   d.scale() < 0 ? BigInteger.ONE                                             : BigInteger.TEN.pow(d.scale()));
return new BigDecimal(numerator).divide(new BigDecimal(denominator), new MathContext(precision, RoundingMode.HALF_EVEN));

代码示例来源:origin: deeplearning4j/nd4j

if (x.compareTo(BigDecimal.ONE) > 0 || x.compareTo(BigDecimal.ONE.negate()) < 0) {
  throw new ArithmeticException("Out of range argument " + x.toString() + " of asin");
  double errpi = Math.sqrt(x.ulp().doubleValue());
  MathContext mc = new MathContext(err2prec(3.14159, errpi));
    ifacN = ifacN.multiply(BigInteger.valueOf(2 * i - 1));
    ifacD = ifacD.multiply(BigInteger.valueOf(i));
  xpowi = sqrt(xhighpr.multiply(new BigDecimal(2)));
  resul = multiplyRound(xpowi, resul);
  MathContext mc = new MathContext(resul.precision());
  BigDecimal pihalf = pi(mc).divide(new BigDecimal(2));
  mc = new MathContext(err2prec(resul.doubleValue(), eps));
  MathContext mc = new MathContext(err2prec(resul.doubleValue(), eps));

代码示例来源:origin: com.dell.doradus/doradus-server

@Override
Object getResult() {
  BigInteger result = m_value.divide(BigInteger.valueOf(m_count));
  int digits = result.abs().toString().length();  // Digits in integral part
  MathContext mc = new MathContext(digits + 3);	// Plus 3 digits for fractional part
  return new BigDecimal(m_value).divide(new BigDecimal(m_count), mc);
}
@Override

代码示例来源:origin: robovm/robovm

if ((subtrahend.isZero()) || (this.isZero())
    || (mc.getPrecision() == 0)) {
  return subtract(subtrahend).round(mc);
if (subtrahend.approxPrecision() < diffScale - 1) {
  if (mc.getPrecision() < this.approxPrecision()) {
    thisSignum = this.signum();
    if (thisSignum != subtrahend.signum()) {
      tempBI = Multiplication.multiplyByPositiveInt(this.getUnscaledValue(), 10)
      .add(BigInteger.valueOf(thisSignum));
    } else {
      tempBI = this.getUnscaledValue().subtract(BigInteger.valueOf(thisSignum));
      tempBI = Multiplication.multiplyByPositiveInt(tempBI, 10)
      .add(BigInteger.valueOf(thisSignum * 9));

代码示例来源:origin: robovm/robovm

long trailingZeros = mc.getPrecision() + 2L
    + divisor.approxPrecision() - approxPrecision();
long diffScale = (long)scale - divisor.scale;
long newScale = diffScale; // scale of the final quotient
int lastPow = TEN_POW.length - 1; // last power of ten
BigInteger quotAndRem[] = {getUnscaledValue()};
if ((mc.getPrecision() == 0) || (this.isZero())
|| (divisor.isZero())) {
  return this.divide(divisor);
  quotAndRem[0] = getUnscaledValue().multiply( Multiplication.powerOf10(trailingZeros) );
  newScale += trailingZeros;
quotAndRem = quotAndRem[0].divideAndRemainder( divisor.getUnscaledValue() );
integerQuot = quotAndRem[0];
if (quotAndRem[1].signum() != 0) {
  compRem = quotAndRem[1].shiftLeftOneBit().compareTo( divisor.getUnscaledValue() );

代码示例来源:origin: deeplearning4j/nd4j

MathContext mcpi = new MathContext(mc.getPrecision() + (int) (Math.log10(10.0 * n)));
final BigDecimal piton = pi(mcpi).pow(n, mc);
BigDecimal S51 = broadhurstBBP(5, 1, a51, new MathContext(2 + mc.getPrecision()));
BigDecimal S53 = broadhurstBBP(5, 3, a53, new MathContext(2 + mc.getPrecision()));
BigDecimal S55 = broadhurstBBP(5, 5, a55, new MathContext(1 + mc.getPrecision()));
S51 = S51.multiply(new BigDecimal(18432));
S53 = S53.multiply(new BigDecimal(14336));
MathContext mcloc = new MathContext(2 + mc.getPrecision() + (int) (Math.log10((double) (n))));
BigDecimal ftrm = pi(mcloc).multiply(new BigDecimal(2));
ftrm = ftrm.pow(n);
double eps = Math.pow(10., -mc.getPrecision());
  int kmax = mc.getPrecision() / 3;
  eps /= kmax;
  BigDecimal exp2p = pi(new MathContext(3 + err2prec(3.14, eps / 0.0075)));
  exp2p = exp(exp2p.multiply(new BigDecimal(2)));
  BigDecimal c = exp2p.subtract(BigDecimal.ONE);
  int kmax = (1 + mc.getPrecision()) / 3;
  eps /= kmax;
  BigDecimal twop = pi(new MathContext(3 + err2prec(3.14, eps / 0.017)));
  twop = twop.multiply(new BigDecimal(2));
  BigDecimal exp2p = exp(twop);

代码示例来源:origin: deeplearning4j/nd4j

/**
 * Return a string in floating point format.
 *
 * @param digits The precision (number of digits)
 * @return The human-readable version in base 10.
 */
public String toFString(int digits) {
  if (b.compareTo(BigInteger.ONE) != 0) {
    MathContext mc = new MathContext(digits, RoundingMode.DOWN);
    BigDecimal f = (new BigDecimal(a)).divide(new BigDecimal(b), mc);
    return (f.toString());
  } else {
    return a.toString();
  }
} /* Rational.toFString */

代码示例来源:origin: deeplearning4j/nd4j

if (x.compareTo(BigDecimal.ZERO) == 0) {
  throw new ArithmeticException("Cannot take cot of zero " + x.toString());
} else if (x.compareTo(BigDecimal.ZERO) < 0) {
  return cot(x.negate()).negate();
} else {
  final BigDecimal xhighpr = scalePrec(res, 2);
  final BigDecimal xhighprSq = multiplyRound(xhighpr, xhighpr);
  MathContext mc = new MathContext(err2prec(xhighpr.doubleValue(), eps));
  BigDecimal resul = BigDecimal.ONE.divide(xhighpr, mc);
  Bernoulli b = new Bernoulli();
  BigInteger fourn = BigInteger.valueOf(4);
    fac = fac.multiply(BigInteger.valueOf(2 * i)).multiply(BigInteger.valueOf(2 * i - 1));
    f = f.multiply(fourn).divide(fac);
    BigDecimal c = multiplyRound(xpowi, f);
    xpowi = multiplyRound(xpowi, xhighprSq);
  mc = new MathContext(err2prec(resul.doubleValue(), eps));
  return resul.round(mc);

代码示例来源:origin: prestodb/presto

@Override
  protected Number getNormalizedValue()
  {
    if (value.isNaN() || value.isInfinite()) {
      return value;
    }
    return new BigDecimal(getValue().doubleValue()).round(new MathContext(precision)).doubleValue();
  }
}

代码示例来源:origin: plantuml/plantuml

public BigDecimal eval(Number vv1, Number vv2) {
    BigDecimal v1 = (BigDecimal) vv1;
    BigDecimal v2 = (BigDecimal) vv2;
    assertNotNull(v1, v2);
    /*- 
     * Thanks to Gene Marin:
     * http://stackoverflow.com/questions/3579779/how-to-do-a-fractional-power-on-bigdecimal-in-java
     */
    int signOf2 = ((BigDecimal) v2).signum();
    double dn1 = v1.doubleValue();
    v2 = v2.multiply(new BigDecimal(signOf2)); // n2 is now positive
    BigDecimal remainderOf2 = v2.remainder(BigDecimal.ONE);
    BigDecimal n2IntPart = v2.subtract(remainderOf2);
    BigDecimal intPow = v1.pow(n2IntPart.intValueExact(), mc);
    BigDecimal doublePow = new BigDecimal(Math.pow(dn1, remainderOf2.doubleValue()));
    BigDecimal result = intPow.multiply(doublePow, mc);
    if (signOf2 == -1) {
      result = BigDecimal.ONE.divide(result, mc.getPrecision(), RoundingMode.HALF_UP);
    }
    return result;
  }
});

代码示例来源:origin: deeplearning4j/nd4j

/**
 * Divide and round.
 *
 * @param x The numerator
 * @param n The denominator
 * @return the divided x/n
 */
static public BigDecimal divideRound(final BigDecimal x, final BigInteger n) {
  /* The estimation of the relative error in the result is |err(x)/x|
   */
  MathContext mc = new MathContext(x.precision());
  return x.divide(new BigDecimal(n), mc);
}

代码示例来源:origin: deeplearning4j/nd4j

if (x.compareTo(BigDecimal.ZERO) == 0) {
  return BigDecimal.ZERO;
} else if (x.compareTo(BigDecimal.ZERO) < 0) {
  return tan(x.negate()).negate();
} else {
    MathContext mc = new MathContext(err2prec(1. / co.doubleValue(), eps));
    return BigDecimal.ONE.divide(co, mc);
  } else {
    Bernoulli b = new Bernoulli();
    BigInteger fourn = BigInteger.valueOf(4);
    BigInteger fac = BigInteger.valueOf(2);
    for (int i = 2;; i++) {
      Rational f = b.at(2 * i).abs();
      fourn = fourn.shiftLeft(2);
      fac = fac.multiply(BigInteger.valueOf(2 * i)).multiply(BigInteger.valueOf(2 * i - 1));
      f = f.multiply(fourn).multiply(fourn.subtract(BigInteger.ONE)).divide(fac);
    MathContext mc = new MathContext(err2prec(result.doubleValue(), eps));
    return result.round(mc);

代码示例来源:origin: prestodb/presto

public BigDecimal toBigDecimal()
{
  return new BigDecimal(unscaledValue, scale, new MathContext(precision));
}

相关文章