Examples of MathContext


Examples of com.ibm.icu.math.MathContext

//#endif

    public void testJB4971()
    {
        DecimalFormat decfmt = new DecimalFormat();
        MathContext resultICU;

        MathContext comp1 = new MathContext(0, MathContext.PLAIN);
        resultICU = decfmt.getMathContextICU();
        if ((comp1.getDigits() != resultICU.getDigits()) ||
            (comp1.getForm() != resultICU.getForm()) ||
            (comp1.getLostDigits() != resultICU.getLostDigits()) ||
            (comp1.getRoundingMode() != resultICU.getRoundingMode()))
        {
            errln("ERROR: Math context 1 not equal - result: " + resultICU.toString() +
                " / expected: " + comp1.toString());
        }

        MathContext comp2 = new MathContext(5, MathContext.ENGINEERING);
        decfmt.setMathContextICU(comp2);
        resultICU = decfmt.getMathContextICU();
        if ((comp2.getDigits() != resultICU.getDigits()) ||
            (comp2.getForm() != resultICU.getForm()) ||
            (comp2.getLostDigits() != resultICU.getLostDigits()) ||
            (comp2.getRoundingMode() != resultICU.getRoundingMode()))
        {
            errln("ERROR: Math context 2 not equal - result: " + resultICU.toString() +
                " / expected: " + comp2.toString());
        }

//#if defined(FOUNDATION10) || defined(J2SE13) || defined(J2SE14)
//#else
View Full Code Here

Examples of java.math.MathContext

        nzeros = prec - (origPrec - 1);
    } else {
        compPrec = prec + 1;
    }

    MathContext mc = new MathContext(compPrec);
    BigDecimal v
        = new BigDecimal(value.unscaledValue(), scale, mc);

    BigDecimalLayout bdl
         = new BigDecimalLayout(v.unscaledValue(), v.scale(),
              BigDecimalLayoutForm.SCIENTIFIC);

    char[] mant = bdl.mantissa();

    // Add a decimal point if necessary.  The mantissa may not
    // contain a decimal point if the scale is zero (the internal
    // representation has no fractional part) or the original
    // precision is one. Append a decimal point if '#' is set or if
    // we require zero padding to get to the requested precision.
     if ((origPrec == 1 || !bdl.hasDot())
         && (nzeros > 0 || (f.contains(Flags.ALTERNATE))))
         mant = addDot(mant);

    // Add trailing zeros in the case precision is greater than
    // the number of available digits after the decimal separator.
    mant = trailingZeros(mant, nzeros);

    char[] exp = bdl.exponent();
    int newW = width;
    if (width != -1)
        newW = adjustWidth(width - exp.length - 1, f, neg);
    localizedMagnitude(sb, mant, f, newW, null);

    sb.append(f.contains(Flags.UPPERCASE) ? 'E' : 'e');

    Flags flags = f.dup().remove(Flags.GROUP);
    char sign = exp[0];
    assert(sign == '+' || sign == '-');
    sb.append(exp[0]);

    char[] tmp = new char[exp.length - 1];
    System.arraycopy(exp, 1, tmp, 0, exp.length - 1);
    sb.append(localizedMagnitude(null, tmp, flags, -1, null));
      } else if (c == Conversion.DECIMAL_FLOAT) {
    // Create a new BigDecimal with the desired precision.
    int prec = (precision == -1 ? 6 : precision);
    int scale = value.scale();
                if (scale > prec) {
                    // more "scale" digits than the requested "precision
                    int compPrec = value.precision();
                    if (compPrec <= scale) {
                        // case of 0.xxxxxx
                        value = value.setScale(prec, RoundingMode.HALF_UP);
                    } else {
                        compPrec -= (scale - prec);
                        value = new BigDecimal(value.unscaledValue(),
                                               scale,
                                               new MathContext(compPrec));
                    }
                }
                BigDecimalLayout bdl = new BigDecimalLayout(
                                           value.unscaledValue(),
                                           value.scale(),
View Full Code Here

Examples of java.math.MathContext

   * @see #PROPERTY_MINIMUM_PRECISION
   */
  public BigDecimal divide(BigDecimal dividend, BigDecimal divisor)
  {
    int precision = getDivisionPrecision(dividend, divisor);
    MathContext mathContext = getMathContext(precision);
    return dividend.divide(divisor, mathContext);
  }
View Full Code Here

Examples of java.math.MathContext

     
      mathContexts.set(newContexts);
      contexts = newContexts;
    }
   
    MathContext mathContext = contexts[idx];
    if (mathContext == null)
    {
      mathContext = new MathContext(precision, RoundingMode.HALF_UP);
      contexts[idx] = mathContext;
    }
    return mathContext;
  }
View Full Code Here

Examples of java.math.MathContext

        }

        BigDecimal res = value.multiply(val.value);
        if (res.precision() > digits) {
            // TODO: rounding mode should not be hard-coded. See #mode.
            res = res.round(new MathContext(digits,  RoundingMode.HALF_UP));
        }
        return new RubyBigDecimal(runtime, res).setResult();
    }
View Full Code Here

Examples of java.math.MathContext

            // Note: MRI has a very non-trivial way of calculating the precision,
            // so we use very simple approximation here:
            int precision = (-times + 4) * (getAllDigits().length() + 4);

            return new RubyBigDecimal(getRuntime(),
                    value.pow(times, new MathContext(precision, RoundingMode.HALF_UP)));
        } else {
            return new RubyBigDecimal(getRuntime(), value.pow(times));
        }
    }
View Full Code Here

Examples of java.math.MathContext

        if (res != null) {
            return res;
        }
        RoundingMode roundMode = getRoundingMode(runtime);
        return new RubyBigDecimal(runtime, value.add(
                val.value, new MathContext(prec, roundMode))); // TODO: why this: .setResult();
    }
View Full Code Here

Examples of java.math.MathContext

            return op_quo(context, other);
        } else {
            // TODO: better algorithm to set precision needed
            int prec = Math.max(200, scale);
            return new RubyBigDecimal(getRuntime(),
                    value.divide(val.value, new MathContext(prec, RoundingMode.HALF_UP))).setResult(scale);
        }
    }
View Full Code Here

Examples of java.math.MathContext

        }

        n += 4; // just in case, add a bit of extra precision

        return new RubyBigDecimal(getRuntime(),
                bigSqrt(this.value, new MathContext(n, RoundingMode.HALF_UP))).setResult();
    }
View Full Code Here

Examples of java.math.MathContext

       
        int precision = value.precision() - value.scale() + n;
       
        if (precision > 0) {
            return new RubyBigDecimal(getRuntime(),
                    value.round(new MathContext(precision, RoundingMode.DOWN)));
        } else {
            // TODO: proper sign
            return new RubyBigDecimal(getRuntime(), BigDecimal.ZERO);
        }
    }
View Full Code Here
TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.