Skip to content

Examples

Marco Terzer edited this page Jun 14, 2017 · 27 revisions

Table of Contents


Example 1: Interest Calculation

public class Interest {
	public static void main(String[] args) {
		Decimal2f principal = Decimal2f.valueOf(9500);
		Decimal3f rate = Decimal3f.valueOf(0.067);
		Decimal2f time = Decimal2f.valueOf(1).divide(4);
		Decimal2f interest = principal.multiplyBy(rate.multiplyExact(time));
		System.out.println("First quarter interest:  $" + interest);
	}
}

will output

First quarter interest: $159.13

Example 2: Circumference of a Circle

public class Circle {
	public static void main(String[] args) {
		Decimal18f PI = Decimal18f.valueOf(Math.PI);
		Decimal2f radius = Decimal2f.valueOf(5);
		Decimal2f circ = radius.multiplyBy(PI.multiply(2));
		System.out.println("Circumference with 5m radius is ~" + circ + "m");
		System.out.println("Circumference with 5m radius is ~" + (2*Math.PI * 5) + "m");

		Decimal2f down = radius.multiplyBy(PI.multiply(2), RoundingMode.DOWN);
		System.out.println("Circumference with 5m radius is larger than " + down + "m");
	}
}

will output

Circumference with 5m radius is ~31.42m
Circumference with 5m radius is ~31.41592653589793m
Circumference with 5m radius is larger than 31.41m

Example 3: Mean and Standard Deviation with MutableDecimal

public class MeanStdDev {
	/* Random generator*/
	private static final Random RND = new Random();
	/* Sample count*/
	private static final int N = 10000;
	/* Expected sample mean*/
	private static final Decimal10f EXP_MEAN = Decimal10f.valueOf(0);
	
	public static void main(String[] args) {
		MutableDecimal10f mean = new MutableDecimal10f();
		MutableDecimal10f var = new MutableDecimal10f();
		for (int i = 0; i < N; i++) {
			double value = RND.nextGaussian();
			mean.add(value);
			var.addSquared(EXP_MEAN.subtract(value));
		}
		System.out.println("Mean: " + mean.divide(N));
		System.out.println("Variance: " + var.divide(N));
		System.out.println("StdDev: " + var.sqrt());
	}
}

will output something like

Mean: 0.0111499553
Variance: 1.0109666338
StdDev: 1.0054683654

Example 4: Addition

public class Add {
	public static void main(String[] args) {
		Decimal2f a = Decimal2f.valueOf(4);
		Decimal2f b = Decimal2f.valueOf(1.5);
		Decimal3f c = Decimal3f.valueOf(0.125);
		Decimal3f d = Decimal3f.valueOf(0.5);

		System.out.println("ADD: values with same scale");
		Decimal2f sumAB = a.add(b);
		Decimal3f sumCD = c.add(d);
		System.out.println("a+b = " + sumAB);
		System.out.println("c+d = " + sumCD);
		
		System.out.println("ADD: values with different scales");
		Decimal2f sumAC = a.add(c, RoundingMode.HALF_UP);
		Decimal3f sumCA = c.add(a, RoundingMode.UNNECESSARY);
		System.out.println("a+c = " + sumAC);
		System.out.println("c+a = " + sumCA);
	}
}

will output

ADD: values with same scale
a+b = 5.50
c+d = 0.625
ADD: values with different scales
a+c = 4.13
c+a = 4.125

Example 5: Multiplication

public class Multiply {
	public static void main(String[] args) {
		Decimal2f a = Decimal2f.valueOf(4);
		Decimal2f b = Decimal2f.valueOf(1.5);
		Decimal3f c = Decimal3f.valueOf(0.125);
		Decimal3f d = Decimal3f.valueOf(0.5);
		
		System.out.println("MULTIPLY: values with same scale");
		Decimal2f productAB = a.multiply(b);
		Decimal3f productCD = c.multiply(d);
		Decimal3f downCD = c.multiply(d, RoundingMode.DOWN);
		System.out.println("a*b = " + productAB);
		System.out.println("c*d = " + productCD);
		System.out.println("c*d = " + downCD);

		System.out.println("MULTIPLY: values with different scales");
		Decimal2f productAC = a.multiplyBy(c);
		Decimal2f productBC = b.multiplyBy(c);
		Decimal2f productBD = b.multiplyBy(d);
		Decimal3f productCA = c.multiplyBy(a);
		System.out.println("a*c = " + productAC);
		System.out.println("b*c = " + productBC);
		System.out.println("b*d = " + productBD);
		System.out.println("c*a = " + productCA);

		System.out.println("MULTIPLY: exact multiplication");
		Decimal<?> exactCD = c.multiplyExact(d);
		Decimal6f typedCD = c.multiplyExact().by(d);
		Decimal5f typedAD = a.multiplyExact().by(d);
		System.out.println("c*d = " + exactCD);
		System.out.println("c*d = " + typedCD);
		System.out.println("a*d = " + typedAD);
	}
}

will output

MULTIPLY: values with same scale
a*b = 6.00
c*d = 0.063
c*d = 0.062
MULTIPLY: values with different scales
a*c = 0.50
b*c = 0.19
b*d = 0.75
c*a = 0.500
MULTIPLY: exact multiplication
c*d = 0.062500
c*d = 0.062500
a*d = 2.00000

Example 6: Square Root of Two

public class Sqrt {
	public static void main(String[] args) {
		System.out.println("sqrt(2)=" + Decimal18f.TWO.sqrt());
		System.out.println("sqrt(2)=" + Math.sqrt(2));
	}
}

will output

sqrt(2)=1.414213562373095049
sqrt(2)=1.4142135623730951

Example 7: Gauss–Legendre to Calculate Digits of PI

public class Pi {
	
	public static void main(String[] args) {
		Decimal17f a = Decimal17f.ONE;
		Decimal17f b = Decimal17f.TWO.sqrt().shiftRight(1);//1/sqrt(2) == sqrt(2)/2
		Decimal17f t = Decimal17f.ONE.divide(4);
		Decimal17f p = Decimal17f.ONE;
		for (int i = 0; i < 3; i++) {
			System.out.println("step " + (i+1) + ": " + pi(a, b, t, p));
			Decimal17f an = a.avg(b);
			Decimal17f bn = a.multiply(b).sqrt();
			Decimal17f tn = t.subtract(p.multiply(a.subtract(an).square()));
			Decimal17f pn = p.shiftLeft(1);
			a = an;
			b = bn;
			t = tn;
			p = pn;
		}
		System.out.println("RESULT: " + pi(a, b, t, p));
		System.out.println("DOUBLE: " + Math.PI);
	}
	private static Decimal<?> pi(Decimal17f a, Decimal17f b, Decimal17f t, Decimal17f p) {
		return a.add(b).square().divide(t).shiftRight(2);
	}
}

will output

step 1: 2.91421356237309503
step 2: 3.14057925052216826
step 3: 3.14159264621354222
RESULT: 3.14159265358979315
DOUBLE: 3.141592653589793

Example 8: Zero Garbage with DecimalArithmetic API

See DecimalArithmetic API