1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package inc.che.common.math;
20
21
22 import inc.che.common.assertion.Assert;
23 import inc.che.common.resource.ResourceManager;
24 import inc.che.common.resource.StringResources;
25
26 import org.apache.log4j.Logger;
27
28 /***
29 * <b>Utilitymethoden für mathematische Probleme</b>
30 * @version $Id: MathUtil.java,v 1.1 2005/03/06 12:57:00 stevemcmee Exp $
31 * @author <address> Steve McMee <stevemcmee@users.sourceforge.net></address>
32 */
33
34 public final class MathUtil {
35
36 /*** CVS ID of this file */
37 public static final String CVS_ID =
38 "$Id: MathUtil.java,v 1.1 2005/03/06 12:57:00 stevemcmee Exp $";
39
40 /***
41 * logger instance for this class
42 */
43
44 private static Logger log = Logger.getLogger(MathUtil.class);
45
46 /***
47 * The ResourceManager
48 */
49 private static ResourceManager resourceManager =
50 ResourceManager.getResourceManager(StringResources.TEXT_RESOURCES);
51 /***
52 * forbidden constructor
53 */
54 private MathUtil() {
55 }
56 /***
57 * konvertiert eine Zahl in Dezimalsystem in ein beliebiges Zahlensystem
58 * @param number Zahl im Dezimalsystem
59 * @param base Basis des Zielzahlensystems
60 * @param lenght Länge des resultats
61 * @return Darstellung von number implements Zahlensystem base
62 */
63 public static String toNumberBase(int number, int base, int length) {
64 if (log.isDebugEnabled()) {
65 log.debug("toNumberBase(" + number + "," + base + ")");
66 }
67 Assert._assert(base > 1, "base must be > 1");
68 Assert._assert(base <= 16, "base must be <= 16");
69 StringBuffer result = new StringBuffer();
70 int upper = (int) (Math.log(number) / Math.log(base));
71 int div = 0;
72 for (int i = upper + 1; i >= 0; i--) {
73 div = number / (int) power(base, i);
74 switch (div) {
75 case 0 :
76 result.append('0');
77 break;
78 case 1 :
79 result.append('1');
80 break;
81 case 2 :
82 result.append('2');
83 break;
84 case 3 :
85 result.append('3');
86 break;
87 case 4 :
88 result.append('4');
89 break;
90 case 5 :
91 result.append('5');
92 break;
93 case 6 :
94 result.append('6');
95 break;
96 case 7 :
97 result.append('7');
98 break;
99 case 8 :
100 result.append('8');
101 break;
102 case 9 :
103 result.append('9');
104 break;
105 case 10 :
106 result.append('A');
107 break;
108 case 11 :
109 result.append('B');
110 break;
111 case 12 :
112 result.append('C');
113 break;
114 case 13 :
115 result.append('D');
116 break;
117 case 14 :
118 result.append('E');
119 break;
120 case 15 :
121 result.append('F');
122 break;
123 default:
124 break;
125 }
126 number = number - div * (int) power(base, i);
127 }
128 result.toString();
129 int len = result.length();
130 if (len > length) {
131 result = result.delete(0, result.length() - length);
132 }
133 if (len < length) {
134 for (int i = 0; i < length - len; i++) {
135 result.insert(0, '0');
136 }
137 }
138 Assert._assert(result.length() == length, "length !=" + length);
139
140 return result.toString();
141 }
142 /***
143 * konvertiert eine Zahl im beliebigen Zahlensystem ins Dezimalsystem
144 * @param number Darstellung der Zahl
145 * @param base Basis des Ausgangszahlensystems
146 * @return Dezimalwert von number
147 */
148 public static int toDecBase(String number, int base) {
149 if (log.isDebugEnabled()) {
150 log.debug("toDecBase(" + number + "," + base + ")");
151 }
152 Assert._assert(base > 1, "base must be > 1");
153 Assert._assert(base <= 16, "base must be <= 16");
154 int erg = 0;
155 int len = number.length();
156 for (int i = 0; i < len; i++) {
157 switch (number.charAt(len - 1 - i)) {
158 case '0' :
159 break;
160 case '1' :
161 erg = erg + (int) power(base, i);
162 break;
163 case '2' :
164 erg = erg + 2 * (int) power(base, i);
165 break;
166 case '3' :
167 erg = erg + 3 * (int) power(base, i);
168 break;
169 case '4' :
170 erg = erg + 4 * (int) power(base, i);
171 break;
172 case '5' :
173 erg = erg + 5 * (int) power(base, i);
174 break;
175 case '6' :
176 erg = erg + 6 * (int) power(base, i);
177 break;
178 case '7' :
179 erg = erg + 7 * (int) power(base, i);
180 break;
181 case '8' :
182 erg = erg + 8 * (int) power(base, i);
183 break;
184 case '9' :
185 erg = erg + 9 * (int) power(base, i);
186 break;
187 case 'a' :
188 case 'A' :
189 erg = erg + 10 * (int) power(base, i);
190 break;
191 case 'b' :
192 case 'B' :
193 erg = erg + 11 * (int) power(base, i);
194 break;
195 case 'c' :
196 case 'C' :
197 erg = erg + 12 * (int) power(base, i);
198 break;
199 case 'd' :
200 case 'D' :
201 erg = erg + 13 * (int) power(base, i);
202 break;
203 case 'e' :
204 case 'E' :
205 erg = erg + 14 * (int) power(base, i);
206 break;
207 case 'f' :
208 case 'F' :
209 erg = erg + 15 * (int) power(base, i);
210 break;
211 default :
212 throw new RuntimeException(
213 resourceManager.getText(
214 "proper_number_false",
215 new String[] {
216 String.valueOf(number),
217 String.valueOf(number.charAt(len - 1 - i))}));
218 }
219 }
220
221 return erg;
222 }
223
224 /***
225 * berechnet die Potenz zweier Zahlen
226 * @param base Basis
227 * @param exp Exponent
228 * @return base^exp
229 */
230 public static double power(double base, int exp) {
231 if (log.isDebugEnabled()) {
232 log.debug("power(" + base + "," + exp + ")");
233 }
234 double erg = 1;
235 for (int i = 0; i < exp; i++) {
236 erg = erg * base;
237 }
238 return erg;
239 }
240 }