java.math.BigDecimal.ulp()方法的使用及代码示例

x33g5p2x  于2022-01-16 转载在 其他  
字(8.4k)|赞(0)|评价(0)|浏览(253)

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

BigDecimal.ulp介绍

[英]Returns the unit in the last place (ULP) of this BigDecimalinstance. An ULP is the distance to the nearest big decimal with the same precision.

The amount of a rounding error in the evaluation of a floating-point operation is often expressed in ULPs. An error of 1 ULP is often seen as a tolerable error.

For class BigDecimal, the ULP of a number is simply 10-scale. For example, new BigDecimal(0.1).ulp() returns 1E-55.
[中]返回此BigDecimalinstance最后一位(ULP)的单位。ULP是到具有相同精度的最接近的大小数点的距离。
浮点运算计算中的舍入误差量通常用ULP表示。1 ULP的错误通常被视为可容忍的错误。
对于BigDecimal类,一个数字的ULP只是10个刻度。例如,新的BigDecimal(0.1)。ulp()返回1E-55。

代码示例

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

/**
 * Subtract and round according to the larger of the two ulp’s.
 *
 * @param x The left term.
 * @param y The right term.
 * @return The difference x-y.
 */
static public BigDecimal subtractRound(final BigDecimal x, final BigDecimal y) {
  BigDecimal resul = x.subtract(y);
  /* The estimation of the absolute error in the result is |err(y)|+|err(x)|
   */
  double errR = Math.abs(y.ulp().doubleValue() / 2.) + Math.abs(x.ulp().doubleValue() / 2.);
  MathContext mc = new MathContext(err2prec(resul.doubleValue(), errR));
  return resul.round(mc);
} /* subtractRound */

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

/**
 * Add and round according to the larger of the two ulp’s.
 *
 * @param x The left summand
 * @param y The right summand
 * @return The sum x+y.
 */
static public BigDecimal addRound(final BigDecimal x, final BigDecimal y) {
  BigDecimal resul = x.add(y);
  /* The estimation of the absolute error in the result is |err(y)|+|err(x)|
   */
  double errR = Math.abs(y.ulp().doubleValue() / 2.) + Math.abs(x.ulp().doubleValue() / 2.);
  MathContext mc = new MathContext(err2prec(resul.doubleValue(), errR));
  return resul.round(mc);
} /* addRound */

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

/**
 * Power function.
 *
 * @param x Base of the power.
 * @param y Exponent of the power.
 * @return x^y.
 * The estimation of the relative error in the result is |log(x)*err(y)|+|y*err(x)/x|
 */
static public BigDecimal pow(final BigDecimal x, final BigDecimal y) {
  if (x.compareTo(BigDecimal.ZERO) < 0) {
    throw new ArithmeticException("Cannot power negative " + x.toString());
  } else if (x.compareTo(BigDecimal.ZERO) == 0) {
    return BigDecimal.ZERO;
  } else {
    /* return x^y = exp(y*log(x)) ;
     */
    BigDecimal logx = log(x);
    BigDecimal ylogx = y.multiply(logx);
    BigDecimal resul = exp(ylogx);
    /* The estimation of the relative error in the result is |log(x)*err(y)|+|y*err(x)/x|
     */
    double errR = Math.abs(logx.doubleValue() * y.ulp().doubleValue() / 2.)
            + Math.abs(y.doubleValue() * x.ulp().doubleValue() / 2. / x.doubleValue());
    MathContext mcR = new MathContext(err2prec(1.0, errR));
    return resul.round(mcR);
  }
} /* BigDecimalMath.pow */

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

err2pi = 0.25 * Math.abs(x.ulp().doubleValue() / k);
} else {
  err2pi = 0.5 * Math.abs(x.ulp().doubleValue());
mc = new MathContext(err2prec(res.doubleValue(), x.ulp().doubleValue() / 2.));

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

/**
 * The hyperbolic tangent.
 *
 * @param x The argument.
 * @return The tanh(x) = sinh(x)/cosh(x).
 */
static public BigDecimal tanh(final BigDecimal x) {
  if (x.compareTo(BigDecimal.ZERO) < 0) {
    return tanh(x.negate()).negate();
  } else if (x.compareTo(BigDecimal.ZERO) == 0) {
    return BigDecimal.ZERO;
  } else {
    BigDecimal xhighpr = scalePrec(x, 2);
    /* tanh(x) = (1-e^(-2x))/(1+e^(-2x)) .
     */
    BigDecimal exp2x = exp(xhighpr.multiply(new BigDecimal(-2)));
    /* The error in tanh x is err(x)/cosh^2(x).
     */
    double eps = 0.5 * x.ulp().doubleValue() / Math.pow(Math.cosh(x.doubleValue()), 2.0);
    MathContext mc = new MathContext(err2prec(Math.tanh(x.doubleValue()), eps));
    return BigDecimal.ONE.subtract(exp2x).divide(BigDecimal.ONE.add(exp2x), mc);
  }
} /* BigDecimalMath.tanh */

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

errpi = 0.5 * Math.abs(x.ulp().doubleValue() / k);
} else {
  errpi = 0.5 * Math.abs(x.ulp().doubleValue());
mc = new MathContext(err2prec(res.doubleValue(), x.ulp().doubleValue() / 2.));

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

/**
 * The hypotenuse.
 *
 * @param x the first argument.
 * @param y the second argument.
 * @return the square root of the sum of the squares of the two arguments, sqrt(x^2+y^2).
 */
static public BigDecimal hypot(final BigDecimal x, final BigDecimal y) {
  /* compute x^2+y^2
   */
  BigDecimal z = x.pow(2).add(y.pow(2));
  /* truncate to the precision set by x and y. Absolute error = 2*x*xerr+2*y*yerr,
   * where the two errors are 1/2 of the ulp’s. Two intermediate protectio digits.
   */
  BigDecimal zerr = x.abs().multiply(x.ulp()).add(y.abs().multiply(y.ulp()));
  MathContext mc = new MathContext(2 + err2prec(z, zerr));
  /* Pull square root */
  z = sqrt(z.round(mc));
  /* Final rounding. Absolute error in the square root is (y*yerr+x*xerr)/z, where zerr holds 2*(x*xerr+y*yerr).
   */
  mc = new MathContext(err2prec(z.doubleValue(), 0.5 * zerr.doubleValue() / z.doubleValue()));
  return z.round(mc);
} /* BigDecimalMath.hypot */

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

final double eps = x.ulp().doubleValue() / (2 * n * x.doubleValue());
for (;;) {

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

/**
 * The inverse hyperbolic cosine.
 *
 * @param x The argument.
 * @return The arccosh(x) .
 */
static public BigDecimal acosh(final BigDecimal x) {
  if (x.compareTo(BigDecimal.ONE) < 0) {
    throw new ArithmeticException("Out of range argument cosh " + x.toString());
  } else if (x.compareTo(BigDecimal.ONE) == 0) {
    return BigDecimal.ZERO;
  } else {
    BigDecimal xhighpr = scalePrec(x, 2);
    /* arccosh(x) = log(x+sqrt(x^2-1))
     */
    BigDecimal logx = log(sqrt(xhighpr.pow(2).subtract(BigDecimal.ONE)).add(xhighpr));
    /* The absolute error in arcsinh x is err(x)/sqrt(x^2-1)
     */
    double xDbl = x.doubleValue();
    double eps = 0.5 * x.ulp().doubleValue() / Math.sqrt(xDbl * xDbl - 1.);
    MathContext mc = new MathContext(err2prec(logx.doubleValue(), eps));
    return logx.round(mc);
  }
} /* BigDecimalMath.acosh */

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

/**
 * The inverse hyperbolic sine.
 *
 * @param x The argument.
 * @return The arcsinh(x) .
 */
static public BigDecimal asinh(final BigDecimal x) {
  if (x.compareTo(BigDecimal.ZERO) == 0) {
    return BigDecimal.ZERO;
  } else {
    BigDecimal xhighpr = scalePrec(x, 2);
    /* arcsinh(x) = log(x+hypot(1,x))
     */
    BigDecimal logx = log(hypot(1, xhighpr).add(xhighpr));
    /* The absolute error in arcsinh x is err(x)/sqrt(1+x^2)
     */
    double xDbl = x.doubleValue();
    double eps = 0.5 * x.ulp().doubleValue() / Math.hypot(1., xDbl);
    MathContext mc = new MathContext(err2prec(logx.doubleValue(), eps));
    return logx.round(mc);
  }
} /* BigDecimalMath.asinh */

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

/**
 * The hypotenuse.
 *
 * @param n the first argument.
 * @param x the second argument.
 * @return the square root of the sum of the squares of the two arguments, sqrt(n^2+x^2).
 */
static public BigDecimal hypot(final int n, final BigDecimal x) {
  /* compute n^2+x^2 in infinite precision
   */
  BigDecimal z = (new BigDecimal(n)).pow(2).add(x.pow(2));
  /* Truncate to the precision set by x. Absolute error = in z (square of the result) is |2*x*xerr|,
   * where the error is 1/2 of the ulp. Two intermediate protection digits.
   * zerr is a signed value, but used only in conjunction with err2prec(), so this feature does not harm.
   */
  double zerr = x.doubleValue() * x.ulp().doubleValue();
  MathContext mc = new MathContext(2 + err2prec(z.doubleValue(), zerr));
  /* Pull square root */
  z = sqrt(z.round(mc));
  /* Final rounding. Absolute error in the square root is x*xerr/z, where zerr holds 2*x*xerr.
   */
  mc = new MathContext(err2prec(z.doubleValue(), 0.5 * zerr / z.doubleValue()));
  return z.round(mc);
} /* BigDecimalMath.hypot */

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

MathContext mc = new MathContext(err2prec(0.5 * x.ulp().doubleValue() / eps));
double xUlpDbl = x.ulp().doubleValue();

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

double xUlpDbl = 0.5 * x.ulp().doubleValue() * x.doubleValue();

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

double xUlpDbl = x.ulp().doubleValue();

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

double errpi = 0.5 * Math.abs(x.ulp().doubleValue());
int val = 2 + err2prec(FastMath.PI, errpi);
MathContext mc = new MathContext(val);
    double xUlpDbl = res.ulp().doubleValue();

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

double eps = 0.5 * x.ulp().doubleValue() / Math.abs(x.doubleValue());
  BigDecimal resul = z;
  for (int k = 2;; k++) {
} else {
  final double xDbl = x.doubleValue();
  final double xUlpDbl = x.ulp().doubleValue();

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

final double xUlpDbl = x.ulp().doubleValue() / 2.;
final double eps = xUlpDbl / 2. / Math.pow(Math.sin(xDbl), 2.);
final BigDecimal xhighpr = scalePrec(res, 2);

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

double eps = x.ulp().doubleValue() / x.doubleValue();
BigDecimal resul = log(scalePrec(x, 2)).negate();
    MathContext m = new MathContext(err2prec(n * z.ulp().doubleValue() / 2. / z.doubleValue()));
    c = c.round(m);
  psi += zdbl / n / (n + zdbl);
eps = psi * x.ulp().doubleValue() / 2.;
mcloc = new MathContext(err2prec(eps));

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

double eps = x.ulp().doubleValue() / (2.0 * Math.hypot(1.0, x.doubleValue()));
MathContext mc = new MathContext(err2prec(resul.doubleValue(), eps));
double eps = x.ulp().doubleValue() / (2.0 * Math.hypot(1.0, x.doubleValue()));
double eps = x.ulp().doubleValue() / (2.0 * Math.hypot(1.0, x.doubleValue()));

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

final double xUlpDbl = x.ulp().doubleValue() / 2.;
final double eps = xUlpDbl / 2. / Math.pow(Math.cos(xDbl), 2.);
if (xDbl > 0.8) {

相关文章