From 42cedf7c439921b89ddbb732905ecb6f47be0db9 Mon Sep 17 00:00:00 2001 From: Arthur Drobot Date: Wed, 26 Jul 2017 11:05:12 -0700 Subject: [PATCH] Modify precision of NaiveNumber. Fix factorial to work with 0./a.exe Add function to get nth term of the exp Maclaurin series. --- src/org/nwapw/abacus/number/NaiveNumber.java | 2 +- src/org/nwapw/abacus/plugin/StandardPlugin.java | 15 +++++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) mode change 100644 => 100755 src/org/nwapw/abacus/plugin/StandardPlugin.java diff --git a/src/org/nwapw/abacus/number/NaiveNumber.java b/src/org/nwapw/abacus/number/NaiveNumber.java index 93c501b..0a86696 100755 --- a/src/org/nwapw/abacus/number/NaiveNumber.java +++ b/src/org/nwapw/abacus/number/NaiveNumber.java @@ -29,7 +29,7 @@ public class NaiveNumber implements NumberInterface { @Override public int precision() { - return 4; + return 10; } @Override diff --git a/src/org/nwapw/abacus/plugin/StandardPlugin.java b/src/org/nwapw/abacus/plugin/StandardPlugin.java old mode 100644 new mode 100755 index 0d718c3..f7a2363 --- a/src/org/nwapw/abacus/plugin/StandardPlugin.java +++ b/src/org/nwapw/abacus/plugin/StandardPlugin.java @@ -80,6 +80,9 @@ public class StandardPlugin extends Plugin { @Override protected NumberInterface applyInternal(NumberInterface[] params) { + if(params[0].signum() == 0){ + return (new NaiveNumber(1)).promoteTo(params[0].getClass()); + } NumberInterface factorial = params[0]; NumberInterface multiplier = params[0]; //It is necessary to later prevent calls of factorial on anything but non-negative integers. @@ -89,6 +92,18 @@ public class StandardPlugin extends Plugin { return factorial; } }); + + System.out.println(getExpSeriesTerm(4, new NaiveNumber(3))); + } + + /** + * Returns the nth term of the Taylor series (centered at 0) of e^x + * @param n the term required (n >= 0). + * @param x the real number at which the series is evaluated. + * @return + */ + private NumberInterface getExpSeriesTerm(int n, NumberInterface x){ + return x.intPow(n).divide(this.getFunction("!").apply((new NaiveNumber(n)).promoteTo(x.getClass()))); } }