java.math.MathContext.getPrecision()方法的使用及代码示例

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

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

MathContext.getPrecision介绍

[英]Returns the precision. The precision is the number of digits used for an operation. Results are rounded to this precision. The precision is guaranteed to be non negative. If the precision is zero, then the computations have to be performed exact, results are not rounded in this case.
[中]返回精度。精度是用于操作的位数。结果四舍五入到这个精度。精度保证为非负。如果精度为零,则必须精确执行计算,在这种情况下,结果不会四舍五入。

代码示例

代码示例来源: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: deeplearning4j/nd4j

/**
 * Boost the precision by appending decimal zeros to the value. This returns a value which appears to have
 * a higher precision than the input.
 *
 * @param x  The input value
 * @param mc The requirement on the minimum precision on return.
 * @return The same value as the input but with increased (pseudo) precision.
 */
static public BigDecimal scalePrec(final BigDecimal x, final MathContext mc) {
  final int diffPr = mc.getPrecision() - x.precision();
  if (diffPr > 0) {
    return scalePrec(x, diffPr);
  } else {
    return x;
  }
} /* BigDecimalMath.scalePrec */

代码示例来源: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: deeplearning4j/nd4j

/**
 * The base of the natural logarithm.
 *
 * @param mc the required precision of the result
 * @return exp(1) = 2.71828....
 */
static public BigDecimal exp(final MathContext mc) {
  /* look it up if possible */
  if (mc.getPrecision() < E.precision()) {
    return E.round(mc);
  } else {
    /* Instantiate a 1.0 with the requested pseudo-accuracy
     * and delegate the computation to the public method above.
     */
    BigDecimal uni = scalePrec(BigDecimal.ONE, mc.getPrecision());
    return exp(uni);
  }
} /* BigDecimalMath.exp */

代码示例来源: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

/**
 * Euler’s constant.
 *
 * @param mc The required precision of the result.
 * @return 3.14159...
 */
static public BigDecimal pi(final MathContext mc) {
  /* look it up if possible */
  if (mc.getPrecision() < PI.precision()) {
    return PI.round(mc);
  } else {
    /* Broadhurst \protect\vrule width0pt\protect\href{http://arxiv.org/abs/math/9803067}{arXiv:math/9803067}
     */
    int[] a = {1, 0, 0, -1, -1, -1, 0, 0};
    BigDecimal S = broadhurstBBP(1, 1, a, mc);
    return multiplyRound(S, 8);
  }
} /* BigDecimalMath.pi */

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

if (mc.getPrecision() < GAMMA.precision()) {
  return GAMMA.round(mc);
} else {
  double eps = prec2err(0.577, mc.getPrecision());
  MathContext mcloc = new MathContext(2 + mc.getPrecision());
  BigDecimal resul = BigDecimal.ONE;
  resul = resul.add(log(2, mcloc));

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

mc.getRoundingMode());
  if (Math.log10(Math.abs(integer)) >= mc.getPrecision()) {
    integer /= 10;
    newScale--;
precision = mc.getPrecision();
smallValue = integer;
bitLength = bitLength(integer);

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

int mcPrecision = mc.getPrecision();
int elength = (int)Math.log10(m) + 1;   // decimal digits in 'n'
int oneBitMask; // mask of bits

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

|| (mc.getPrecision() == 0)) {
return subtract(subtrahend).round(mc);
if (mc.getPrecision() < this.approxPrecision()) {
  thisSignum = this.signum();
  if (thisSignum != subtrahend.signum()) {

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

double eps = prec2err(x, mc.getPrecision());
int kmax = (int) (6.6 * mc.getPrecision() / p);

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

|| (mc.getPrecision() == 0)) {
  return add(augend).round(mc);
  return add(augend).round(mc);
if (mc.getPrecision() >= larger.approxPrecision()) {

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

int mcPrecision = mc.getPrecision();
if (approxPrecision() < mcPrecision || mcPrecision == 0) {
  return;

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

long trailingZeros = mc.getPrecision() + 2L
    + divisor.approxPrecision() - approxPrecision();
long diffScale = (long)scale - divisor.scale;
if ((mc.getPrecision() == 0) || (this.isZero())
|| (divisor.isZero())) {
  return this.divide(divisor);

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

int mcPrecision = mc.getPrecision();
int diffPrecision = this.precision() - divisor.precision();
int lastPow = TEN_POW.length - 1;

代码示例来源: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;
  int kmax = (1 + mc.getPrecision()) / 3;
  eps /= kmax;

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

/**
 * The natural logarithm.
 *
 * @param r  The main argument, a strictly positive value.
 * @param mc The requirements on the precision.
 * @return ln(r).
 */
static public BigDecimal log(final Rational r, final MathContext mc) {
  /* the value is undefined if x is negative.
   */
  if (r.compareTo(Rational.ZERO) <= 0) {
    throw new ArithmeticException("Cannot take log of negative " + r.toString());
  } else if (r.compareTo(Rational.ONE) == 0) {
    return BigDecimal.ZERO;
  } else {
    /* log(r+epsr) = log(r)+epsr/r. Convert the precision to an absolute error in the result.
     * eps contains the required absolute error of the result, epsr/r.
     */
    double eps = prec2err(Math.log(r.doubleValue()), mc.getPrecision());
    /* Convert this further into a requirement of the relative precision in r, given that
     * epsr/r is also the relative precision of r. Add one safety digit.
     */
    MathContext mcloc = new MathContext(1 + err2prec(eps));
    final BigDecimal resul = log(r.BigDecimalValue(mcloc));
    return resul.round(mc);
  }
} /* log */

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

return BigDecimal.ZERO;
} else if (n == 2) {
  if (mc.getPrecision() < LOG2.precision()) {
    return LOG2.round(mc);
  } else {
    BigDecimal S = broadhurstBBP(2, 1, a, new MathContext(1 + mc.getPrecision()));
    S = S.multiply(new BigDecimal(8));
    S = sqrt(divideRound(S, 3));
  int kmax = (int) (mc.getPrecision() / 1.87);
  MathContext mcloc = new MathContext(mc.getPrecision() + 1 + (int) (Math.log10(kmax * 0.693 / 1.098)));
  BigDecimal log3 = multiplyRound(log(2, mcloc), 19);
  double eps = prec2err(1.098, mc.getPrecision()) / kmax;
  Rational r = new Rational(7153, 524288);
  Rational pk = new Rational(7153, 524288);
  int kmax = (int) (mc.getPrecision() / 1.33);
  MathContext mcloc = new MathContext(mc.getPrecision() + 1 + (int) (Math.log10(kmax * 0.693 / 1.609)));
  BigDecimal log5 = multiplyRound(log(2, mcloc), 14);
  double eps = prec2err(1.6, mc.getPrecision()) / kmax;
  Rational r = new Rational(759, 16384);
  Rational pk = new Rational(759, 16384);
  int kmax = (int) (mc.getPrecision() / 0.903);
  MathContext mcloc = new MathContext(mc.getPrecision() + 1 + (int) (Math.log10(kmax * 3 * 0.693 / 1.098)));
  BigDecimal log7 = multiplyRound(log(2, mcloc), 3);

代码示例来源:origin: com.google.gwt/gwt-servlet

public static void serialize(SerializationStreamWriter streamWriter,
  MathContext instance) throws SerializationException {
 streamWriter.writeInt(instance.getPrecision());
 streamWriter.writeInt(instance.getRoundingMode().ordinal());
}

相关文章