Upcoming SlideShare
×

# JCalc:Calculations in java with open source API

537 views

Published on

Introduction of JCalc (www.jdice.org) open source library for easier writing formula and calculations in Java. Presented at JavaCro'14, 2014/05/12.

Published in: Software, Technology, Education
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

• Be the first to like this

### JCalc:Calculations in java with open source API

1. 1. Calculations in Java with open source API Author: Davor Sauer www.jdice.org
2. 2. Content  Calculations in Java- basics  Calculations in Java with JCalc  Comparison of complex calculation with plain Java and JCalc  Features  Questions
3. 3. Calculations in Java- basics Example: Simple calculation: If you pay \$2.00 for a beer that costs \$1.10, how much changes do you get? System.out.println(2.00 - 1.10); Answers: a) .9 b) .90 c) 0.899999999999 d) None of the above BigDecimal payment = new BigDecimal(2.00); BigDecimal cost = new BigDecimal(1.10); System.out.println(payment.subtract(cost)); Answers: a) .9 b) .90 c) 0.899999999999 d) None of the above => 0.89999999991118215802998747... BigDecimal payment = new BigDecimal("2.00"); BigDecimal cost = new BigDecimal("1.10"); System.out.println(payment.subtract(cost)); Answers: a) .9 b) 0.90 c) 0.899999999999 d) None of the above
4. 4. Java + JCalc ...more sugar?
5. 5. Calculations in Java- basics Example: Simple calculation: If you pay \$2.00 for a beer that costs \$1.10, how much changes do you get? System.out.println(Calculator.builder().val(2.00).sub(1.10).calculate()); Answers: a) 0.9 b) .90 c) 0.899999999999 d) None of the aboveNum p = new Num(2.00); Num c = new Num(1.10); System.out.println(Calculator.builder().val(p).sub(c).calculate()); Answers: a) .9 b) .90 c) 0.899999999999 d) None of the above Num p = new Num("2.00"); Num c = new Num("1.10"); System.out.println(Calculator.builder().val(p).sub(c).setStripTrailingZeros(false).calculate()); System.out.println(Calculator.builder("2.00 - 1.10").setStripTrailingZeros(false).calculate()); Answers: a) .9 b) 0.90 c) 0.899999999999 d) None of the above 0.9 0.899999999999 0.89999999991118215802998747...
6. 6. Complex example  Calculate fixed monthly payment for a fixed rate mortgage by Java BigDecimal interestRate = new BigDecimal("6.5"); // fixed yearly interest rate in % BigDecimal P = new BigDecimal(200000); BigDecimal paymentYears = new BigDecimal(30); // monthly interest rate => 6.5 / 12 / 100 = 0.0054166667 BigDecimal r = interestRate.divide(new BigDecimal(12), 10, BigDecimal.ROUND_HALF_UP).divide(new BigDecimal(100), 10, BigDecimal.ROUND_HALF_UP); // numerator // => 0.005416666 * 200000 = 1083.3333400000 BigDecimal numerator = r.multiply(P); // denominator r = r.add(new BigDecimal(1)); // => 1.0054166667 BigDecimal pow = new BigDecimal(30 * 12); // N = 30 * 12 // => 1.0054166667 ^ (-30 * 12) ===> 1 / 1.005416666 ^ (30 * 12) BigDecimal one = BigDecimal.ONE; BigDecimal r_pow = r.pow(pow.intValue()); // => 1.0054166667 ^ 360 = 6.99179805731691416804.... r_pow = one.divide(r_pow, 10, BigDecimal.ROUND_HALF_UP); // => 1 / 6.991798.. = 0.1430247258 // => 1 - 0.1430247258 = 0.8569752742 BigDecimal denominator = new BigDecimal(1); denominator = denominator.subtract(r_pow); // => 1083.3333400000 / 0.8569752742 = 1264.1360522464 BigDecimal c = numerator.divide(denominator, 10, BigDecimal.ROUND_HALF_UP); c = c.setScale(2, BigDecimal.ROUND_HALF_UP); System.out.println("c = " + c);  Line of code: 15
7. 7. Complex example  Calculate fixed monthly payment for a fixed rate mortgage by Java Num interestRate = new Num(6.5); // fixed yearly interest rate in % Num P = new Num(200000); Num paymentYears = new Num(30); // monthly interest rate : r = 6.5 / 100 / 12 Num r = Calculator.builder().openBracket().val(interestRate).div(100).closeBracket().div(12).calculate(); // N = 30 * 12 * -1 Num N = Calculator.builder().val(paymentYears).mul(12).mul(-1).calculate(); // c = (r * P) / (1 / (1 + r)^N Calculator c = new Calculator() .openBracket() .val(r).mul(P) .closeBracket() // numerator .div() // --------------- .openBracket() // denumerator .val(1).sub().openBracket().val(1).add(r).closeBracket().pow(N) .closeBracket(); Num result = c.calculate().setScale(2); System.out.println("c = " + result);  Line of code: 8
8. 8. Complex example  Calculate fixed monthly payment for a fixed rate mortgage by Java Num interestRate = new Num("A", 6.5); Num P = new Num("B", 200000); Num paymentYears = new Num("C", -30); Calculator c = Calculator.builder("((A / 100 / 12) * B) / (1 - ((1 + (A / 100 / 12)) ^ (C * 12)))", interestRate, P, paymentYears); Num result = c.calculate(); System.out.println("c = " + result.setScale(2));  Line of code: 6
9. 9. Feature: Show calculation steps Num interestRate = new Num("A", 6.5); Num P = new Num("B", 200000); Num paymentYears = new Num("C", -30); Calculator c = Calculator.builder("((A / 100 / 12) * B) / (1 - ((1 + (A / 100 / 12)) ^ (C * 12)))", interestRate, P, paymentYears); c.setScale(10); c.setTracingSteps(true); // track calculation steps Num result = c.calculate(); for(Step step : c.getTracedSteps()) System.out.println(step); System.out.println("c = " + result.setScale(2)); Output: 6.5 / 100 = 0.065 0.065/ 12 = 0.0054166667 0.0054166667 * 200000 = 1083.33334 6.5 / 100 = 0.065 0.065/ 12 = 0.0054166667 1 + 0.0054166667 = 1.0054166667 -30 * 12 = -360 1.0054166667 ^ -360 = 0.1430247258 1 - 0.1430247258 = 0.8569752742 1083.33334/ 0.8569752742 = 1264.1360522464 c = 1264.14
10. 10. Feature: Modularity Calculator calc = new Calculator(); calc.use(QuestionOperator.class); // use custom operator '?' calc.use(SumFunction.class); // use custom function 'sum' calc.expression("2 ? 2 + 5 - 1 + sum(1,2,3,4)"); @SingletonExtension public class QuestionOperator implements Operator { .... // implementation for ‘?’ operator .... } @SingletonExtension public class SumFunction implements Function { .... // implementation for ‘sum’ function .... }
11. 11. Feature: Default configuration roundingMode=HALF_UP scale=2 stripTrailingZeros=true decimalSeparator.out='.' decimalSeparator.in='.' numconverter[0]=org.jdice.calc.test.NumTest\$CustomObject > org.jdice.calc.test.NumTest\$CustomObjectNumConverter operator[0]=org.jdice.calc.test.CustomOperatorFunctionTest\$QuestionOperator function[0]=org.jdice.calc.test.CustomOperatorFunctionTest\$SumFunction Configure default properties with 'jcalc.properties' file in class path
12. 12. Questions, ideas, suggestions… Project page: www.jdice.org