/** * <p>Returns a pseudorandom, uniformly distributed int value * between <code>0</code> (inclusive) and the specified value * (exclusive), from the Math.random() sequence.</p> * * @param n the specified exclusive max-value * @return the random int */ public static int nextInt(int n) { return nextInt(JVM_RANDOM, n); }
/** * <p>Returns the next pseudorandom, uniformly distributed long value * from the Math.random() sequence.</p> * <b>N.B. All values are >= 0.<b> * @return the random long */ public static long nextLong() { return nextLong(JVM_RANDOM); }
/** * <p>Gets a hashCode for the fraction.</p> * * @return a hash code value for this object */ public int hashCode() { if (hashCode == 0) { // hashcode update should be atomic. hashCode = 37 * (37 * 17 + getNumerator()) + getDenominator(); } return hashCode; }
/** * <p>Tests whether the specified <code>double</code> occurs within * this range using <code>double</code> comparison.</p> * * <p>This implementation uses the {@link #getMinimumDouble()} and * {@link #getMaximumDouble()} methods and should be good for most uses.</p> * * @param value the double to test * @return <code>true</code> if the specified number occurs within this * range by <code>double</code> comparison */ public boolean containsDouble(double value) { int compareMin = NumberUtils.compare(getMinimumDouble(), value); int compareMax = NumberUtils.compare(getMaximumDouble(), value); return compareMin <= 0 && compareMax >= 0; }
/** * <p>Tests whether the specified <code>float</code> occurs within * this range using <code>float</code> comparison.</p> * * <p>This implementation uses the {@link #getMinimumFloat()} and * {@link #getMaximumFloat()} methods and should be good for most uses.</p> * * @param value the float to test * @return <code>true</code> if the specified number occurs within this * range by <code>float</code> comparison */ public boolean containsFloat(float value) { int compareMin = NumberUtils.compare(getMinimumFloat(), value); int compareMax = NumberUtils.compare(getMaximumFloat(), value); return compareMin <= 0 && compareMax >= 0; }
/** * <p>Tests whether the specified range overlaps with this range * using <code>double</code> comparison.</p> * * <p><code>null</code> is handled and returns <code>false</code>.</p> * * @param range the range to test, may be <code>null</code> * @return <code>true</code> if the specified range overlaps with this range */ public boolean overlapsRange(Range range) { if (range == null) { return false; } return range.containsDouble(min) || range.containsDouble(max) || containsDouble(range.getMinimumDouble()); }
/** * <p>Returns the next pseudorandom, uniformly distributed boolean value * from the Math.random() sequence.</p> * * @return the random boolean */ public static boolean nextBoolean() { return nextBoolean(JVM_RANDOM); }
/** * Compares this mutable to another in ascending order. * * @param obj the other mutable to compare to, not null * @return negative if this is less, zero if equal, positive if greater */ public int compareTo(Object obj) { MutableFloat other = (MutableFloat) obj; float anotherVal = other.value; return NumberUtils.compare(value, anotherVal); }
/** * <p>Returns the next pseudorandom, uniformly distributed float value * between <code>0.0</code> and <code>1.0</code> from the Math.random() * sequence.</p> * * @return the random double */ public static double nextDouble() { return nextDouble(JVM_RANDOM); }
/** * <p>Returns the next pseudorandom, uniformly distributed float value * between <code>0.0</code> and <code>1.0</code> from the Math.random() * sequence.</p> * * @return the random float */ public static float nextFloat() { return nextFloat(JVM_RANDOM); }
/** * <p>Returns the next pseudorandom, uniformly distributed long value * from the Math.random() sequence.</p> * Identical to <code>nextLong(Long.MAX_VALUE)</code> * <p> * <b>N.B. All values are >= 0.<b> * </p> * @return the random long */ public long nextLong() { return nextLong(Long.MAX_VALUE); }
/** * <p>Gets the minimum of three <code>double</code> values.</p> * * <p>NaN is only returned if all numbers are NaN as per IEEE-754r. </p> * * @param a value 1 * @param b value 2 * @param c value 3 * @return the smallest of the values */ public static double min(double a, double b, double c) { return min(min(a, b), c); }
/** * <p>Adds the value of this fraction to another, returning the result in reduced form. * The algorithm follows Knuth, 4.5.1.</p> * * @param fraction the fraction to add, must not be <code>null</code> * @return a <code>Fraction</code> instance with the resulting values * @throws IllegalArgumentException if the fraction is <code>null</code> * @throws ArithmeticException if the resulting numerator or denominator exceeds * <code>Integer.MAX_VALUE</code> */ public Fraction add(Fraction fraction) { return addSub(fraction, true /* add */); }
/** * <p>Gets the maximum of three <code>float</code> values.</p> * * <p>NaN is only returned if all numbers are NaN as per IEEE-754r. </p> * * @param a value 1 * @param b value 2 * @param c value 3 * @return the largest of the values */ public static float max(float a, float b, float c) { return max(max(a, b), c); }
/** * <p>Returns the next pseudorandom, uniformly distributed int value * from the Math.random() sequence.</p> * Identical to <code>nextInt(Integer.MAX_VALUE)</code> * <p> * <b>N.B. All values are >= 0.<b> * </p> * @return the random int */ public int nextInt() { return nextInt(Integer.MAX_VALUE); }
/** * <p>Returns the next pseudorandom, uniformly distributed int value * from the Math.random() sequence.</p> * <b>N.B. All values are >= 0.<b> * @return the random int */ public static int nextInt() { return nextInt(JVM_RANDOM); }
/** * Compares this mutable to another in ascending order. * * @param obj the other mutable to compare to, not null * @return negative if this is less, zero if equal, positive if greater * @throws ClassCastException if the argument is not a MutableDouble */ public int compareTo(Object obj) { MutableDouble other = (MutableDouble) obj; double anotherVal = other.value; return NumberUtils.compare(value, anotherVal); }
/** * <p>Gets the minimum of three <code>float</code> values.</p> * * <p>NaN is only returned if all numbers are NaN as per IEEE-754r. </p> * * @param a value 1 * @param b value 2 * @param c value 3 * @return the smallest of the values */ public static float min(float a, float b, float c) { return min(min(a, b), c); }
/** * <p>Subtracts the value of another fraction from the value of this one, * returning the result in reduced form.</p> * * @param fraction the fraction to subtract, must not be <code>null</code> * @return a <code>Fraction</code> instance with the resulting values * @throws IllegalArgumentException if the fraction is <code>null</code> * @throws ArithmeticException if the resulting numerator or denominator * cannot be represented in an <code>int</code>. */ public Fraction subtract(Fraction fraction) { return addSub(fraction, false /* subtract */); }
/** * <p>Gets the maximum of three <code>double</code> values.</p> * * <p>NaN is only returned if all numbers are NaN as per IEEE-754r. </p> * * @param a value 1 * @param b value 2 * @param c value 3 * @return the largest of the values */ public static double max(double a, double b, double c) { return max(max(a, b), c); }