Search in sources :

Example 1 with MathArithmeticException

use of org.apache.ignite.ml.math.exceptions.MathArithmeticException in project ignite by apache.

the class Precision method roundUnscaled.

/**
 * Rounds the given non-negative value to the "nearest" integer. Nearest is
 * determined by the rounding method specified. Rounding methods are defined
 * in {@link BigDecimal}.
 *
 * @param unscaled Value to round.
 * @param sign Sign of the original, scaled value.
 * @param roundingMtd Rounding method, as defined in {@link BigDecimal}.
 * @return the rounded value.
 * @throws MathArithmeticException if an exact operation is required but result is not exact
 * @throws MathIllegalArgumentException if {@code roundingMethod} is not a valid rounding method.
 * @since 1.1 (previously in {@code MathUtils}, moved as of version 3.0)
 */
private static double roundUnscaled(double unscaled, double sign, int roundingMtd) throws MathArithmeticException, MathIllegalArgumentException {
    switch(roundingMtd) {
        case BigDecimal.ROUND_CEILING:
            if (sign == -1)
                unscaled = Math.floor(Math.nextAfter(unscaled, Double.NEGATIVE_INFINITY));
            else
                unscaled = Math.ceil(Math.nextAfter(unscaled, Double.POSITIVE_INFINITY));
            break;
        case BigDecimal.ROUND_DOWN:
            unscaled = Math.floor(Math.nextAfter(unscaled, Double.NEGATIVE_INFINITY));
            break;
        case BigDecimal.ROUND_FLOOR:
            if (sign == -1)
                unscaled = Math.ceil(Math.nextAfter(unscaled, Double.POSITIVE_INFINITY));
            else
                unscaled = Math.floor(Math.nextAfter(unscaled, Double.NEGATIVE_INFINITY));
            break;
        case BigDecimal.ROUND_HALF_DOWN:
            {
                unscaled = Math.nextAfter(unscaled, Double.NEGATIVE_INFINITY);
                double fraction = unscaled - Math.floor(unscaled);
                if (fraction > 0.5)
                    unscaled = Math.ceil(unscaled);
                else
                    unscaled = Math.floor(unscaled);
                break;
            }
        case BigDecimal.ROUND_HALF_EVEN:
            {
                double fraction = unscaled - Math.floor(unscaled);
                if (fraction > 0.5)
                    unscaled = Math.ceil(unscaled);
                else if (fraction < 0.5)
                    unscaled = Math.floor(unscaled);
                else {
                    // The following equality test is intentional and needed for rounding purposes
                    if (Math.floor(unscaled) / 2.0 == Math.floor(Math.floor(unscaled) / 2.0)) {
                        // even
                        unscaled = Math.floor(unscaled);
                    } else {
                        // odd
                        unscaled = Math.ceil(unscaled);
                    }
                }
                break;
            }
        case BigDecimal.ROUND_HALF_UP:
            {
                unscaled = Math.nextAfter(unscaled, Double.POSITIVE_INFINITY);
                double fraction = unscaled - Math.floor(unscaled);
                if (fraction >= 0.5)
                    unscaled = Math.ceil(unscaled);
                else
                    unscaled = Math.floor(unscaled);
                break;
            }
        case BigDecimal.ROUND_UNNECESSARY:
            if (unscaled != Math.floor(unscaled))
                throw new MathArithmeticException();
            break;
        case BigDecimal.ROUND_UP:
            // do not round if the discarded fraction is equal to zero
            if (unscaled != Math.floor(unscaled))
                unscaled = Math.ceil(Math.nextAfter(unscaled, Double.POSITIVE_INFINITY));
            break;
        default:
            throw new MathIllegalArgumentException(INVALID_ROUNDING_METHOD, roundingMtd, "ROUND_CEILING", BigDecimal.ROUND_CEILING, "ROUND_DOWN", BigDecimal.ROUND_DOWN, "ROUND_FLOOR", BigDecimal.ROUND_FLOOR, "ROUND_HALF_DOWN", BigDecimal.ROUND_HALF_DOWN, "ROUND_HALF_EVEN", BigDecimal.ROUND_HALF_EVEN, "ROUND_HALF_UP", BigDecimal.ROUND_HALF_UP, "ROUND_UNNECESSARY", BigDecimal.ROUND_UNNECESSARY, "ROUND_UP", BigDecimal.ROUND_UP);
    }
    return unscaled;
}
Also used : MathArithmeticException(org.apache.ignite.ml.math.exceptions.MathArithmeticException) MathIllegalArgumentException(org.apache.ignite.ml.math.exceptions.MathIllegalArgumentException)

Aggregations

MathArithmeticException (org.apache.ignite.ml.math.exceptions.MathArithmeticException)1 MathIllegalArgumentException (org.apache.ignite.ml.math.exceptions.MathIllegalArgumentException)1