/** * Returns the string representation of this rational number as integer and fraction parts in the form "integerPart fractionNominator/fractionDenominator". * * <p>The integer part is omitted if it is 0 (when this absolute rational number is smaller than 1).</p> * <p>The fraction part is omitted it it is 0 (when this rational number is an integer).</p> * <p>If this rational number is 0, then "0" is returned.</p> * * <p>Example: <code>BigRational.valueOf(3.5).toIntegerRationalString()</code> returns <code>"3 1/2"</code>.</p> * * @return the integer and fraction rational string representation * @see #valueOf(int, int, int) */ public String toIntegerRationalString() { BigDecimal fractionNumerator = numerator.remainder(denominator); BigDecimal integerNumerator = numerator.subtract(fractionNumerator); BigDecimal integerPart = integerNumerator.divide(denominator); StringBuilder result = new StringBuilder(); if (integerPart.signum() != 0) { result.Append(integerPart); } if (fractionNumerator.signum() != 0) { if (result.Length > 0) { result.Append(' '); } result.Append(fractionNumerator.abs()); result.Append('/'); result.Append(denominator); } if (result.Length == 0) { result.Append('0'); } return(result.ToString()); }
public BigRational(BigDecimal num, BigDecimal denom) { BigDecimal n = num; BigDecimal d = denom; if (d.signum() == 0) { throw new ArithmeticException("Divide by zero"); } if (d.signum() < 0) { n = n.negate(); d = d.negate(); } numerator = n; denominator = d; }
/** * Returns a complex number with the specified polar {@link BigDecimal} radius and angle using the specified {@link MathContext}. * * @param radius the {@link BigDecimal} radius of the polar representation * @param angle the {@link BigDecimal} angle in radians of the polar representation * @param mathContext the {@link MathContext} used to calculate the result * @return the complex number */ public static BigComplex valueOfPolar(BigDecimal radius, BigDecimal angle, MathContext mathContext) { if (radius.signum() == 0) { return(ZERO); } return(valueOf( radius.multiply(BigDecimalMath.cos(angle, mathContext), mathContext), radius.multiply(BigDecimalMath.sin(angle, mathContext), mathContext))); }
/** * 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)); }
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)); }
/** * Returns the signum function of this rational number. * * @return -1, 0 or 1 as the value of this rational number is negative, zero or positive. */ public int signum() { return(numerator.signum()); }
/** * Returns whether this complex number only has a real part (the imaginary part is 0). * * @return {@code true} if this complex number only has a real part, {@code false} if the imaginary part is not 0 */ public Boolean isReal() { return(im.signum() == 0); }