27 28 /** 29 * Faster Math ceil / floor routines derived from StrictMath 30 */ 31 public final class FloatMath implements MarlinConst { 32 33 // overflow / NaN handling enabled: 34 static final boolean CHECK_OVERFLOW = true; 35 static final boolean CHECK_NAN = true; 36 37 private FloatMath() { 38 // utility class 39 } 40 41 // faster inlined min/max functions in the branch prediction is high 42 static float max(final float a, final float b) { 43 // no NaN handling 44 return (a >= b) ? a : b; 45 } 46 47 public static int max(final int a, final int b) { 48 return (a >= b) ? a : b; 49 } 50 51 public static int min(final int a, final int b) { 52 return (a <= b) ? a : b; 53 } 54 55 /** 56 * Faster alternative to ceil(float) optimized for the integer domain 57 * and supporting NaN and +/-Infinity. 58 * 59 * @param a a value. 60 * @return the largest (closest to positive infinity) integer value 61 * that less than or equal to the argument and is equal to a mathematical 62 * integer. 63 */ 64 public static int ceil_int(final float a) { 65 final int intpart = (int) a; 66 67 if (a <= intpart 68 || (CHECK_OVERFLOW && intpart == Integer.MAX_VALUE) 69 || CHECK_NAN && Float.isNaN(a)) { 70 return intpart; 71 } 72 return intpart + 1; 73 } 74 75 /** 76 * Faster alternative to floor(float) optimized for the integer domain 77 * and supporting NaN and +/-Infinity. 78 * 79 * @param a a value. 80 * @return the largest (closest to positive infinity) floating-point value 81 * that less than or equal to the argument and is equal to a mathematical 82 * integer. 83 */ 84 public static int floor_int(final float a) { 85 final int intpart = (int) a; 86 87 if (a >= intpart 88 || (CHECK_OVERFLOW && intpart == Integer.MIN_VALUE) 89 || CHECK_NAN && Float.isNaN(a)) { 90 return intpart; 91 } 92 return intpart - 1; 93 } 94 } | 27 28 /** 29 * Faster Math ceil / floor routines derived from StrictMath 30 */ 31 public final class FloatMath implements MarlinConst { 32 33 // overflow / NaN handling enabled: 34 static final boolean CHECK_OVERFLOW = true; 35 static final boolean CHECK_NAN = true; 36 37 private FloatMath() { 38 // utility class 39 } 40 41 // faster inlined min/max functions in the branch prediction is high 42 static float max(final float a, final float b) { 43 // no NaN handling 44 return (a >= b) ? a : b; 45 } 46 47 static double max(final double a, final double b) { 48 // no NaN handling 49 return (a >= b) ? a : b; 50 } 51 52 public static int max(final int a, final int b) { 53 return (a >= b) ? a : b; 54 } 55 56 public static int min(final int a, final int b) { 57 return (a <= b) ? a : b; 58 } 59 60 /** 61 * Faster alternative to ceil(float) optimized for the integer domain 62 * and supporting NaN and +/-Infinity. 63 * 64 * @param a a value. 65 * @return the largest (closest to positive infinity) integer value 66 * that less than or equal to the argument and is equal to a mathematical 67 * integer. 68 */ 69 public static int ceil_int(final float a) { 70 final int intpart = (int) a; 71 72 if (a <= intpart 73 || (CHECK_OVERFLOW && intpart == Integer.MAX_VALUE) 74 || CHECK_NAN && Float.isNaN(a)) { 75 return intpart; 76 } 77 return intpart + 1; 78 } 79 80 /** 81 * Faster alternative to ceil(double) optimized for the integer domain 82 * and supporting NaN and +/-Infinity. 83 * 84 * @param a a value. 85 * @return the largest (closest to positive infinity) integer value 86 * that less than or equal to the argument and is equal to a mathematical 87 * integer. 88 */ 89 public static int ceil_int(final double a) { 90 final int intpart = (int) a; 91 92 if (a <= intpart 93 || (CHECK_OVERFLOW && intpart == Integer.MAX_VALUE) 94 || CHECK_NAN && Double.isNaN(a)) { 95 return intpart; 96 } 97 return intpart + 1; 98 } 99 100 /** 101 * Faster alternative to floor(float) optimized for the integer domain 102 * and supporting NaN and +/-Infinity. 103 * 104 * @param a a value. 105 * @return the largest (closest to positive infinity) floating-point value 106 * that less than or equal to the argument and is equal to a mathematical 107 * integer. 108 */ 109 public static int floor_int(final float a) { 110 final int intpart = (int) a; 111 112 if (a >= intpart 113 || (CHECK_OVERFLOW && intpart == Integer.MIN_VALUE) 114 || CHECK_NAN && Float.isNaN(a)) { 115 return intpart; 116 } 117 return intpart - 1; 118 } 119 120 /** 121 * Faster alternative to floor(double) optimized for the integer domain 122 * and supporting NaN and +/-Infinity. 123 * 124 * @param a a value. 125 * @return the largest (closest to positive infinity) floating-point value 126 * that less than or equal to the argument and is equal to a mathematical 127 * integer. 128 */ 129 public static int floor_int(final double a) { 130 final int intpart = (int) a; 131 132 if (a >= intpart 133 || (CHECK_OVERFLOW && intpart == Integer.MIN_VALUE) 134 || CHECK_NAN && Double.isNaN(a)) { 135 return intpart; 136 } 137 return intpart - 1; 138 } 139 } |