Ejemplo n.º 1
0
  /**
   * Create a {@link BigFraction} given the numerator and denominator as <code>BigInteger</code>.
   * The {@link BigFraction} is reduced to lowest terms.
   *
   * @param num the numerator, must not be <code>null</code>.
   * @param den the denominator, must not be <code>null</code>.
   * @throws ArithmeticException if the denominator is <code>zero</code>.
   * @throws NullPointerException if the numerator or the denominator is <code>zero</code>.
   */
  public BigFraction(BigInteger num, BigInteger den) {
    if (num == null) {
      throw MathRuntimeException.createNullPointerException("numerator is null");
    }
    if (den == null) {
      throw MathRuntimeException.createNullPointerException("denominator is null");
    }
    if (BigInteger.ZERO.equals(den)) {
      throw MathRuntimeException.createArithmeticException("denominator must be different from 0");
    }
    if (BigInteger.ZERO.equals(num)) {
      numerator = BigInteger.ZERO;
      denominator = BigInteger.ONE;
    } else {

      // reduce numerator and denominator by greatest common denominator
      final BigInteger gcd = num.gcd(den);
      if (BigInteger.ONE.compareTo(gcd) < 0) {
        num = num.divide(gcd);
        den = den.divide(gcd);
      }

      // move sign to numerator
      if (BigInteger.ZERO.compareTo(den) > 0) {
        num = num.negate();
        den = den.negate();
      }

      // store the values in the final fields
      numerator = num;
      denominator = den;
    }
  }
Ejemplo n.º 2
0
  /**
   * Create a {@link BigFraction} given the numerator and denominator as {@code BigInteger}. The
   * {@link BigFraction} is reduced to lowest terms.
   *
   * @param num the numerator, must not be {@code null}.
   * @param den the denominator, must not be {@code null}.
   * @throws ZeroException if the denominator is zero.
   * @throws NullArgumentException if either of the arguments is null
   */
  public BigFraction(BigInteger num, BigInteger den) {
    MathUtils.checkNotNull(num, LocalizedFormats.NUMERATOR);
    MathUtils.checkNotNull(den, LocalizedFormats.DENOMINATOR);
    if (BigInteger.ZERO.equals(den)) {
      throw new ZeroException(LocalizedFormats.ZERO_DENOMINATOR);
    }
    if (BigInteger.ZERO.equals(num)) {
      numerator = BigInteger.ZERO;
      denominator = BigInteger.ONE;
    } else {

      // reduce numerator and denominator by greatest common denominator
      final BigInteger gcd = num.gcd(den);
      if (BigInteger.ONE.compareTo(gcd) < 0) {
        num = num.divide(gcd);
        den = den.divide(gcd);
      }

      // move sign to numerator
      if (BigInteger.ZERO.compareTo(den) > 0) {
        num = num.negate();
        den = den.negate();
      }

      // store the values in the final fields
      numerator = num;
      denominator = den;
    }
  }
Ejemplo n.º 3
0
 /**
  * Returns the absolute value of this {@link BigFraction}.
  *
  * @return the absolute value as a {@link BigFraction}.
  */
 public BigFraction abs() {
   return (BigInteger.ZERO.compareTo(numerator) <= 0) ? this : negate();
 }