Android Open Source - timesync Math Util






From Project

Back to project page timesync.

License

The source code is released under:

Apache License

If you think the Android project timesync listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package me.tatarka.timesync.lib;
//from  w w w . j  a  va  2  s . c o  m
class MathUtil {
    /**
     * Divides 2 longs, but takes the ceiling instead of rounding towards zero.
     *
     * Implementation taken from: <a href="http://ericlippert.com/2013/01/28/integer-division-that-rounds-up/">
     * http://ericlippert.com/2013/01/28/integer-division-that-rounds-up/</a>
     *
     * @param a the numerator
     * @param b the denominator
     * @return ceil[a/b]
     * @throws java.lang.ArithmeticException if b is zero.
     */
    static long divCeil(long a, long b) {
        if (b == 0) throw new ArithmeticException("divide by zero");
        if (b == -1 && a == Long.MIN_VALUE) return Long.MIN_VALUE;
        long roundedTowardsZero = a / b;
        boolean dividedEvenly = (a % b) == 0;
        if (dividedEvenly) {
            return roundedTowardsZero;
        } else {
            boolean roundedDown = (a > 0) == (b > 0);
            return roundedDown ? roundedTowardsZero + 1 : roundedTowardsZero;
        }
    }

    /**
     * For some pseudo-random uniformly distributed long seed, returns a pseudo-random uniformly
     * distributed long between lower and upper (both inclusive). A good source of the seed
     * would be {@link java.util.Random#nextLong()}.
     *
     * @param seed  the seed value
     * @param lower the lower bounds
     * @param upper the upper bounds
     * @return the new pseudo-random value
     */
    static long randomInRange(long seed, long lower, long upper) {
        if (lower == upper) return upper; // Only valid value!
        long span = upper - lower;
        if (span < 0) {
            throw new IllegalArgumentException("upper must be greater than lower !(" + upper + ">" + lower + ")");
        }

        // Convert [-Long.MIN_VALUE,Long.MAX_VALUE] to [0, span]
        // source: http://stackoverflow.com/a/2546186 which was in turn taken from the javadoc for
        // Random.nextInt(n).
        long bits, val;
        do {
            bits = (seed << 1) >>> 1;
            val = bits % span;
        } while (bits - val + span < 0L);

        return val + lower;
    }
}




Java Source Code List

me.tatarka.timesync.app.MainActivity.java
me.tatarka.timesync.app.MyApplication.java
me.tatarka.timesync.app.RandomSync.java
me.tatarka.timesync.lib.BooleanFormatException.java
me.tatarka.timesync.lib.EventCalculator.java
me.tatarka.timesync.lib.MathUtil.java
me.tatarka.timesync.lib.ReceiverUtils.java
me.tatarka.timesync.lib.SuperNotCalledException.java
me.tatarka.timesync.lib.TimeSyncBootReceiver.java
me.tatarka.timesync.lib.TimeSyncNetworkReceiver.java
me.tatarka.timesync.lib.TimeSyncParser.java
me.tatarka.timesync.lib.TimeSyncPowerReceiver.java
me.tatarka.timesync.lib.TimeSyncPreferences.java
me.tatarka.timesync.lib.TimeSyncProxy.java
me.tatarka.timesync.lib.TimeSyncService.java
me.tatarka.timesync.lib.TimeSync.java