public boolean isEven() { return NumberUtil.isEven(fInteger); }
@Override public boolean isOdd() { return NumberUtil.isOdd(fBigIntValue); }
/** * Test if the value is a perfect square (i.e. its square root is another * integer). See <a href= * "http://stackoverflow.com/questions/295579/fastest-way-to-determine-if-an-integers-square-root-is-an-integer"> * Stackoverflow.com - Fastest way to determine if an integer's square root * is an integer</a> * * @param bi * @return <code>true</code> if the number is a perfect square. */ public final static boolean isPerfectSquare(BigInteger bi) { try { return isPerfectSquare(toLong(bi)); } catch (ArithmeticException ae) { } return false; // number out of range exception }
/** * {@inheritDoc} */ public int toInt() throws ArithmeticException { return NumberUtil.toInt(fInteger); }
/** * Ceiling, round towards positive infinity. * <P> * Possible loss of precision. */ public static BigInteger ceiling(BigFraction f) { // [rounding step, possible loss of precision step] return round(f, BigDecimal.ROUND_CEILING); }
/** * {@inheritDoc} */ @Override public long toLong() throws ArithmeticException { return NumberUtil.toLong(fDouble); }
public static IInteger fibonacci(final IInteger iArg) { BigInteger a = BigInteger.ONE; BigInteger b = BigInteger.ZERO; BigInteger c = BigInteger.ONE; BigInteger d = BigInteger.ZERO; BigInteger f = BigInteger.ZERO; final BigInteger c2 = BigInteger.valueOf(2); BigInteger temp = iArg.getBigNumerator(); while (!NumberUtil.isZero(temp)) { if (NumberUtil.isOdd(temp)) { d = f.multiply(c); f = a.multiply(c).add(f.multiply(b).add(d)); a = a.multiply(b).add(d); } d = c.multiply(c); c = b.multiply(c).multiply(c2).add(d); b = b.multiply(b).add(d); temp = temp.shiftRight(1); } final IInteger i = F.integer(f); return i; }
/** * @return */ @Override public IExpr inverse() { if (NumberUtil.isNegative(fInteger)) { return FractionSym.valueOf(BigInteger.valueOf(-1), fInteger.negate()); } return FractionSym.valueOf(BigInteger.ONE, fInteger); }
@Override public IExpr evaluateArg1(final IExpr arg1) { if (arg1.isSignedNumber()) { final ISignedNumber in = (ISignedNumber) arg1; if (in.isNegative()) { return F.Pi; } else if (!in.equals(F.C0)) { return F.C0; } } else if (arg1.isComplex()) { final IComplex ic = (IComplex) arg1; if (ic.getRealPart().equals(BigFraction.ZERO)) { final BigFraction imaginaryPart = ic.getImaginaryPart(); if (NumberUtil.isNegative(imaginaryPart)) { return Times(F.CN1D2, F.Pi); } else if (NumberUtil.isPositive(imaginaryPart)) { return Times(F.C1D2, F.Pi); } } } return null; }
/** * Fractional part. * <P> * Possible loss of precision. */ public static BigFraction fractionalPart(BigFraction f) { // this==ip+fp; sign(fp)==sign(this) // [possible loss of precision step] return f.subtract(new BigFraction(integerPart(f), BigInteger.ONE)); }
/** * Round. * <P> * Round mode is one of { * <CODE>ROUND_UP, ROUND_DOWN, ROUND_CEILING, ROUND_FLOOR, * ROUND_HALF_UP, ROUND_HALF_DOWN, ROUND_HALF_EVEN, * ROUND_HALF_CEILING, ROUND_HALF_FLOOR, ROUND_HALF_ODD, * ROUND_UNNECESSARY, DEFAULT_ROUND_MODE (==ROUND_HALF_UP)</CODE> . * <P> * If rounding isn't necessary, i.e. this BigRational is an integer, [as an * optimization] this BigRational is returned. * <P> * Possible loss of precision. */ // @PrecisionLoss public static BigInteger round(BigFraction f, int roundMode) { // [rounding step, possible loss of precision step] return roundToBigInteger(f, roundMode); }
/** {@inheritDoc} */ public ISignedNumber ceil() { return IntegerSym.valueOf(NumberUtil.ceiling(fRational)); }
/** {@inheritDoc} */ @Override public int toIntDefault(int defaultValue) { try { return NumberUtil.toInt(fDouble); } catch (ArithmeticException ae) { return defaultValue; } }
/** * Ceiling, round towards positive infinity. * <P> * Possible loss of precision. */ public static BigInteger ceiling(BigFraction f) { // [rounding step, possible loss of precision step] return round(f, BigDecimal.ROUND_CEILING); }
/** * {@inheritDoc} */ @Override public long toLong() throws ArithmeticException { return NumberUtil.toLong(fBigIntValue); }
/** * Test if the fraction value is a perfect square (i.e. its numerator's and * denominator's square root are integers). See <a href= * "http://stackoverflow.com/questions/295579/fastest-way-to-determine-if-an-integers-square-root-is-an-integer"> * Stackoverflow.com - Fastest way to determine if an integer's square root * is an integer</a> * * @param bf * @return <code>true</code> if the number is a perfect square. */ public final static boolean isPerfectSquare(BigFraction bf) { try { long num = toLong(bf.getNumerator()); long den = toLong(bf.getDenominator()); return isPerfectSquare(den) && isPerfectSquare(num); } catch (ArithmeticException ae) { } return false; // number out of range exception }
/** * @return */ @Override public ISignedNumber inverse() { if (isOne()) { return this; } if (NumberUtil.isNegative(fBigIntValue)) { return AbstractFractionSym.valueOf(BigInteger.valueOf(-1), fBigIntValue.negate()); } return AbstractFractionSym.valueOf(BigInteger.ONE, fBigIntValue); }
/** * Fractional part. * <P> * Possible loss of precision. */ public static BigFraction fractionalPart(BigFraction f) { // this==ip+fp; sign(fp)==sign(this) // [possible loss of precision step] return f.subtract(new BigFraction(integerPart(f), BigInteger.ONE)); }
/** * Round. * <P> * Round mode is one of { * <CODE>ROUND_UP, ROUND_DOWN, ROUND_CEILING, ROUND_FLOOR, * ROUND_HALF_UP, ROUND_HALF_DOWN, ROUND_HALF_EVEN, * ROUND_HALF_CEILING, ROUND_HALF_FLOOR, ROUND_HALF_ODD, * ROUND_UNNECESSARY, DEFAULT_ROUND_MODE (==ROUND_HALF_UP)</CODE> . * <P> * If rounding isn't necessary, i.e. this BigRational is an integer, [as an * optimization] this BigRational is returned. * <P> * Possible loss of precision. */ // @PrecisionLoss public static BigInteger round(BigFraction f, int roundMode) { // [rounding step, possible loss of precision step] return roundToBigInteger(f, roundMode); }