1 3 4 package gnu.kawa.functions; 5 import gnu.math.*; 6 import java.math.*; 7 8 public class Arithmetic 9 { 10 11 public static final int INT_CODE = 1; 12 13 public static final int LONG_CODE = 2; 14 15 public static final int BIGINTEGER_CODE = 3; 16 17 public static final int INTNUM_CODE = 4; 18 19 public static final int BIGDECIMAL_CODE = 5; 20 21 public static final int RATNUM_CODE = 6; 22 23 public static final int FLOAT_CODE = 7; 24 25 public static final int DOUBLE_CODE = 8; 26 27 public static final int FLONUM_CODE = 9; 28 29 public static final int NUMERIC_CODE = 10; 30 31 public static int classifyValue (Object value) 32 { 33 if (value instanceof Numeric) 34 { 35 if (value instanceof IntNum) 36 return INTNUM_CODE; 37 else if (value instanceof RatNum) 38 return RATNUM_CODE; 39 else if (value instanceof DFloNum) 40 return FLONUM_CODE; 41 else 42 return NUMERIC_CODE; 43 } 44 else if (value instanceof Number ) 45 { 46 if (value instanceof Integer || value instanceof Short 47 || value instanceof Byte ) 48 return INT_CODE; 49 else if (value instanceof Long ) 50 return LONG_CODE; 51 else if (value instanceof Float ) 52 return FLOAT_CODE; 53 else if (value instanceof Double ) 54 return DOUBLE_CODE; 55 else if (value instanceof BigInteger) 56 return BIGINTEGER_CODE; 57 else if (value instanceof BigDecimal) 58 return BIGDECIMAL_CODE; 59 else 60 return -1; 61 } 62 else 63 return -1; 64 } 65 66 public static int asInt (Object value) 67 { 68 return ((Number ) value).intValue(); 69 } 70 71 public static long asLong (Object value) 72 { 73 return ((Number ) value).longValue(); 74 } 75 76 public static float asFloat (Object value) 77 { 78 return ((Number ) value).floatValue(); 79 } 80 81 public static double asDouble (Object value) 82 { 83 return ((Number ) value).doubleValue(); 84 } 85 86 public static BigInteger asBigInteger (Object value) 87 { 88 if (value instanceof BigInteger) 89 return (BigInteger) value; 90 if (value instanceof IntNum) 91 return new BigInteger(value.toString()); 92 return BigInteger.valueOf(((Number ) value).longValue()); 93 } 94 95 public static IntNum asIntNum (BigDecimal value) 96 { 97 return IntNum.valueOf(((BigDecimal) value).toBigInteger().toString(), 10); 98 } 99 100 public static IntNum asIntNum (BigInteger value) 101 { 102 return IntNum.valueOf(value.toString(), 10); 103 } 104 105 public static IntNum asIntNum (Object value) 106 { 107 if (value instanceof IntNum) 108 return (IntNum) value; 109 if (value instanceof BigInteger) 110 return IntNum.valueOf(value.toString(), 10); 111 if (value instanceof BigDecimal) 112 return asIntNum((BigDecimal) value); 113 return IntNum.make(((Number ) value).longValue()); 114 } 115 116 public static BigDecimal asBigDecimal (Object value) 117 { 118 if (value instanceof BigDecimal) 119 return (BigDecimal) value; 120 if (value instanceof BigInteger) 121 return new BigDecimal((BigInteger) value); 122 if (value instanceof Long || value instanceof Integer 123 || value instanceof Short || value instanceof Byte ) 124 return BigDecimal.valueOf(((Number ) value).longValue()); 125 return new BigDecimal(value.toString()); 126 } 127 128 public static final IntNum ten_exp_9 = IntNum.make(1000000000); 129 130 public static RatNum asRatNum (Object value) 131 { 132 if (value instanceof RatNum) 133 return (RatNum) value; 134 if (value instanceof BigInteger) 135 return IntNum.valueOf(value.toString(), 10); 136 if (value instanceof BigDecimal) 137 { 138 BigDecimal d = (BigDecimal) value; 139 RatNum v = IntNum.valueOf(d.unscaledValue().toString(), 10); 140 int scale = d.scale(); 141 for (; scale >= 9; scale -= 9) 142 v = RatNum.divide(v, ten_exp_9); 143 for (; scale <= -9; scale += 9) 144 v = RatNum.times(v, ten_exp_9); 145 IntNum scaleVal; 146 switch (scale > 0 ? scale : -scale) 147 { 148 case 1: scaleVal = IntNum.make(10); break; 149 case 2: scaleVal = IntNum.make(100); break; 150 case 3: scaleVal = IntNum.make(1000); break; 151 case 4: scaleVal = IntNum.make(10000); break; 152 case 5: scaleVal = IntNum.make(100000); break; 153 case 6: scaleVal = IntNum.make(1000000); break; 154 case 7: scaleVal = IntNum.make(10000000); break; 155 case 8: scaleVal = IntNum.make(100000000); break; 156 default: 157 return v; 158 } 159 if (scale > 0) 160 return RatNum.divide(v, scaleVal); 161 else 162 return RatNum.times(v, scaleVal); 163 } 164 else 165 return IntNum.make(((Number ) value).longValue()); 166 } 167 168 public static Numeric asNumeric (Object value) 169 { 170 if (! (value instanceof Numeric)) 171 { 172 if (value instanceof BigInteger || value instanceof Long 173 || value instanceof Short || value instanceof Byte 174 || value instanceof Integer ) 175 return asIntNum(value); 176 if (value instanceof BigDecimal) 177 return asRatNum(value); 178 if (value instanceof Float || value instanceof Double ) 179 return new DFloNum(asDouble(value)); 180 } 181 return (Numeric) value; 182 } 183 184 188 public static String toString (Object number, int radix) 189 { 190 int code = Arithmetic.classifyValue(number); 191 switch (code) 192 { 193 case Arithmetic.INT_CODE: 194 return Integer.toString(Arithmetic.asInt(number), radix); 195 case Arithmetic.LONG_CODE: 196 return Long.toString(Arithmetic.asLong(number), radix); 197 case Arithmetic.BIGINTEGER_CODE: 198 return Arithmetic.asBigInteger(number).toString(radix); 199 case Arithmetic.INTNUM_CODE: 200 return Arithmetic.asIntNum(number).toString(radix); 201 case Arithmetic.BIGDECIMAL_CODE: 202 if (radix == 10) 203 return Arithmetic.asBigDecimal(number).toString(); 204 case Arithmetic.FLOAT_CODE: 206 if (radix == 10) 207 return Float.toString(Arithmetic.asFloat(number)); 208 case Arithmetic.DOUBLE_CODE: 210 case Arithmetic.FLONUM_CODE: 211 if (radix == 10) 212 return Double.toString(Arithmetic.asDouble(number)); 213 case Arithmetic.RATNUM_CODE: 215 default: 216 return Arithmetic.asNumeric(number).toString(radix); 217 } 218 } 219 220 224 public static Object convert (Object value, int code) 225 { 226 switch (code) 227 { 228 case Arithmetic.INT_CODE: 229 if (value instanceof Integer ) 230 return value; 231 int i = ((Number ) value).intValue(); 232 233 235 return new Integer (i); 236 237 case Arithmetic.LONG_CODE: 238 if (value instanceof Long ) 239 return value; 240 long l = ((Number ) value).longValue(); 241 242 244 return new Long (l); 245 246 case Arithmetic.BIGINTEGER_CODE: 247 return Arithmetic.asBigInteger(value); 248 case Arithmetic.INTNUM_CODE: 249 return Arithmetic.asIntNum(value); 250 case Arithmetic.BIGDECIMAL_CODE: 251 return Arithmetic.asBigDecimal(value); 252 case Arithmetic.RATNUM_CODE: 253 return Arithmetic.asRatNum(value); 254 case Arithmetic.FLOAT_CODE: 255 if (value instanceof Float ) 256 return value; 257 float f = Arithmetic.asFloat(value); 258 259 261 return new Float (f); 262 263 case Arithmetic.DOUBLE_CODE: 264 if (value instanceof Double ) 265 return value; 266 double d = Arithmetic.asDouble(value); 267 268 270 return new Double (d); 271 272 case Arithmetic.FLONUM_CODE: 273 if (value instanceof DFloNum) 274 return value; 275 return DFloNum.make(Arithmetic.asDouble(value)); 276 default: 277 return (Number ) value; 278 } 279 } 280 } 281 | Popular Tags |