Skip to content

DecimalArithmetic API

Marco Terzer edited this page Mar 5, 2016 · 8 revisions

Zero Garbage with DecimalArithmetic API

The DecimalArithmetic class defines the basic primitive operations for Decimal numbers for one particular combination of scale, rounding mode and overflow mode. Primitive here means that Decimal values are simply represented by their underlying unscaled long value. All operations therefore use unscaled longs for Decimal arguments and return longs for Decimal number results.

See DecimalArithmetic javadoc.


Example: Zero Garbage with DecimalArithmetic API
public class ZeroGarbage {
	public static void main(String[] args) throws IOException {
		ScaleMetrics scale3 = Scales.getScaleMetrics(3);
		DecimalArithmetic arith = scale3.getDefaultArithmetic();
		
		long a = arith.fromLong(4);
		long b = arith.fromDouble(1.5);
		long c = arith.fromDouble(0.125);
		long d = arith.fromUnscaled(1, arith.getScale());

		System.out.println("ZERO GARBAGE: print values");
		System.out.print("a = ");arith.toString(a, System.out);
		System.out.println();
		System.out.print("b = ");arith.toString(b, System.out);
		System.out.println();
		System.out.print("c = ");arith.toString(c, System.out);
		System.out.println();
		System.out.print("d = ");arith.toString(d, System.out);
		System.out.println();
		
		System.out.println();
		System.out.println("ZERO GARBAGE: add values");
		long sumAB = arith.add(a, b);
		long sumCD = arith.add(c, d);
		System.out.print("a+b = ");arith.toString(sumAB, System.out);
		System.out.println();
		System.out.print("c+d = ");arith.toString(sumCD, System.out);
		System.out.println();
		
		System.out.println();
		System.out.println("ZERO GARBAGE: calculate average");
		long avgAB = arith.avg(a, b);
		long avgCD = arith.avg(c, d);
		long avgABCD = arith.divideByLong(arith.add(sumAB, sumCD), 4);
		System.out.print("(a+b)/2 = ");arith.toString(avgAB, System.out);
		System.out.println();
		System.out.print("(c+d)/2 = ");arith.toString(avgCD, System.out);
		System.out.println();
		System.out.print("(a+b+c+d)/4 = ");arith.toString(avgABCD, System.out);
		System.out.println();

		System.out.println();
		System.out.println("ZERO GARBAGE: round up/down");
		long avgBCdup = arith.deriveArithmetic(RoundingMode.UP).avg(b, c);
		long avgBCdown = arith.deriveArithmetic(RoundingMode.DOWN).avg(b, c);
		System.out.print("UP:   (b+c)/2 = ");arith.toString(avgBCdup, System.out);
		System.out.println();
		System.out.print("DOWN: (b+c)/2 = ");arith.toString(avgBCdown, System.out);
		System.out.println();

		System.out.println();
		System.out.println("ZERO GARBAGE: round to 2 decimal places");
		DecimalArithmetic arith2 = arith.deriveArithmetic(2);
		long rounded = arith.round(c, 2);
		long scaled = arith2.fromUnscaled(c, arith.getScale());
		System.out.print("round(c,2) = ");arith.toString(rounded, System.out);
		System.out.println();
		System.out.print("scale(c,2) = ");arith2.toString(scaled, System.out);
		System.out.println();
	}
}

will output

ZERO GARBAGE: print values
a = 4.000
b = 1.500
c = 0.125
d = 0.001

ZERO GARBAGE: add values
a+b = 5.500
c+d = 0.126

ZERO GARBAGE: calculate average
(a+b)/2 = 2.750
(c+d)/2 = 0.063
(a+b+c+d)/4 = 1.407

ZERO GARBAGE: round up/down
UP:   (b+c)/2 = 0.813
DOWN: (b+c)/2 = 0.812

ZERO GARBAGE: round to 2 decimal places
round(c,2) = 0.130
scale(c,2) = 0.13
Clone this wiki locally