| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
|
| 19 | |
|
| 20 | |
|
| 21 | |
|
| 22 | |
|
| 23 | |
package org.webmacro.servlet; |
| 24 | |
|
| 25 | |
import org.webmacro.Context; |
| 26 | |
import org.webmacro.ContextTool; |
| 27 | |
import org.webmacro.PropertyException; |
| 28 | |
|
| 29 | |
|
| 30 | |
|
| 31 | |
|
| 32 | |
|
| 33 | |
|
| 34 | |
|
| 35 | |
|
| 36 | |
public class MathTool extends ContextTool |
| 37 | |
{ |
| 38 | |
|
| 39 | |
|
| 40 | 41 | private static final MathTool _instance = new MathTool(); |
| 41 | |
|
| 42 | |
|
| 43 | |
|
| 44 | |
|
| 45 | |
|
| 46 | |
public static final MathTool getInstance () |
| 47 | |
{ |
| 48 | 0 | return _instance; |
| 49 | |
} |
| 50 | |
|
| 51 | |
|
| 52 | |
|
| 53 | |
|
| 54 | |
|
| 55 | |
|
| 56 | |
|
| 57 | |
public static final double PI = java.lang.Math.PI; |
| 58 | |
|
| 59 | |
|
| 60 | |
|
| 61 | |
|
| 62 | |
public static final int min (int a, int b) |
| 63 | |
{ |
| 64 | 0 | return (a < b) ? a : b; |
| 65 | |
} |
| 66 | |
|
| 67 | |
public static final long min (long a, long b) |
| 68 | |
{ |
| 69 | 0 | return (a < b) ? a : b; |
| 70 | |
} |
| 71 | |
|
| 72 | |
public static final float min (float a, float b) |
| 73 | |
{ |
| 74 | 0 | return (a < b) ? a : b; |
| 75 | |
} |
| 76 | |
|
| 77 | |
public static final double min (double a, double b) |
| 78 | |
{ |
| 79 | 0 | return (a < b) ? a : b; |
| 80 | |
} |
| 81 | |
|
| 82 | |
|
| 83 | |
|
| 84 | |
|
| 85 | |
|
| 86 | |
public static final int max (int a, int b) |
| 87 | |
{ |
| 88 | 0 | return (a > b) ? a : b; |
| 89 | |
} |
| 90 | |
|
| 91 | |
public static final long max (long a, long b) |
| 92 | |
{ |
| 93 | 0 | return (a > b) ? a : b; |
| 94 | |
} |
| 95 | |
|
| 96 | |
public static final float max (float a, float b) |
| 97 | |
{ |
| 98 | 0 | return (a > b) ? a : b; |
| 99 | |
} |
| 100 | |
|
| 101 | |
public static final double max (double a, double b) |
| 102 | |
{ |
| 103 | 0 | return (a > b) ? a : b; |
| 104 | |
} |
| 105 | |
|
| 106 | |
|
| 107 | |
|
| 108 | |
|
| 109 | |
|
| 110 | |
public static final int random (int start, int end) |
| 111 | |
{ |
| 112 | 1000000 | return start + (int) ((end - start + 1) * java.lang.Math.random()); |
| 113 | |
} |
| 114 | |
|
| 115 | |
|
| 116 | |
|
| 117 | |
|
| 118 | |
public static final int pow (int base, int power) |
| 119 | |
{ |
| 120 | 0 | return base ^ power; |
| 121 | |
} |
| 122 | |
|
| 123 | |
public static final long pow (long base, long power) |
| 124 | |
{ |
| 125 | 0 | return base ^ power; |
| 126 | |
} |
| 127 | |
|
| 128 | |
|
| 129 | |
|
| 130 | |
|
| 131 | |
|
| 132 | |
public static final int abs (int a) |
| 133 | |
{ |
| 134 | 0 | return java.lang.Math.abs(a); |
| 135 | |
} |
| 136 | |
|
| 137 | |
public static final long abs (long a) |
| 138 | |
{ |
| 139 | 0 | return java.lang.Math.abs(a); |
| 140 | |
} |
| 141 | |
|
| 142 | |
public static final float abs (float a) |
| 143 | |
{ |
| 144 | 0 | return java.lang.Math.abs(a); |
| 145 | |
} |
| 146 | |
|
| 147 | |
public static final double abs (double a) |
| 148 | |
{ |
| 149 | 0 | return java.lang.Math.abs(a); |
| 150 | |
} |
| 151 | |
|
| 152 | |
|
| 153 | |
|
| 154 | |
|
| 155 | |
|
| 156 | |
public static final int mod (int a, int b) |
| 157 | |
{ |
| 158 | 0 | return a % b; |
| 159 | |
} |
| 160 | |
|
| 161 | |
|
| 162 | |
|
| 163 | |
|
| 164 | |
|
| 165 | |
public static final double multiply (Number a, Number b) |
| 166 | |
{ |
| 167 | 0 | return a.doubleValue() * b.doubleValue(); |
| 168 | |
} |
| 169 | |
|
| 170 | |
|
| 171 | |
|
| 172 | |
|
| 173 | |
|
| 174 | |
public static final double divide (Number a, Number b) |
| 175 | |
{ |
| 176 | 0 | return a.doubleValue() / b.doubleValue(); |
| 177 | |
} |
| 178 | |
|
| 179 | |
|
| 180 | |
|
| 181 | |
|
| 182 | |
|
| 183 | |
|
| 184 | |
|
| 185 | |
public MathTool () |
| 186 | 104 | { |
| 187 | 104 | } |
| 188 | |
|
| 189 | |
|
| 190 | |
|
| 191 | |
|
| 192 | |
|
| 193 | |
public MathTool (Context context) |
| 194 | 0 | { |
| 195 | 0 | } |
| 196 | |
|
| 197 | |
|
| 198 | |
|
| 199 | |
|
| 200 | |
|
| 201 | |
|
| 202 | |
public Object init (Context context) throws PropertyException |
| 203 | |
{ |
| 204 | 0 | return MathTool.getInstance(); |
| 205 | |
} |
| 206 | |
|
| 207 | |
public static void main (String[] args) |
| 208 | |
{ |
| 209 | 0 | System.out.println("Generating 200 random ints between 10 and 99 inclusive:"); |
| 210 | 0 | for (int i = 0; i < 200; i++) |
| 211 | |
{ |
| 212 | 0 | System.out.print(MathTool.random(10, 99) + " "); |
| 213 | 0 | if (((i + 1) % 20) == 0) System.out.println(); |
| 214 | |
} |
| 215 | 0 | System.out.println("\nDone."); |
| 216 | 0 | } |
| 217 | |
} |