Exemplo n.º 1
0
  public static ArrayList<BigDecimal> factors(BigDecimal query) {
    if (query.equals(new BigDecimal("0"))) {
      return null;
    }

    ArrayList<BigDecimal> factors = new ArrayList<BigDecimal>();

    BigDecimal sqrt = sqrt(query);

    if (sqrt.remainder(new BigDecimal("1")).equals(new BigDecimal("0"))) {
      factors.add(sqrt);
    }

    for (float i = 1; i < sqrt.floatValue(); i++) {
      // simplifying assumption
      if (factors.size() > 2) {
        break;
      }

      String m = Float.toString(i);
      BigDecimal otherFactor = query.divide(new BigDecimal(m), MathContext.DECIMAL128);

      if (query
              .divide(new BigDecimal(m), MathContext.DECIMAL128)
              .remainder(new BigDecimal(1))
              .floatValue()
          == 0) {
        factors.add(new BigDecimal(m));
        factors.add(query.divide(new BigDecimal(m)));
      }
    }

    return factors;
  }
Exemplo n.º 2
0
 public static Comparable<?> remainder(Comparable<?> param1, Comparable<?> param2)
     throws ParseException {
   if (param1 == null || param2 == null) {
     return null;
   }
   if (param1 instanceof String) {
     param1 = parse((String) param1);
   }
   if (param2 instanceof String) {
     param2 = parse((String) param2);
   }
   if (param1 instanceof Number && param2 instanceof Number) {
     // BigInteger type is not supported
     if (param1 instanceof BigDecimal || param2 instanceof BigDecimal) {
       BigDecimal b1 = getBigDecimal((Number) param1);
       BigDecimal b2 = getBigDecimal((Number) param2);
       return b1.remainder(b2);
     }
     if (param1 instanceof Double
         || param2 instanceof Double
         || param1 instanceof Float
         || param2 instanceof Float) {
       return ((Number) param1).doubleValue() % ((Number) param2).doubleValue();
     } else { // Long, Integer, Short, Byte
       long l1 = ((Number) param1).longValue();
       long l2 = ((Number) param2).longValue();
       return l1 % l2;
     }
   } else {
     throw new ParseException(
         WRONG_TYPE + "  (" + param1.getClass() + "%" + param2.getClass() + ")");
   }
 }
Exemplo n.º 3
0
 /**
  * 数值求余处理
  *
  * @param num1 被除数
  * @param num2 除数
  * @return 余数
  */
 public static BigDecimal remainder(BigDecimal num1, BigDecimal num2) {
   if (null == num2 || BigDecimal.ZERO.compareTo(num2) == 0) {
     return null;
   }
   if (null == num1 || BigDecimal.ZERO.compareTo(num1) == 0) {
     return BigDecimal.ZERO;
   }
   return num1.remainder(num2);
 }
Exemplo n.º 4
0
 public static double calcIncrement(double value) {
   BigDecimal b = BigDecimal.valueOf(value);
   BigDecimal r = b.remainder(BigDecimal.ONE);
   if (r.compareTo(BigDecimal.ZERO) == 0) {
     return 1;
   }
   double res = 1 / Math.pow(10, r.scale());
   return res;
 }
Exemplo n.º 5
0
  /** @param args fala k da jedan od zadataka ne ubija racunar */
  public static void main(String[] args) {

    int count = 0;
    BigDecimal num = new BigDecimal(Long.MAX_VALUE).add(BigDecimal.ONE);
    while (count < 10) {

      if (num.remainder(new BigDecimal(5)).equals(BigDecimal.ZERO)
          || num.remainder(new BigDecimal(6))
              .equals(
                  BigDecimal
                      .ZERO)) { // ako equals(bigDecimal.ZERO) vraca true, broje je djeljiv sa 6,
                                // ili gore sa 5
        count++;
        System.out.println(count + ": " + num);
      }
      num = num.add(BigDecimal.ONE);
    }
  }
Exemplo n.º 6
0
  /**
   * Updates tick label for log scale.
   *
   * @param length the length of axis
   */
  private void updateTickLabelForLogScale(int length) {
    double min = axis.getRange().lower;
    double max = axis.getRange().upper;

    int digitMin = (int) Math.ceil(Math.log10(min));
    int digitMax = (int) Math.ceil(Math.log10(max));

    final BigDecimal MIN = new BigDecimal(new Double(min).toString());
    BigDecimal tickStep = pow(10, digitMin - 1);
    BigDecimal firstPosition;

    if (MIN.remainder(tickStep).doubleValue() <= 0) {
      firstPosition = MIN.subtract(MIN.remainder(tickStep));
    } else {
      firstPosition = MIN.subtract(MIN.remainder(tickStep)).add(tickStep);
    }

    for (int i = digitMin; i <= digitMax; i++) {
      for (BigDecimal j = firstPosition;
          j.doubleValue() <= pow(10, i).doubleValue();
          j = j.add(tickStep)) {
        if (j.doubleValue() > max) {
          break;
        }

        if (axis.isDateEnabled()) {
          Date date = new Date((long) j.doubleValue());
          tickLabels.add(format(date));
        } else {
          tickLabels.add(format(j.doubleValue()));
        }
        tickLabelValues.add(j.doubleValue());

        int tickLabelPosition =
            (int)
                ((Math.log10(j.doubleValue()) - Math.log10(min))
                    / (Math.log10(max) - Math.log10(min))
                    * length);
        tickLabelPositions.add(tickLabelPosition);
      }
      tickStep = tickStep.multiply(pow(10, 1));
      firstPosition = tickStep.add(pow(10, i));
    }
  }
Exemplo n.º 7
0
  /**
   * Adds the specified dayTime duration.
   *
   * @param add value to be added
   */
  private void add(final BigDecimal add) {
    // normalized modulo: sc % 60  vs.  (-sc + sc % 60 + 60 + sc) % 60
    final BigDecimal sc = sec().add(add);
    sec =
        sc.signum() >= 0
            ? sc.remainder(BD60)
            : sc.negate().add(sc.remainder(BD60)).add(BD60).add(sc).remainder(BD60);

    final long mn = Math.max(min(), 0) + div(sc.longValue(), 60);
    min = (byte) mod(mn, 60);
    final long ho = Math.max(hou, 0) + div(mn, 60);
    hou = (byte) mod(ho, 24);
    final long da = div(ho, 24);

    final long[] ymd = ymd(days().add(BigDecimal.valueOf(da)));
    yea = ymd[0];
    mon = (byte) ymd[1];
    day = (byte) ymd[2];
  }
  public static void main(String[] args) throws IOException {
    br = new BufferedReader(new InputStreamReader(System.in));
    out = new PrintWriter(new OutputStreamWriter(System.out));
    // br = new BufferedReader(new FileReader("in.txt"));
    // out = new PrintWriter(new FileWriter("out.txt"));

    int n = readInt();
    BigDecimal bd = new BigDecimal("0").setScale(15, BigDecimal.ROUND_HALF_UP);
    for (int i = 0; i < n; i++) bd = bd.add(new BigDecimal(next()));
    out.println(bd.remainder(new BigDecimal("360")).toString());
    out.close();
  }
Exemplo n.º 9
0
 // ======================================================================
 // [FUNC] Converts BigDecimal to a radix (No shit!)
 private static String dec2rad(String str, BigDecimal rad) {
   BigDecimal bd = new BigDecimal(str);
   String ret = "";
   int rem = 0;
   bd = bd.setScale(0, BigDecimal.ROUND_DOWN);
   while (bd.compareTo(BigDecimal.ZERO) > 0) {
     rem = bd.remainder(rad).intValue();
     if (rem >= 0 && rem <= 9) ret = (char) (rem + '0') + ret;
     else if (rem >= 10) ret = (char) (rem + 'A' - 10) + ret;
     bd = bd.divide(rad, 0, BigDecimal.ROUND_DOWN);
   }
   return ret == "" ? "0" : ret;
 }
Exemplo n.º 10
0
  public static Timestamp decimalToTimestamp(HiveDecimal d) {
    BigDecimal nanoInstant = d.bigDecimalValue().multiply(BILLION_BIG_DECIMAL);
    int nanos = nanoInstant.remainder(BILLION_BIG_DECIMAL).intValue();
    if (nanos < 0) {
      nanos += 1000000000;
    }
    long seconds =
        nanoInstant.subtract(new BigDecimal(nanos)).divide(BILLION_BIG_DECIMAL).longValue();
    Timestamp t = new Timestamp(seconds * 1000);
    t.setNanos(nanos);

    return t;
  }
Exemplo n.º 11
0
 public BigDecimal roundOffToNearestEven(final BigDecimal amount) {
   BigDecimal roundedAmt;
   final BigDecimal remainder = amount.remainder(new BigDecimal(2));
   /*
    * if reminder is less than 1, subtract reminder amount from passed
    * amount else reminder is greater than 1, subtract reminder amount from
    * passed amount and add 5 to result amount
    */
   if (remainder.compareTo(new BigDecimal("1")) == 1)
     roundedAmt = amount.subtract(remainder).add(new BigDecimal(2));
   else roundedAmt = amount.subtract(remainder);
   return roundedAmt;
 }
Exemplo n.º 12
0
  /**
   * Updates tick label for normal scale.
   *
   * @param length axis length (>0)
   * @param tickStep the tick step
   */
  private void updateTickLabelForLinearScale(int length, BigDecimal tickStep) {
    double min = axis.getRange().lower;
    double max = axis.getRange().upper;

    final BigDecimal MIN = new BigDecimal(new Double(min).toString());
    BigDecimal firstPosition;

    /* if (min % tickStep <= 0) */
    if (MIN.remainder(tickStep).doubleValue() <= 0) {
      /* firstPosition = min - min % tickStep */
      firstPosition = MIN.subtract(MIN.remainder(tickStep));
    } else {
      /* firstPosition = min - min % tickStep + tickStep */
      firstPosition = MIN.subtract(MIN.remainder(tickStep)).add(tickStep);
    }

    // the unit time starts from 1:00
    if (axis.isDateEnabled()) {
      BigDecimal zeroOclock =
          firstPosition.subtract(new BigDecimal(new Double(3600000).toString()));
      if (MIN.compareTo(zeroOclock) == -1) {
        firstPosition = zeroOclock;
      }
    }

    for (BigDecimal b = firstPosition; b.doubleValue() <= max; b = b.add(tickStep)) {
      if (axis.isDateEnabled()) {
        Date date = new Date((long) b.doubleValue());
        tickLabels.add(format(date));
      } else {
        tickLabels.add(format(b.doubleValue()));
      }
      tickLabelValues.add(b.doubleValue());

      int tickLabelPosition = (int) ((b.doubleValue() - min) / (max - min) * length);
      tickLabelPositions.add(tickLabelPosition);
    }
  }
Exemplo n.º 13
0
  private static Object doBigDecimalArithmetic(
      final BigDecimal val1,
      final int operation,
      final BigDecimal val2,
      boolean iNumber,
      int returnTarget) {
    switch (operation) {
      case ADD:
        if (iNumber) {
          return narrowType(val1.add(val2, MATH_CONTEXT), returnTarget);
        } else {
          return val1.add(val2, MATH_CONTEXT);
        }
      case DIV:
        if (iNumber) {
          return narrowType(val1.divide(val2, MATH_CONTEXT), returnTarget);
        } else {
          return val1.divide(val2, MATH_CONTEXT);
        }

      case SUB:
        if (iNumber) {
          return narrowType(val1.subtract(val2, MATH_CONTEXT), returnTarget);
        } else {
          return val1.subtract(val2, MATH_CONTEXT);
        }
      case MULT:
        if (iNumber) {
          return narrowType(val1.multiply(val2, MATH_CONTEXT), returnTarget);
        } else {
          return val1.multiply(val2, MATH_CONTEXT);
        }

      case POWER:
        if (iNumber) {
          return narrowType(val1.pow(val2.intValue(), MATH_CONTEXT), returnTarget);
        } else {
          return val1.pow(val2.intValue(), MATH_CONTEXT);
        }

      case MOD:
        if (iNumber) {
          return narrowType(val1.remainder(val2), returnTarget);
        } else {
          return val1.remainder(val2);
        }

      case GTHAN:
        return val1.compareTo(val2) == 1 ? Boolean.TRUE : Boolean.FALSE;
      case GETHAN:
        return val1.compareTo(val2) >= 0 ? Boolean.TRUE : Boolean.FALSE;
      case LTHAN:
        return val1.compareTo(val2) == -1 ? Boolean.TRUE : Boolean.FALSE;
      case LETHAN:
        return val1.compareTo(val2) <= 0 ? Boolean.TRUE : Boolean.FALSE;
      case EQUAL:
        return val1.compareTo(val2) == 0 ? Boolean.TRUE : Boolean.FALSE;
      case NEQUAL:
        return val1.compareTo(val2) != 0 ? Boolean.TRUE : Boolean.FALSE;
    }
    return null;
  }
Exemplo n.º 14
0
 public static Number bigDecimalRemainder(BigDecimal x, BigDecimal y) {
   return x.remainder(y);
 }
Exemplo n.º 15
0
 @Override
 public TimeStamp unmarshal(BigDecimal v) throws Exception {
   return TimeStamp.time(
       v.longValue(), v.remainder(new BigDecimal(1)).scaleByPowerOfTen(9).intValue());
 }
Exemplo n.º 16
0
  /**
   * Type converts values to other classes. For example an Integer can be converted to a Long.
   *
   * @param <T> Data Type.
   * @param value The value to be converted.
   * @param to The class to be converted to. Must be one of the Java types corresponding to the
   *     DataTypes.
   * @return The converted value.
   * @throws TypeMismatchException Thrown desired class is incompatible with the source class.
   * @throws OverflowException Thrown only on narrowing value conversions, e.g from a BigInteger to
   *     an Integer.
   */
  @Nullable
  public static <T> T convert(Object value, Class<?> to)
      throws TypeMismatchException, OverflowException {
    final Object result;

    if (value == null || value.getClass() == to) {
      result = value;
    } else {
      final Class<?> from = value.getClass();
      try {
        if (from == Integer.class) { // Integer -> ...
          if (to == Long.class) {
            result = new Long(((Number) value).longValue());
          } else if (to == BigInteger.class) {
            result = BigInteger.valueOf(((Number) value).intValue());
          } else if (to == Float.class) {
            result = new Float(((Number) value).floatValue());
          } else if (to == Double.class) {
            result = new Double(((Number) value).doubleValue());
          } else if (to == BigDecimal.class) {
            // Use intValue() to avoid precision errors
            result = new BigDecimal(((Number) value).intValue());
          } else {
            throw new TypeMismatchException(value.getClass(), to);
          }

        } else if (from == Long.class) { // Long -> ...
          if (to == Integer.class) {
            final long l = ((Long) value).longValue();
            if (l < Integer.MIN_VALUE || l > Integer.MAX_VALUE) {
              throw new OverflowException((Number) value, to);
            }
            result = new Integer((int) l);
          } else if (to == BigInteger.class) {
            result = BigInteger.valueOf(((Number) value).longValue());
          } else if (to == Float.class) {
            result = new Float(((Number) value).floatValue());
          } else if (to == Double.class) {
            result = new Double(((Number) value).doubleValue());
          } else if (to == BigDecimal.class) {
            // Use longValue() to avoid precision errors
            result = new BigDecimal(((Number) value).longValue());
          } else {
            throw new TypeMismatchException(value.getClass(), to);
          }

        } else if (from == BigInteger.class) { // BigInteger -> ...
          final BigInteger bi = (BigInteger) value;
          if (to == Integer.class) {
            result = new Integer(bi.intValueExact());
          } else if (to == Long.class) {
            result = new Long(bi.longValueExact());
          } else if (to == Float.class) {
            final float f1 = bi.floatValue();
            if (f1 == Float.NEGATIVE_INFINITY || f1 == Float.POSITIVE_INFINITY) {
              throw new OverflowException(bi, to);
            }
            result = new Float(f1);
          } else if (to == Double.class) {
            final double d = bi.doubleValue();
            if (d == Double.NEGATIVE_INFINITY || d == Double.POSITIVE_INFINITY) {
              throw new OverflowException(bi, to);
            }
            result = new Double(d);
          } else if (to == BigDecimal.class) {
            result = new BigDecimal(bi);
          } else {
            throw new TypeMismatchException(value.getClass(), to);
          }

        } else if (from == Float.class) { // Float -> ...
          final float fl = ((Float) value).floatValue();
          if (to == Integer.class) {
            if (fl < Integer.MIN_VALUE || fl > Integer.MAX_VALUE | fl % 1 > 0) {
              throw new OverflowException((Number) value, to);
            }
            result = new Integer((int) fl);
          } else if (to == Long.class) {
            if (fl < Long.MIN_VALUE || fl > Long.MAX_VALUE | fl % 1 > 0) {
              throw new OverflowException((Number) value, to);
            }
            result = new Long((long) fl);
          } else if (to == BigInteger.class) {
            if (fl % 1 > 0) {
              throw new OverflowException((Number) value, to);
            }
            final BigDecimal bd = BigDecimal.valueOf(fl);
            result = bd.toBigInteger();
          } else if (to == Double.class) {
            result = new Double(((Number) value).doubleValue());
          } else if (to == BigDecimal.class) {
            result = BigDecimal.valueOf(fl);
          } else {
            throw new TypeMismatchException(value.getClass(), to);
          }

        } else if (from == Double.class) { // Double -> ...
          final double d = ((Double) value).doubleValue();
          if (to == Integer.class) {
            if (d < Integer.MIN_VALUE || d > Integer.MAX_VALUE || d % 1 > 0) {
              throw new OverflowException((Number) value, to);
            }
            result = new Integer((int) d);
          } else if (to == Long.class) { // OK
            if (d < Long.MIN_VALUE || d > Long.MAX_VALUE || d % 1 > 0) {
              throw new OverflowException((Number) value, to);
            }
            result = new Long((int) d);
          } else if (to == BigInteger.class) { // OK
            if (d % 1 > 0) {
              throw new OverflowException((Number) value, to);
            }
            final BigDecimal bd = BigDecimal.valueOf(d);
            result = bd.toBigInteger();
          } else if (to == Float.class) { // OK
            if (d < -Float.MAX_VALUE || d > Float.MAX_VALUE) {
              throw new OverflowException((Number) value, to);
            }
            result = new Float((float) d);
          } else if (to == BigDecimal.class) { // OK
            result = BigDecimal.valueOf(d);
          } else {
            throw new TypeMismatchException(value.getClass(), to);
          }

        } else if (from == BigDecimal.class) { // BigDecimal -> ...
          final BigDecimal bd = (BigDecimal) value;
          if (to == Integer.class) { // OK
            result = new Integer(bd.intValueExact());
          } else if (to == Long.class) { // OK
            result = new Long(bd.longValueExact());
          } else if (to == BigInteger.class) { // OK
            // BigDecimal modulus
            final BigDecimal remainder = bd.remainder(BigDecimal.ONE);
            if (!remainder.equals(BigDecimal.ZERO)) {
              throw new OverflowException(bd, to);
            }
            result = bd.toBigInteger();
          } else if (to == Float.class) { // OK
            if (bd.compareTo(BigDecimal_MIN_FLOAT) < 0 || bd.compareTo(BigDecimal_MAX_FLOAT) > 0) {
              throw new OverflowException(bd, to);
            }
            result = new Float(bd.floatValue());
          } else if (to == Double.class) { // OK
            if (bd.compareTo(BigDecimal_MIN_DOUBLE) < 0
                || bd.compareTo(BigDecimal_MAX_DOUBLE) > 0) {
              throw new OverflowException(bd, to);
            }
            result = new Double(bd.doubleValue());
          } else {
            throw new TypeMismatchException(value.getClass(), to);
          }

        } else {
          throw new UnexpectedException("convert: " + from.getName());
        }
      } catch (final ArithmeticException e) {
        // Thrown by intValueExact() etc.
        throw new OverflowException((Number) value, to);
      }
    }

    @SuppressWarnings("unchecked")
    final T t = (T) result;
    return t;
  }