/** * Creates a rational number of the specified {@link BigDecimal} value. * * @param value the double value * @return the rational number */ public static BigRational valueOf(BigDecimal value) { if (value.compareTo(BigDecimal.ZERO) == 0) { return(ZERO); } if (value.compareTo(BigDecimal.ONE) == 0) { return(ONE); } int scale = value.scale(); if (scale == 0) { return(new BigRational(value, BigDecimal.ONE)); } else if (scale < 0) { BigDecimal n = new BigDecimal(value.unscaledValue()).multiply(BigDecimal.ONE.movePointLeft(value.scale())); return(new BigRational(n, BigDecimal.ONE)); } else { BigDecimal n = new BigDecimal(value.unscaledValue()); BigDecimal d = BigDecimal.ONE.movePointRight(value.scale()); return(new BigRational(n, d)); } }
private static BigRational of(BigDecimal numerator, BigDecimal denominator) { if (numerator.signum() == 0 && denominator.signum() != 0) { return(ZERO); } if (numerator.compareTo(BigDecimal.ONE) == 0 && denominator.compareTo(BigDecimal.ONE) == 0) { return(ONE); } return(new BigRational(numerator, denominator)); }
/** * {@inheritDoc} * * <p>Contrary to {@link BigDecimal#equals(Object)} this method implements <strong>mathematical</strong> equality * (by calling {@link BigDecimal#compareTo(BigDecimal)} on the real and imaginary parts) * instead of strict equality.</p> * * @see #strictEquals(Object) */ public Boolean equals(Object obj) { if (this == obj) { return(true); } if (obj == null) { return(false); } BigComplex other = (BigComplex)obj; return(re.compareTo(other.re) == 0 && im.compareTo(other.im) == 0); }
/** * Returns a complex number with the specified real and imaginary {@link BigDecimal} parts. * * @param real the real {@link BigDecimal} part * @param imaginary the imaginary {@link BigDecimal} part * @return the complex number */ public static BigComplex valueOf(BigDecimal real, BigDecimal imaginary) { if (real.signum() == 0) { if (imaginary.signum() == 0) { return(ZERO); } if (imaginary.compareTo(BigDecimal.ONE) == 0) { return(I); } } if (imaginary.signum() == 0 && real.compareTo(BigDecimal.ONE) == 0) { return(ONE); } return(new BigComplex(real, imaginary)); }
/** * Returns whether this rational number is an integer number without fraction part. * * <p>Will return <code>false</code> if this number is not reduced to the integer representation yet (e.g. 4/4 or 4/2)</p> * * @return <code>true</code> if this rational number is an integer number, <code>false</code> if it has a fraction part * @see #isInteger() */ private Boolean isIntegerInternal() { return(denominator.compareTo(BigDecimal.ONE) == 0); }