Java Code Examples for java.math.BigInteger#remainder()

The following examples show how to use java.math.BigInteger#remainder() . These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
/**
 * <p>Set year of XSD <code>dateTime</code> year field.</p>
 *
 * <p>Unset this field by invoking the setter with a parameter value of
 * {@link DatatypeConstants#FIELD_UNDEFINED}.</p>
 *
 * <p>Note: if the absolute value of the <code>year</code> parameter
 * is less than 10^9, the eon component of the XSD year field is set to
 * <code>null</code> by this method.</p>
 *
 * @param year value constraints are summarized in <a href="#datetimefield-year">year field of date/time field mapping table</a>.
 *   If year is {@link DatatypeConstants#FIELD_UNDEFINED}, then eon is set to <code>null</code>.
 */
public final void setYear(int year) {
    if (year == DatatypeConstants.FIELD_UNDEFINED) {
        this.year = DatatypeConstants.FIELD_UNDEFINED;
        this.eon = null;
    }
    else if (Math.abs(year) < BILLION_I) {
        this.year = year;
        this.eon = null;
    } else {
        BigInteger theYear = BigInteger.valueOf((long) year);
        BigInteger remainder = theYear.remainder(BILLION_B);
        this.year = remainder.intValue();
        setEon(theYear.subtract(remainder));
    }
}
 
Example 2
/**
 * Formats a {@link BigFraction} object to produce a string.  The BigFraction
 * is output in proper format.
 *
 * @param fraction the object to format.
 * @param toAppendTo where the text is to be appended
 * @param pos On input: an alignment field, if desired. On output: the
 *            offsets of the alignment field
 * @return the value passed in as toAppendTo.
 */
@Override
public StringBuffer format(final BigFraction fraction,
                           final StringBuffer toAppendTo, final FieldPosition pos) {

    pos.setBeginIndex(0);
    pos.setEndIndex(0);

    BigInteger num = fraction.getNumerator();
    BigInteger den = fraction.getDenominator();
    BigInteger whole = num.divide(den);
    num = num.remainder(den);

    if (!BigInteger.ZERO.equals(whole)) {
        getWholeFormat().format(whole, toAppendTo, pos);
        toAppendTo.append(' ');
        if (num.compareTo(BigInteger.ZERO) < 0) {
            num = num.negate();
        }
    }
    getNumeratorFormat().format(num, toAppendTo, pos);
    toAppendTo.append(" / ");
    getDenominatorFormat().format(den, toAppendTo, pos);

    return toAppendTo;
}
 
Example 3
Source Project: activiti6-boot2   File: PathConverter.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * @return a path representing the content with the given unique content index.
 */
public File getPathForId(BigInteger id) {
    BigInteger remainder = null;
    
    if(id.compareTo(BigInteger.ZERO) < 0) {
        throw new ContentStorageException("ID cannot be negative");
    }
    
    Long[] blocks = new Long[iterationDepth];
    for(int i=0; i< iterationDepth; i++) {
        remainder = id.remainder(blockSize);
        blocks[i] = remainder.longValue();
        id = id.subtract(remainder).divide(blockSize);
    }
    
    if(!id.equals(BigInteger.ZERO)) {
        throw new ContentStorageException("ID out of range of content storage limit: " + 
           blockSize.pow(iterationDepth).subtract(BigInteger.ONE));
    }
    
    StringBuffer buffer = new StringBuffer();
    for(int i=iterationDepth - 1; i>=0; i--) {
        buffer.append(blocks[i].toString()).append(File.separatorChar);
    }
    return new File(buffer.toString());
}
 
Example 4
/**
 * <p>Set year of XSD <code>dateTime</code> year field.</p>
 *
 * <p>Unset this field by invoking the setter with a parameter value of
 * {@link DatatypeConstants#FIELD_UNDEFINED}.</p>
 *
 * <p>Note: if the absolute value of the <code>year</code> parameter
 * is less than 10^9, the eon component of the XSD year field is set to
 * <code>null</code> by this method.</p>
 *
 * @param year value constraints are summarized in <a href="#datetimefield-year">year field of date/time field mapping table</a>.
 *   If year is {@link DatatypeConstants#FIELD_UNDEFINED}, then eon is set to <code>null</code>.
 */
public void setYear(int year) {
    if (year == DatatypeConstants.FIELD_UNDEFINED) {
        this.year = DatatypeConstants.FIELD_UNDEFINED;
        this.eon = null;
    }
    else if (Math.abs(year) < BILLION_I) {
        this.year = year;
        this.eon = null;
    } else {
        BigInteger theYear = BigInteger.valueOf((long) year);
        BigInteger remainder = theYear.remainder(BILLION_B);
        this.year = remainder.intValue();
        setEon(theYear.subtract(remainder));
    }
}
 
Example 5
/**
 * Formats a {@link BigFraction} object to produce a string.  The BigFraction
 * is output in proper format.
 *
 * @param fraction the object to format.
 * @param toAppendTo where the text is to be appended
 * @param pos On input: an alignment field, if desired. On output: the
 *            offsets of the alignment field
 * @return the value passed in as toAppendTo.
 */
@Override
public StringBuffer format(final BigFraction fraction,
                           final StringBuffer toAppendTo, final FieldPosition pos) {

    pos.setBeginIndex(0);
    pos.setEndIndex(0);

    BigInteger num = fraction.getNumerator();
    BigInteger den = fraction.getDenominator();
    BigInteger whole = num.divide(den);
    num = num.remainder(den);

    if (!BigInteger.ZERO.equals(whole)) {
        getWholeFormat().format(whole, toAppendTo, pos);
        toAppendTo.append(' ');
        if (num.compareTo(BigInteger.ZERO) < 0) {
            num = num.negate();
        }
    }
    getNumeratorFormat().format(num, toAppendTo, pos);
    toAppendTo.append(" / ");
    getDenominatorFormat().format(den, toAppendTo, pos);

    return toAppendTo;
}
 
Example 6
/**
 * Formats a {@link BigFraction} object to produce a string.  The BigFraction
 * is output in proper format.
 *
 * @param fraction the object to format.
 * @param toAppendTo where the text is to be appended
 * @param pos On input: an alignment field, if desired. On output: the
 *            offsets of the alignment field
 * @return the value passed in as toAppendTo.
 */
@Override
public StringBuffer format(final BigFraction fraction,
                           final StringBuffer toAppendTo, final FieldPosition pos) {

    pos.setBeginIndex(0);
    pos.setEndIndex(0);

    BigInteger num = fraction.getNumerator();
    BigInteger den = fraction.getDenominator();
    BigInteger whole = num.divide(den);
    num = num.remainder(den);

    if (!BigInteger.ZERO.equals(whole)) {
        getWholeFormat().format(whole, toAppendTo, pos);
        toAppendTo.append(' ');
        if (num.compareTo(BigInteger.ZERO) < 0) {
            num = num.negate();
        }
    }
    getNumeratorFormat().format(num, toAppendTo, pos);
    toAppendTo.append(" / ");
    getDenominatorFormat().format(den, toAppendTo, pos);

    return toAppendTo;
}
 
Example 7
private static void validateDHPublicKey(BigInteger p,
        BigInteger g, BigInteger y) throws InvalidKeyException {

    // For better interoperability, the interval is limited to [2, p-2].
    BigInteger leftOpen = BigInteger.ONE;
    BigInteger rightOpen = p.subtract(BigInteger.ONE);
    if (y.compareTo(leftOpen) <= 0) {
        throw new InvalidKeyException(
                "Diffie-Hellman public key is too small");
    }
    if (y.compareTo(rightOpen) >= 0) {
        throw new InvalidKeyException(
                "Diffie-Hellman public key is too large");
    }

    // y^q mod p == 1?
    // Unable to perform this check as q is unknown in this circumstance.

    // p is expected to be prime.  However, it is too expensive to check
    // that p is prime.  Instead, in order to mitigate the impact of
    // non-prime values, we check that y is not a factor of p.
    BigInteger r = p.remainder(y);
    if (r.equals(BigInteger.ZERO)) {
        throw new InvalidKeyException("Invalid Diffie-Hellman parameters");
    }
}
 
Example 8
/**
 * Formats a {@link BigFraction} object to produce a string.  The BigFraction
 * is output in proper format.
 *
 * @param fraction the object to format.
 * @param toAppendTo where the text is to be appended
 * @param pos On input: an alignment field, if desired. On output: the
 *            offsets of the alignment field
 * @return the value passed in as toAppendTo.
 */
@Override
public StringBuffer format(final BigFraction fraction,
                           final StringBuffer toAppendTo, final FieldPosition pos) {

    pos.setBeginIndex(0);
    pos.setEndIndex(0);

    BigInteger num = fraction.getNumerator();
    BigInteger den = fraction.getDenominator();
    BigInteger whole = num.divide(den);
    num = num.remainder(den);

    if (!BigInteger.ZERO.equals(whole)) {
        getWholeFormat().format(whole, toAppendTo, pos);
        toAppendTo.append(' ');
        if (num.compareTo(BigInteger.ZERO) < 0) {
            num = num.negate();
        }
    }
    getNumeratorFormat().format(num, toAppendTo, pos);
    toAppendTo.append(" / ");
    getDenominatorFormat().format(den, toAppendTo, pos);

    return toAppendTo;
}
 
Example 9
Source Project: bistoury   File: OpModulus.java    License: GNU General Public License v3.0 5 votes vote down vote up
@Override
public TypedValue getValueInternal(ExpressionState state) throws EvaluationException {
    Object leftOperand = getLeftOperand().getValueInternal(state).getValue();
    Object rightOperand = getRightOperand().getValueInternal(state).getValue();

    if (leftOperand instanceof Number && rightOperand instanceof Number) {
        Number leftNumber = (Number) leftOperand;
        Number rightNumber = (Number) rightOperand;

        if (leftNumber instanceof BigDecimal || rightNumber instanceof BigDecimal) {
            BigDecimal leftBigDecimal = NumberUtils.convertNumberToTargetClass(leftNumber, BigDecimal.class);
            BigDecimal rightBigDecimal = NumberUtils.convertNumberToTargetClass(rightNumber, BigDecimal.class);
            return new TypedValue(leftBigDecimal.remainder(rightBigDecimal));
        } else if (leftNumber instanceof Double || rightNumber instanceof Double) {
            this.exitTypeDescriptor = "D";
            return new TypedValue(leftNumber.doubleValue() % rightNumber.doubleValue());
        } else if (leftNumber instanceof Float || rightNumber instanceof Float) {
            this.exitTypeDescriptor = "F";
            return new TypedValue(leftNumber.floatValue() % rightNumber.floatValue());
        } else if (leftNumber instanceof BigInteger || rightNumber instanceof BigInteger) {
            BigInteger leftBigInteger = NumberUtils.convertNumberToTargetClass(leftNumber, BigInteger.class);
            BigInteger rightBigInteger = NumberUtils.convertNumberToTargetClass(rightNumber, BigInteger.class);
            return new TypedValue(leftBigInteger.remainder(rightBigInteger));
        } else if (leftNumber instanceof Long || rightNumber instanceof Long) {
            this.exitTypeDescriptor = "J";
            return new TypedValue(leftNumber.longValue() % rightNumber.longValue());
        } else if (CodeFlow.isIntegerForNumericOp(leftNumber) || CodeFlow.isIntegerForNumericOp(rightNumber)) {
            this.exitTypeDescriptor = "I";
            return new TypedValue(leftNumber.intValue() % rightNumber.intValue());
        } else {
            // Unknown Number subtypes -> best guess is double division
            return new TypedValue(leftNumber.doubleValue() % rightNumber.doubleValue());
        }
    }

    return state.operate(Operation.MODULUS, leftOperand, rightOperand);
}
 
Example 10
/**
 * <p>Set year of XSD <code>dateTime</code> year field.</p>
 *
 * <p>Unset this field by invoking the setter with a parameter value of
 * {@link DatatypeConstants#FIELD_UNDEFINED}.</p>
 *
 * <p>Note: if the absolute value of the <code>year</code> parameter
 * is less than 10^9, the eon component of the XSD year field is set to
 * <code>null</code> by this method.</p>
 *
 * @param year value constraints are summarized in <a href="#datetimefield-year">year field of date/time field mapping table</a>.
 *   If year is {@link DatatypeConstants#FIELD_UNDEFINED}, then eon is set to <code>null</code>.
 */
public void setYear(int year) {
    if (year == DatatypeConstants.FIELD_UNDEFINED) {
        this.year = DatatypeConstants.FIELD_UNDEFINED;
        this.eon = null;
    } else if (Math.abs(year) < BILLION.intValue()) {
        this.year = year;
        this.eon = null;
    } else {
        BigInteger theYear = BigInteger.valueOf((long) year);
        BigInteger remainder = theYear.remainder(BILLION);
        this.year = remainder.intValue();
        setEon(theYear.subtract(remainder));
    }
}
 
Example 11
/**
 * <p>Set year of XSD <code>dateTime</code> year field.</p>
 *
 * <p>Unset this field by invoking the setter with a parameter value of
 * {@link DatatypeConstants#FIELD_UNDEFINED}.</p>
 *
 * <p>Note: if the absolute value of the <code>year</code> parameter
 * is less than 10^9, the eon component of the XSD year field is set to
 * <code>null</code> by this method.</p>
 *
 * @param year value constraints are summarized in <a href="#datetimefield-year">year field of date/time field mapping table</a>.
 *   If year is {@link DatatypeConstants#FIELD_UNDEFINED}, then eon is set to <code>null</code>.
 */
public void setYear(int year) {
    if (year == DatatypeConstants.FIELD_UNDEFINED) {
        this.year = DatatypeConstants.FIELD_UNDEFINED;
        this.eon = null;
    } else if (Math.abs(year) < BILLION.intValue()) {
        this.year = year;
        this.eon = null;
    } else {
        BigInteger theYear = BigInteger.valueOf((long) year);
        BigInteger remainder = theYear.remainder(BILLION);
        this.year = remainder.intValue();
        setEon(theYear.subtract(remainder));
    }
}
 
Example 12
/**
 * <p>Set year of XSD <code>dateTime</code> year field.</p>
 *
 * <p>Unset this field by invoking the setter with a parameter value of
 * {@link DatatypeConstants#FIELD_UNDEFINED}.</p>
 *
 * <p>Note: if the absolute value of the <code>year</code> parameter
 * is less than 10^9, the eon component of the XSD year field is set to
 * <code>null</code> by this method.</p>
 *
 * @param year value constraints are summarized in <a href="#datetimefield-year">year field of date/time field mapping table</a>.
 *   If year is {@link DatatypeConstants#FIELD_UNDEFINED}, then eon is set to <code>null</code>.
 */
public void setYear(int year) {
    if (year == DatatypeConstants.FIELD_UNDEFINED) {
        this.year = DatatypeConstants.FIELD_UNDEFINED;
        this.eon = null;
    } else if (Math.abs(year) < BILLION.intValue()) {
        this.year = year;
        this.eon = null;
    } else {
        BigInteger theYear = BigInteger.valueOf((long) year);
        BigInteger remainder = theYear.remainder(BILLION);
        this.year = remainder.intValue();
        setEon(theYear.subtract(remainder));
    }
}
 
Example 13
Source Project: tlaplus   File: Combinatorics.java    License: MIT License 5 votes vote down vote up
public static BigInteger[] toSeq(BigInteger[] B, BigInteger n, int len) {
  Assert.check((B.length >= len) && (len != 0), EC.SYSTEM_INDEX_ERROR);

  BigInteger[] nlist = new BigInteger[len];
  BigInteger num = n;
  BigInteger base = B[0];
  nlist[0] = num.remainder(base);
  for (int i = 1; i < len; i++) {
    num = num.divide(base);
    base = B[i];
    nlist[i] = num.remainder(base);
  }
  return nlist;
}
 
Example 14
/**
 * <p>Set year of XSD <code>dateTime</code> year field.</p>
 *
 * <p>Unset this field by invoking the setter with a parameter value of
 * {@link DatatypeConstants#FIELD_UNDEFINED}.</p>
 *
 * <p>Note: if the absolute value of the <code>year</code> parameter
 * is less than 10^9, the eon component of the XSD year field is set to
 * <code>null</code> by this method.</p>
 *
 * @param year value constraints are summarized in <a href="#datetimefield-year">year field of date/time field mapping table</a>.
 *   If year is {@link DatatypeConstants#FIELD_UNDEFINED}, then eon is set to <code>null</code>.
 */
public void setYear(int year) {
    if (year == DatatypeConstants.FIELD_UNDEFINED) {
        this.year = DatatypeConstants.FIELD_UNDEFINED;
        this.eon = null;
    } else if (Math.abs(year) < BILLION.intValue()) {
        this.year = year;
        this.eon = null;
    } else {
        BigInteger theYear = BigInteger.valueOf((long) year);
        BigInteger remainder = theYear.remainder(BILLION);
        this.year = remainder.intValue();
        setEon(theYear.subtract(remainder));
    }
}
 
Example 15
/**
 * Formats a {@link BigFraction} object to produce a string.  The BigFraction
 * is output in proper format.
 *
 * @param fraction the object to format.
 * @param toAppendTo where the text is to be appended
 * @param pos On input: an alignment field, if desired. On output: the
 * offsets of the alignment field
 * @return the value passed in as toAppendTo.
 */
@Override
public StringBuffer format(final BigFraction fraction,
                           final StringBuffer toAppendTo,
                           final FieldPosition pos) {

    pos.setBeginIndex(0);
    pos.setEndIndex(0);

    BigInteger num = fraction.getNumerator();
    BigInteger den = fraction.getDenominator();
    BigInteger whole = num.divide(den);
    num = num.remainder(den);

    if (!BigInteger.ZERO.equals(whole)) {
        getWholeFormat().format(whole, toAppendTo, pos);
        toAppendTo.append(' ');
        if (num.compareTo(BigInteger.ZERO) < 0) {
            num = num.negate();
        }
    }
    getNumeratorFormat().format(num, toAppendTo, pos);
    toAppendTo.append(" / ");
    getDenominatorFormat().format(den, toAppendTo, pos);

    return toAppendTo;
}
 
Example 16
Source Project: java-technology-stack   File: OpModulus.java    License: MIT License 4 votes vote down vote up
@Override
public TypedValue getValueInternal(ExpressionState state) throws EvaluationException {
	Object leftOperand = getLeftOperand().getValueInternal(state).getValue();
	Object rightOperand = getRightOperand().getValueInternal(state).getValue();

	if (leftOperand instanceof Number && rightOperand instanceof Number) {
		Number leftNumber = (Number) leftOperand;
		Number rightNumber = (Number) rightOperand;

		if (leftNumber instanceof BigDecimal || rightNumber instanceof BigDecimal) {
			BigDecimal leftBigDecimal = NumberUtils.convertNumberToTargetClass(leftNumber, BigDecimal.class);
			BigDecimal rightBigDecimal = NumberUtils.convertNumberToTargetClass(rightNumber, BigDecimal.class);
			return new TypedValue(leftBigDecimal.remainder(rightBigDecimal));
		}
		else if (leftNumber instanceof Double || rightNumber instanceof Double) {
			this.exitTypeDescriptor = "D";
			return new TypedValue(leftNumber.doubleValue() % rightNumber.doubleValue());
		}
		else if (leftNumber instanceof Float || rightNumber instanceof Float) {
			this.exitTypeDescriptor = "F";
			return new TypedValue(leftNumber.floatValue() % rightNumber.floatValue());
		}
		else if (leftNumber instanceof BigInteger || rightNumber instanceof BigInteger) {
			BigInteger leftBigInteger = NumberUtils.convertNumberToTargetClass(leftNumber, BigInteger.class);
			BigInteger rightBigInteger = NumberUtils.convertNumberToTargetClass(rightNumber, BigInteger.class);
			return new TypedValue(leftBigInteger.remainder(rightBigInteger));
		}
		else if (leftNumber instanceof Long || rightNumber instanceof Long) {
			this.exitTypeDescriptor = "J";
			return new TypedValue(leftNumber.longValue() % rightNumber.longValue());
		}
		else if (CodeFlow.isIntegerForNumericOp(leftNumber) || CodeFlow.isIntegerForNumericOp(rightNumber)) {
			this.exitTypeDescriptor = "I";
			return new TypedValue(leftNumber.intValue() % rightNumber.intValue());
		}
		else {
			// Unknown Number subtypes -> best guess is double division
			return new TypedValue(leftNumber.doubleValue() % rightNumber.doubleValue());
		}
	}

	return state.operate(Operation.MODULUS, leftOperand, rightOperand);
}
 
Example 17
/**
 * <p>Set low and high order component of XSD <code>dateTime</code> year field.</p>
 *
 * <p>Unset this field by invoking the setter with a parameter value of <code>null</code>.</p>
 *
 * @param year value constraints summarized in <a href="#datetimefield-year">year field of date/time field mapping table</a>.
 *
 * @throws IllegalArgumentException if <code>year</code> parameter is
 * outside value constraints for the field as specified in
 * <a href="#datetimefieldmapping">date/time field mapping table</a>.
 */
public void setYear(BigInteger year) {
    if (year == null) {
        this.eon = null;
        this.year = DatatypeConstants.FIELD_UNDEFINED;
    } else {
        BigInteger temp = year.remainder(BILLION);
        this.year = temp.intValue();
        setEon(year.subtract(temp));
    }
}
 
Example 18
/**
 * <p>Set low and high order component of XSD <code>dateTime</code> year field.</p>
 *
 * <p>Unset this field by invoking the setter with a parameter value of <code>null</code>.</p>
 *
 * @param year value constraints summarized in <a href="#datetimefield-year">year field of date/time field mapping table</a>.
 *
 * @throws IllegalArgumentException if <code>year</code> parameter is
 * outside value constraints for the field as specified in
 * <a href="#datetimefieldmapping">date/time field mapping table</a>.
 */
public void setYear(BigInteger year) {
    if (year == null) {
        this.eon = null;
        this.year = DatatypeConstants.FIELD_UNDEFINED;
    } else {
        BigInteger temp = year.remainder(BILLION_B);
        this.year = temp.intValue();
        setEon(year.subtract(temp));
    }
}
 
Example 19
/**
 * <p>Set low and high order component of XSD <code>dateTime</code> year field.</p>
 *
 * <p>Unset this field by invoking the setter with a parameter value of <code>null</code>.</p>
 *
 * @param year value constraints summarized in <a href="#datetimefield-year">year field of date/time field mapping table</a>.
 *
 * @throws IllegalArgumentException if <code>year</code> parameter is
 * outside value constraints for the field as specified in
 * <a href="#datetimefieldmapping">date/time field mapping table</a>.
 */
public void setYear(BigInteger year) {
    if (year == null) {
        this.eon = null;
        this.year = DatatypeConstants.FIELD_UNDEFINED;
    } else {
        BigInteger temp = year.remainder(BILLION);
        this.year = temp.intValue();
        setEon(year.subtract(temp));
    }
}
 
Example 20
/**
 * <p>Set low and high order component of XSD <code>dateTime</code> year field.</p>
 *
 * <p>Unset this field by invoking the setter with a parameter value of <code>null</code>.</p>
 *
 * @param year value constraints summarized in <a href="#datetimefield-year">year field of date/time field mapping table</a>.
 *
 * @throws IllegalArgumentException if <code>year</code> parameter is
 * outside value constraints for the field as specified in
 * <a href="#datetimefieldmapping">date/time field mapping table</a>.
 */
public void setYear(BigInteger year) {
    if (year == null) {
        this.eon = null;
        this.year = DatatypeConstants.FIELD_UNDEFINED;
    } else {
        BigInteger temp = year.remainder(BILLION);
        this.year = temp.intValue();
        setEon(year.subtract(temp));
    }
}