Methods Summary |
---|
public java.lang.Object | clone()
//JMH TBD
return null;
|
private java.lang.String | format(double f, int MaxDen)Returns a fractional string representation of a double to a maximum denominator size
This code has been translated to java from the following web page.
http://www.codeproject.com/cpp/fraction.asp
Originally coded in c++ By Dean Wyant dwyant@mindspring.com
The code on the web page is freely available.
long Whole = (long)f;
int sign = 1;
if (f < 0) {
sign = -1;
}
double Precision = 0.00001;
double AllowedError = Precision;
double d = Math.abs(f);
d -= Whole;
double Frac = d;
double Diff = Frac;
long Num = 1;
long Den = 0;
long A = 0;
long B = 0;
long i = 0;
if (Frac > Precision) {
while (true) {
d = 1.0 / d;
i = (long) (d + Precision);
d -= i;
if (A > 0) {
Num = i * Num + B;
}
Den = (long) (Num / Frac + 0.5);
Diff = Math.abs((double) Num / Den - Frac);
if (Den > MaxDen) {
if (A > 0) {
Num = A;
Den = (long) (Num / Frac + 0.5);
Diff = Math.abs((double) Num / Den - Frac);
} else {
Den = MaxDen;
Num = 1;
Diff = Math.abs((double) Num / Den - Frac);
if (Diff > Frac) {
Num = 0;
Den = 1;
// Keeps final check below from adding 1 and keeps Den from being 0
Diff = Frac;
}
}
break;
}
if ((Diff <= AllowedError) || (d < Precision)) {
break;
}
Precision = AllowedError / Diff;
// This calcualtion of Precision does not always provide results within
// Allowed Error. It compensates for loss of significant digits that occurs.
// It helps to round the inprecise reciprocal values to i.
B = A;
A = Num;
}
}
if (Num == Den) {
Whole++;
Num = 0;
Den = 0;
} else if (Den == 0) {
Num = 0;
}
if (sign < 0) {
if (Whole == 0) {
Num = -Num;
} else {
Whole = -Whole;
}
}
return new StringBuffer().append(Whole).append(" ").append(Num).append("/").append(Den).toString();
|
public final java.lang.String | format(double val)
if (mode == ONE_DIGIT) {
return format(val, 9);
} else if (mode == TWO_DIGIT) {
return format(val, 99);
} else if (mode == THREE_DIGIT) {
return format(val, 999);
} else if (mode == UNITS) {
return formatUnit(val , units);
}
throw new RuntimeException("Unexpected Case");
|
public java.lang.StringBuffer | format(java.lang.Object obj, java.lang.StringBuffer toAppendTo, java.text.FieldPosition pos)
if (obj instanceof Number) {
toAppendTo.append(format(((Number)obj).doubleValue()));
return toAppendTo;
}
else throw new IllegalArgumentException("Can only handle Numbers");
|
private java.lang.String | formatUnit(double f, int units)This method formats the double in the units specified.
The usints could be any number but in this current implementation it is
halves (2), quaters (4), eigths (8) etc
long Whole = (long)f;
f -= Whole;
long Num = Math.round(f * units);
return new StringBuffer().append(Whole).append(" ").append(Num).append("/").append(units).toString();
|
public java.lang.Object | parseObject(java.lang.String source, java.text.ParsePosition status)
//JMH TBD
return null;
|
public java.lang.Object | parseObject(java.lang.String source)
//JMH TBD
return null;
|