/* * Copyright (c) 2016 Couchbase, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.couchbase.client.core.utils; /** * Various math utility methods, also backports from later JDK versions. * * @author Michael Nitschinger * @since 1.3.4 */ public class MathUtils { /** * Backport of floorMod from the JDK since we need to support 1.6. * * Returns the largest (closest to positive infinity) * {@code long} value that is less than or equal to the algebraic quotient. * There is one special case, if the dividend is the * {@linkplain Long#MIN_VALUE Long.MIN_VALUE} and the divisor is {@code -1}, * then integer overflow occurs and * the result is equal to the {@code Long.MIN_VALUE}. * <p> * Normal integer division operates under the round to zero rounding mode * (truncation). This operation instead acts under the round toward * negative infinity (floor) rounding mode. * The floor rounding mode gives different results than truncation * when the exact result is negative. * <p> * * @param x the dividend * @param y the divisor * @return the largest (closest to positive infinity) * {@code long} value that is less than or equal to the algebraic quotient. * @throws ArithmeticException if the divisor {@code y} is zero */ public static long floorMod(long x, long y) { return x - floorDiv(x, y) * y; } /** * Backport of floorDiv from the JDK since we need to support 1.6. * * Returns the largest (closest to positive infinity) * {@code long} value that is less than or equal to the algebraic quotient. * There is one special case, if the dividend is the * {@linkplain Long#MIN_VALUE Long.MIN_VALUE} and the divisor is {@code -1}, * then integer overflow occurs and * the result is equal to the {@code Long.MIN_VALUE}. * <p> * Normal integer division operates under the round to zero rounding mode * (truncation). This operation instead acts under the round toward * negative infinity (floor) rounding mode. * The floor rounding mode gives different results than truncation * when the exact result is negative. * <p> * * @param x the dividend * @param y the divisor * @return the largest (closest to positive infinity) * {@code long} value that is less than or equal to the algebraic quotient. * @throws ArithmeticException if the divisor {@code y} is zero * @see #floorMod(long, long) */ public static long floorDiv(long x, long y) { long r = x / y; // if the signs are different and modulo not zero, round down if ((x ^ y) < 0 && (r * y != x)) { r--; } return r; } }