jcalc:calculations in java with open source api

13
Calculations in Java with open source API Author: Davor Sauer www.jdice.org

Upload: davor-sauer

Post on 20-Aug-2015

211 views

Category:

Software


2 download

TRANSCRIPT

Page 1: JCalc:Calculations in java with open source API

Calculations in Java with open source API

Author: Davor Sauerwww.jdice.org

Page 2: JCalc:Calculations in java with open source API

Content

Calculations in Java- basics

Calculations in Java with JCalc

Comparison of complex calculation with plain Java and JCalc

Features

Questions

Page 3: JCalc:Calculations in java with open source API
Page 4: JCalc:Calculations in java with open source API

Calculations in Java- basicsExample: 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) .9b) .90c) 0.899999999999d) None of the above

BigDecimal payment = new BigDecimal(2.00);BigDecimal cost = new BigDecimal(1.10);System.out.println(payment.subtract(cost));

Answers:a) .9b) .90c) 0.899999999999d) 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) .9b) 0.90c) 0.899999999999d) None of the above

Page 5: JCalc:Calculations in java with open source API

Java + JCalc

...more sugar?

Page 6: JCalc:Calculations in java with open source API

Calculations in Java- basicsExample: 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.9b) .90c) 0.899999999999d) 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) .9b) .90c) 0.899999999999d) 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) .9b) 0.90c) 0.899999999999d) None of the above

0.9

0.899999999999

0.89999999991118215802998747...

Page 7: JCalc:Calculations in java with open source API

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.0054166667BigDecimal 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.3333400000BigDecimal numerator = r.multiply(P);

// denominatorr = r.add(new BigDecimal(1)); // => 1.0054166667BigDecimal 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.8569752742BigDecimal denominator = new BigDecimal(1);denominator = denominator.subtract(r_pow);

// => 1083.3333400000 / 0.8569752742 = 1264.1360522464BigDecimal 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

Page 8: JCalc:Calculations in java with open source API

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 / 12Num r = Calculator.builder().openBracket().val(interestRate).div(100).closeBracket().div(12).calculate();

// N = 30 * 12 * -1Num N = Calculator.builder().val(paymentYears).mul(12).mul(-1).calculate();

// c = (r * P) / (1 / (1 + r)^NCalculator 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

Page 9: JCalc:Calculations in java with open source API

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

Page 10: JCalc:Calculations in java with open source API

Feature: Show calculation stepsNum 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.0650.065 / 12 = 0.00541666670.0054166667 * 200000 = 1083.333346.5 / 100 = 0.0650.065 / 12 = 0.00541666671 + 0.0054166667 = 1.0054166667-30 * 12 = -3601.0054166667 ^ -360 = 0.14302472581 - 0.1430247258 = 0.85697527421083.33334 / 0.8569752742 = 1264.1360522464c = 1264.14

Page 11: JCalc:Calculations in java with open source API

Feature: ModularityCalculator 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)");

@SingletonExtensionpublic class QuestionOperator implements Operator { .... // implementation for ‘?’ operator ....}

@SingletonExtensionpublic class SumFunction implements Function { .... // implementation for ‘sum’ function ....}

Page 12: JCalc:Calculations in java with open source API

Feature: Default configuration

roundingMode=HALF_UPscale=2stripTrailingZeros=truedecimalSeparator.out='.'decimalSeparator.in='.'numconverter[0]=org.jdice.calc.test.NumTest$CustomObject > org.jdice.calc.test.NumTest$CustomObjectNumConverteroperator[0]=org.jdice.calc.test.CustomOperatorFunctionTest$QuestionOperatorfunction[0]=org.jdice.calc.test.CustomOperatorFunctionTest$SumFunction

Configure default properties with 'jcalc.properties' file in class path

Page 13: JCalc:Calculations in java with open source API

Questions, ideas, suggestions…

Project page: www.jdice.org