if (isZero()) return zero(); if (other.isZero()) throw new UcumException("Attempt to divide "+toString()+" by zero"); tens[0] = stringAddition(stringMultiply('0', s.length()), s); for (int i = 1; i < 10; i++) tens[i] = stringAddition(tens[i-1], s); String v = digits; String r = ""; while (!(handled && ((l > m) || ((vi >= v.length()) && ((Utilities.noString(w) || allZeros(w, 0))))))) { l++; handled = true; if (tens[i].compareTo(w) <= 0) { proc = true; r = r + cdig(i+1); w = trimLeadingZeros(stringSubtraction(w, tens[i])); if (!(handled && ((l > m) || ((vi >= v.length()) && ((Utilities.noString(w) || allZeros(w, 0))))))) { if (vi < v.length()) { w = w + v.charAt(vi); w = delete(w, 0, 1); r = r + "0"; if (!(handled && ((l > m) || ((vi >= v.length()) && ((Utilities.noString(w) || allZeros(w, 0))))))) { if (vi < v.length()) { w = w + v.charAt(vi);
public static Decimal one() { try { return new Decimal("1"); } catch (Exception e) { return null; // won't happen } }
if (isZero() || other.isZero()) return zero(); String s1 = stringMultiply('0', max - decimal+1) + digits; String s2 = stringMultiply('0', max - other.decimal+1) + other.digits; if (s1.length() < s2.length()) s1 = s1 + stringMultiply('0', s2.length() - s1.length()); else if (s2.length() < s1.length()) s2 = s2 + stringMultiply('0', s1.length() - s2.length()); s[i] = stringMultiply('0', s2.length()-(i+1)); int c = 0; for (int j = s1.length() - 1; j >= 0; j--) { t = c + (dig(s1.charAt(j)) * dig(s2.charAt(i))); s[i] = insert(String.valueOf(cdig(t % 10)), s[i], 0); c = t / 10; s[i] = insert(String.valueOf(cdig(t % 10)), s[i], 0); c = t / 10; t = Math.max(t, sv.length()); for (int i = 0; i < s.length; i++) s[i] = stringMultiply('0', t-s[i].length())+s[i]; for (int i = t - 1; i>= 0; i--) { for (int j = 0; j < s.length; j++) c = c + dig(s[j].charAt(i)); res = insert(String.valueOf(cdig(c %10)), res, 0); c = c / 10;
public int asInteger() throws UcumException { if (!isWholeNumber()) throw new UcumException("Unable to represent "+toString()+" as an integer"); if (comparesTo(new Decimal(Integer.MIN_VALUE)) < 0) throw new UcumException("Unable to represent "+toString()+" as a signed 8 byte integer"); if (comparesTo(new Decimal(Integer.MAX_VALUE)) > 0) throw new UcumException("Unable to represent "+toString()+" as a signed 8 byte integer"); return Integer.parseInt(asDecimal()); }
private void testString(String s, String st, String std) throws UcumException { Decimal dec = new Decimal(s); String s1 = dec.toString(); String s2 = dec.asScientific(); check(s1.equals(st), "decimal: expected "+st+" but got "+s1); check(s2.equals(std), "scientific: expected "+std+" but got "+s2); dec = new Decimal(std); s1 = dec.asDecimal(); check(s1.equals(st), "decimal(2): expected "+st+" but got "+s1); }
private Decimal doSubtract(Decimal other) { int max = Math.max(decimal, other.decimal); String s1 = stringMultiply('0', max - decimal+1) + digits; String s2 = stringMultiply('0', max - other.decimal+1) + other.digits; if (s1.length() < s2.length()) s1 = s1 + stringMultiply('0', s2.length() - s1.length()); else if (s2.length() < s1.length()) s2 = s2 + stringMultiply('0', s1.length() - s2.length()); s3 = stringSubtraction(s1, s2); s3 = delete(s3, 0, 1); if (max != s3.length()) { if (max < 0) throw new Error("Unhandled"); else if (max < s3.length()) s3 = insert(".", s3, max); else throw new Error("Unhandled"); Decimal result = new Decimal(); try { result.setValueDecimal(s3); } catch (Exception e) {
private Decimal doAdd(Decimal other) { int max = Math.max(decimal, other.decimal); String s1 = stringMultiply('0', max - decimal+1) + digits; String s2 = stringMultiply('0', max - other.decimal+1) + other.digits; if (s1.length() < s2.length()) s1 = s1 + stringMultiply('0', s2.length() - s1.length()); else if (s2.length() < s1.length()) s2 = s2 + stringMultiply('0', s1.length() - s2.length()); String s3 = stringAddition(s1, s2); s3 = delete(s3, 0, 1); throw new Error("Unhandled"); else if (max < s3.length()) s3 = insert(".", s3, max); else throw new Error("Unhandled"); Decimal result = new Decimal(); try { result.setValueDecimal(s3); } catch (Exception e) {
private void runConversionCase(XmlPullParser xpp) throws XmlPullParserException, IOException, UcumException { String id = xpp.getAttributeValue(null, "id"); String value = xpp.getAttributeValue(null, "value"); String srcUnit = xpp.getAttributeValue(null, "srcUnit"); String dstUnit = xpp.getAttributeValue(null, "dstUnit"); String outcome = xpp.getAttributeValue(null, "outcome"); System.out.println("case "+id+": "+value+" "+srcUnit+" -> "+outcome+" "+dstUnit); Decimal res = ucumSvc.convert(new Decimal(value), srcUnit, dstUnit); debug("Convert Test "+id+": the value '"+value+" "+srcUnit+"' ==> "+res.toString()+" "+dstUnit); // if (!res.toPlainString().equals(outcome)) { - that assumes that we can get the precision right, which we can't if (res.comparesTo(new Decimal(outcome)) != 0) { errCount++; System.err.println("Test "+id+": The value '"+outcome+"' was expected the result was "+res.toString()); } while (xpp.getEventType() != XmlPullParser.END_TAG) xpp.next(); xpp.next(); }
@Override public String toString() { return asDecimal(); }