Package java.math

Examples of java.math.BigDecimal.precision()


             */
            MathContext nmc = new MathContext(mc.getPrecision() + 2, mc.getRoundingMode()); // more precision...
            for (int i = 1; i < asize; ){
                BigDecimal v=_getArg_(startAt, i++);
                int digits0=Math.abs(r.precision()-r.scale());
                int digits1=Math.abs(v.precision()-v.scale());
                nmc = new MathContext(nmc.getPrecision() + digits0 + digits1, mc.getRoundingMode()); // more precision...
                r = r.divide(v, nmc);
            }
        }
        catch (ArithmeticException ex) {
View Full Code Here


            BigDecimal r;
            do {
                old_r = number.get();
                r = old_r;
                for (int i = 0; i < v.length; i++){
                    int digits0=Math.abs(r.precision()-r.scale());
                    int digits1=Math.abs(v[i].precision()-v[i].scale());
                    nmc = new MathContext(nmc.getPrecision() + digits0 + digits1, mc.getRoundingMode()); // more precision...
                    r = r.divide(v[i],nmc);
                }
            }
View Full Code Here

        BigDecimal bigRight = toBigDecimal(right);
        try {
            return bigLeft.divide(bigRight);
        } catch (ArithmeticException e) {
            // set a DEFAULT precision if otherwise non-terminating
            int precision = Math.max(bigLeft.precision(), bigRight.precision()) + DIVISION_EXTRA_PRECISION;
            BigDecimal result = bigLeft.divide(bigRight, new MathContext(precision));
            int scale = Math.max(Math.max(bigLeft.scale(), bigRight.scale()), DIVISION_MIN_SCALE);
            if (result.scale() > scale) result = result.setScale(scale, BigDecimal.ROUND_HALF_UP);
            return result;
        }
View Full Code Here

            int sign2 = val.isInfinity() ? val.infinitySign : val.value.signum();
            return newInfinity(runtime, sign1 * sign2);
        }

        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

        // TODO: no need to calculate every time.
        if (isZero()) {
            return 0;
        }
        BigDecimal val = value.abs().stripTrailingZeros();
        return val.precision() - val.scale();
    }

    @JRubyMethod(name = "sqrt", required = 1)
    public IRubyObject sqrt(IRubyObject arg) {
        Ruby runtime = getRuntime();
View Full Code Here

    BigDecimal s;

    s = new BigDecimal(m.unscaledValue(), 0);

    // 10**k is the greatest power of 10 <= s, i.e. k = floor(log10(s))
    k = s.precision() - s.scale();

    // (1) s     = m * 10**m.scale()   by def of BigDecimal.unscaledValue()
    // (2) m     = s * 10**(n-k)       by def of s,n,k
    // (3) m     = s * 10**-m.scale()  from 1
    // (4) n - k = -m.scale()          by 2, 3
View Full Code Here

  System.out.println(b2.scale());      // 101
  System.out.println(b2.signum());     // 1

  BigDecimal b3 =
      new BigDecimal("-22222222222222222222222222222222222222222222222222222222222222222222222.33333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333");
  System.out.println(b3.precision())// 172. Number of digits in the "unscaled value". The precision of a zero value is 1.
  System.out.println(b3.scale());      // 101
  System.out.println(b3.signum());     // -1

  // RoundingMode.DOWN always goes towards 0
  // b3.precision() - b3.scale() will be the number of digits in the integer part of the BigDecimal
View Full Code Here

  System.out.println(b3.scale());      // 101
  System.out.println(b3.signum());     // -1

  // RoundingMode.DOWN always goes towards 0
  // b3.precision() - b3.scale() will be the number of digits in the integer part of the BigDecimal
  MathContext mc = new MathContext(b3.precision() - b3.scale(), RoundingMode.DOWN);
  BigDecimal b3ROUNDED = b3.round(mc);
  BigDecimal b3FRACTIONALPART = b3.subtract(b3ROUNDED);
 
  System.out.println(b3ROUNDED);       // -22222222222222222222222222222222222222222222222222222222222222222222222
  System.out.println(b3FRACTIONALPART); // -0.33333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333
View Full Code Here

  //
  // Playing around with BigDecimal
  //
 
  BigDecimal b = BigDecimal.TEN;
  System.out.println(b.precision())// Number of digits in the "unscaled value". The precision of a zero value is 1.
  System.out.println(b.scale());
  System.out.println(b.signum());     // 1
 
  BigDecimal b2 =
      new BigDecimal("22222222222222222222222222222222222222222222222222222222222222222222222.33333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333");
View Full Code Here

  System.out.println(b.scale());
  System.out.println(b.signum());     // 1
 
  BigDecimal b2 =
      new BigDecimal("22222222222222222222222222222222222222222222222222222222222222222222222.33333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333");
  System.out.println(b2.precision())// 172. Number of digits in the "unscaled value". The precision of a zero value is 1.
  System.out.println(b2.scale());      // 101
  System.out.println(b2.signum());     // 1

  BigDecimal b3 =
      new BigDecimal("-22222222222222222222222222222222222222222222222222222222222222222222222.33333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333");
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.