diff --git a/app/src/main/java/org/solovyev/android/calculator/converter/ConverterFragment.java b/app/src/main/java/org/solovyev/android/calculator/converter/ConverterFragment.java index e4af8f70..39ea38b2 100644 --- a/app/src/main/java/org/solovyev/android/calculator/converter/ConverterFragment.java +++ b/app/src/main/java/org/solovyev/android/calculator/converter/ConverterFragment.java @@ -21,6 +21,7 @@ import android.widget.*; import butterknife.Bind; import butterknife.ButterKnife; import jscl.JsclMathEngine; +import jscl.NumeralBase; import midpcalc.Real; import org.solovyev.android.calculator.*; @@ -310,12 +311,13 @@ public class ConverterFragment extends BaseDialogFragment if (!TextUtils.isEmpty(groupingSeparator)) { value = value.replace(groupingSeparator, ""); } - return Double.longBitsToDouble(new Real(value).toDoubleBits()); + final long bits = new Real(value).toDoubleBits(); + return Double.longBitsToDouble(bits); } @Nonnull private String formatDouble(double toValue) { - return JsclMathEngine.getInstance().formatDec(toValue); + return JsclMathEngine.getInstance().format(toValue, NumeralBase.dec); } @Override diff --git a/jscl/src/main/java/jscl/JsclMathEngine.java b/jscl/src/main/java/jscl/JsclMathEngine.java index 9a84275b..c295fec7 100644 --- a/jscl/src/main/java/jscl/JsclMathEngine.java +++ b/jscl/src/main/java/jscl/JsclMathEngine.java @@ -9,7 +9,7 @@ import jscl.math.operator.Percent; import jscl.math.operator.Rand; import jscl.math.operator.matrix.OperatorsRegistry; import jscl.text.ParseException; -import midpcalc.Real; +import org.solovyev.common.NumberFormatter; import org.solovyev.common.math.MathRegistry; import org.solovyev.common.msg.MessageRegistry; import org.solovyev.common.msg.Messages; @@ -17,7 +17,6 @@ import org.solovyev.common.msg.Messages; import javax.annotation.Nonnull; import javax.annotation.Nullable; import java.math.BigDecimal; -import java.text.DecimalFormat; import java.text.DecimalFormatSymbols; import java.util.List; import java.util.Locale; @@ -33,17 +32,10 @@ public class JsclMathEngine implements MathEngine { @Nonnull private final ConstantsRegistry constantsRegistry = new ConstantsRegistry(); @Nonnull - private final ThreadLocal defaultNumbersFormat = new ThreadLocal() { + private final ThreadLocal numberFormatter = new ThreadLocal() { @Override - protected DecimalFormat initialValue() { - return new DecimalFormat(); - } - }; - @Nonnull - private final ThreadLocal smallNumbersFormat = new ThreadLocal() { - @Override - protected DecimalFormat initialValue() { - return new DecimalFormat("##0.#####E0"); + protected NumberFormatter initialValue() { + return new NumberFormatter(); } }; @Nonnull @@ -165,107 +157,30 @@ public class JsclMathEngine implements MathEngine { public String format(@Nonnull Double value, @Nonnull NumeralBase nb) throws NumeralBaseException { if (value.isInfinite()) { return formatInfinity(value); - } else if (value.isNaN()) { + } + if (value.isNaN()) { // return "NaN" return String.valueOf(value); - } else if (nb == NumeralBase.dec) { - return formatDec(value); - } else { - return convert(value, nb); } - } - - @Nonnull - public String formatDec(@Nonnull Double value) { - if (value == 0d) { - return "0"; + if (nb == NumeralBase.dec) { + if (value == 0d) { + return "0"; + } + // detect if current number is precisely equals to constant in constants' registry (NOTE: ONLY FOR SYSTEM CONSTANTS) + final IConstant constant = findConstant(value); + if (constant != null) { + return constant.getName(); + } } - // detect if current number is precisely equals to constant in constants' registry (NOTE: ONLY FOR SYSTEM CONSTANTS) - final IConstant constant = findConstant(value); - if (constant != null) { - return constant.getName(); - } - + final NumberFormatter nf = numberFormatter.get(); + nf.setGroupingSeparator(useGroupingSeparator ? decimalGroupSymbols.getGroupingSeparator() : NumberFormatter.NO_GROUPING); + nf.setPrecision(roundResult ? precision : NumberFormatter.DEFAULT_PRECISION); if (scienceNotation) { - return formatDecEngineering(value); - } - - return formatDecDefault(value); - } - - @Nonnull - private String formatDecEngineering(@Nonnull Double value) { - final double absValue = Math.abs(value); - final boolean smallNumber = absValue < 1 && absValue >= 0.001; - final Real.NumberFormat nf = new Real.NumberFormat(); - nf.fse = smallNumber ? Real.NumberFormat.FSE_FIX : Real.NumberFormat.FSE_ENG; - if (useGroupingSeparator) { - nf.thousand = decimalGroupSymbols.getGroupingSeparator(); - } - if (roundResult) { - nf.precision = precision; - } - final Real real = new Real(Double.toString(value)); - return stripTrailingZeros(real.toString(nf)); - } - - @Nonnull - private String stripTrailingZeros(@Nonnull String s) { - final int dot = s.indexOf('.'); - if (dot < 0) { - // no dot - no trailing zeros - return s; - } - final int e = s.lastIndexOf('E'); - final int start; - String exponent = ""; - if (e > 0) { - exponent = s.substring(e); - if (exponent.length() == 2 && exponent.charAt(1) == '0') { - exponent = ""; - } - start = e - 1; + nf.useEngineeringFormat(NumberFormatter.DEFAULT_MAGNITUDE); } else { - start = s.length() - 1; + nf.useSimpleFormat(); } - final int i = findLastNonZero(s, start); - return s.substring(0, i == dot ? i : i + 1) + exponent; - } - - private int findLastNonZero(String s, int start) { - int i = start; - for (; i >= 0; i--) { - if (s.charAt(i) != '0') { - break; - } - } - return i; - } - - @Nonnull - private String formatDecDefault(@Nonnull Double value) { - final BigDecimal bd; - if (roundResult) { - bd = BigDecimal.valueOf(value).setScale(precision, BigDecimal.ROUND_HALF_UP); - } else { - bd = BigDecimal.valueOf(value); - } - value = bd.doubleValue(); - if (value == 0) { - return "0"; - } - - final DecimalFormat df = Math.abs(value) < Math.pow(10, -5) ? smallNumbersFormat.get() : defaultNumbersFormat.get(); - df.setDecimalFormatSymbols(decimalGroupSymbols); - df.setGroupingUsed(useGroupingSeparator); - df.setGroupingSize(NumeralBase.dec.getGroupingSize()); - if (roundResult) { - df.setMaximumFractionDigits(precision); - } else { - // set maximum fraction digits high enough to show all fraction digits in case of no rounding - df.setMaximumFractionDigits(MAX_FRACTION_DIGITS); - } - return df.format(value); + return nf.format(value, nb.radix).toString(); } @Nullable diff --git a/jscl/src/main/java/midpcalc/Real.java b/jscl/src/main/java/midpcalc/Real.java index d0736b2b..d42294fe 100755 --- a/jscl/src/main/java/midpcalc/Real.java +++ b/jscl/src/main/java/midpcalc/Real.java @@ -1,6 +1,5 @@ package midpcalc; - /** * Java integer implementation of 63-bit precision floating point. *
Version 1.13 @@ -60,7 +59,7 @@ package midpcalc; * accuracy. Error bounds are for "typical arguments" and may increase when * results approach zero or * infinity. The abbreviation {@link Math#ulp(double) ULP} means Unit in the - * Last Place. An error bound of � ULP means that the result is correctly + * Last Place. An error bound of œ ULP means that the result is correctly * rounded. The relative execution time listed under each method is the * average from running on SonyEricsson T610 (R3C), K700i, and Nokia 6230i. *

@@ -261,23 +260,9 @@ public final class Real { * Initialized to mantissa of e. */ public static long randSeedB = 0x56fc2a2c515da54dL; - // Temporary values used by functions (to avoid "new" inside functions) - private static Real tmp0 = new Real(); // tmp for basic functions - private static Real recipTmp = new Real(); - private static Real recipTmp2 = new Real(); - private static Real sqrtTmp = new Real(); - private static Real expTmp = new Real(); - private static Real expTmp2 = new Real(); - private static Real expTmp3 = new Real(); - private static Real tmp1 = new Real(); - private static Real tmp2 = new Real(); - private static Real tmp3 = new Real(); - private static Real tmp4 = new Real(); - private static Real tmp5 = new Real(); - private static byte[] ftoaDigits = new byte[65]; - private static StringBuffer ftoaBuf = new StringBuffer(40); - private static StringBuffer ftoaExp = new StringBuffer(15); - private static NumberFormat tmpFormat = new NumberFormat(); + private final byte[] digits = new byte[65]; + private final StringBuilder buf = new StringBuilder(40); + private final StringBuilder exp = new StringBuilder(15); /** * The mantissa of a Real. To maintain numbers in a * normalized state and to preserve the integrity of abnormal numbers, it @@ -285,7 +270,7 @@ public final class Real { * Real directly. *

*

The number represented by a Real equals:
- *      -1sign � mantissa � 2-62 � 2exponent-0x40000000 + *      -1sign · mantissa · 2-62 · 2exponent-0x40000000 *

*

The normalized mantissa of a finite Real must be * between 0x4000000000000000L and @@ -330,6 +315,18 @@ public final class Real { * extensions. */ public byte sign; + private Real tmp0; + private Real tmp1; + private Real tmp2; + private Real tmp3; + private Real tmp4; + private Real tmp5; + private Real recipTmp; + private Real recipTmp2; + private Real sqrtTmp; + private Real expTmp; + private Real expTmp2; + private Real expTmp3; /** * Creates a new Real with a value of zero. @@ -562,6 +559,66 @@ public final class Real { nextBits(63); } + private Real tmp0() { + if (tmp0 == null) tmp0 = new Real(); + return tmp0; + } + + private Real tmp1() { + if (tmp1 == null) tmp1 = new Real(); + return tmp1; + } + + private Real tmp2() { + if (tmp2 == null) tmp2 = new Real(); + return tmp2; + } + + private Real tmp3() { + if (tmp3 == null) tmp3 = new Real(); + return tmp3; + } + + private Real tmp4() { + if (tmp4 == null) tmp4 = new Real(); + return tmp4; + } + + private Real tmp5() { + if (tmp5 == null) tmp5 = new Real(); + return tmp5; + } + + private Real recipTmp() { + if (recipTmp == null) recipTmp = new Real(); + return recipTmp; + } + + private Real recipTmp2() { + if (recipTmp2 == null) recipTmp2 = new Real(); + return recipTmp2; + } + + private Real sqrtTmp() { + if (sqrtTmp == null) sqrtTmp = new Real(); + return sqrtTmp; + } + + private Real expTmp() { + if (expTmp == null) expTmp = new Real(); + return expTmp; + } + + private Real expTmp2() { + if (expTmp2 == null) expTmp2 = new Real(); + return expTmp2; + } + + private Real expTmp3() { + if (expTmp3 == null) expTmp3 = new Real(); + return expTmp3; + } + /** * Assigns this Real the value of another Real. *

@@ -667,7 +724,7 @@ public final class Real { * Equivalent double code: * this = Double.{@link Double#valueOf(String) valueOf}(a); * Approximate error bound: - * �-1 ULPs + * œ-1 ULPs * * Execution time relative to add:   * @@ -728,9 +785,9 @@ public final class Real { * * Approximate error bound: * base-10 - * �-1 ULPs + * œ-1 ULPs * 2/8/16 - * � ULPs + * œ ULPs * * Execution time relative to add:   * base-2 @@ -1319,7 +1376,7 @@ public final class Real { *

* Error bound: - * � ULPs + * œ ULPs *
* Execution time relative to add:   * @@ -1443,7 +1500,7 @@ public final class Real { *

* Error bound: - * � ULPs + * œ ULPs *
* Execution time relative to add:   * @@ -1541,6 +1598,7 @@ public final class Real { * otherwise. */ public boolean equalTo(int a) { + final Real tmp0 = tmp0(); tmp0.assign(a); return equalTo(tmp0); } @@ -1597,6 +1655,7 @@ public final class Real { * otherwise, or if this Real is NaN. */ public boolean notEqualTo(int a) { + final Real tmp0 = tmp0(); tmp0.assign(a); return notEqualTo(tmp0); } @@ -1649,6 +1708,7 @@ public final class Real { * or if this Real is NaN. */ public boolean lessThan(int a) { + final Real tmp0 = tmp0(); tmp0.assign(a); return lessThan(tmp0); } @@ -1701,6 +1761,7 @@ public final class Real { * otherwise, or if this Real is NaN. */ public boolean lessEqual(int a) { + final Real tmp0 = tmp0(); tmp0.assign(a); return lessEqual(tmp0); } @@ -1753,6 +1814,7 @@ public final class Real { * false otherwise, or if this Real is NaN. */ public boolean greaterThan(int a) { + final Real tmp0 = tmp0(); tmp0.assign(a); return greaterThan(tmp0); } @@ -1805,6 +1867,7 @@ public final class Real { * false otherwise, or if this Real is NaN. */ public boolean greaterEqual(int a) { + final Real tmp0 = tmp0(); tmp0.assign(a); return greaterEqual(tmp0); } @@ -2275,11 +2338,11 @@ public final class Real { * Equivalent double code: * this += a; *
Error bound: - * � ULPs + * œ ULPs *
* Execution time relative to add:   * - * �� 1.0 �� + * «« 1.0 »» *
* * @param a the Real to add to this. @@ -2388,7 +2451,7 @@ public final class Real { * Equivalent double code:

* this += a; *
Error bound: - * � ULPs + * œ ULPs *
* Execution time relative to add:   * @@ -2398,6 +2461,7 @@ public final class Real { * @param a the int to add to this. */ public void add(int a) { + final Real tmp0 = tmp0(); tmp0.assign(a); add(tmp0); } @@ -2532,7 +2596,7 @@ public final class Real { * Equivalent double code: * this -= a; *
Error bound: - * � ULPs + * œ ULPs *
* Execution time relative to add:   * @@ -2542,6 +2606,7 @@ public final class Real { * @param a the Real to subtract from this. */ public void sub(Real a) { + final Real tmp0 = tmp0(); tmp0.mantissa = a.mantissa; tmp0.exponent = a.exponent; tmp0.sign = (byte) (a.sign ^ 1); @@ -2558,7 +2623,7 @@ public final class Real { * Equivalent double code: * this -= a; *
Error bound: - * � ULPs + * œ ULPs *
* Execution time relative to add:   * @@ -2568,6 +2633,7 @@ public final class Real { * @param a the int to subtract from this. */ public void sub(int a) { + final Real tmp0 = tmp0(); tmp0.assign(a); sub(tmp0); } @@ -2581,7 +2647,7 @@ public final class Real { * Equivalent double code: * this *= a; *
Error bound: - * � ULPs + * œ ULPs *
* Execution time relative to add:   * @@ -2648,7 +2714,7 @@ public final class Real { * Equivalent double code: * this *= a; *
Error bound: - * � ULPs + * œ ULPs *
* Execution time relative to add:   * @@ -2812,7 +2878,7 @@ public final class Real { * Equivalent double code: * this = this*this; *
Error bound: - * � ULPs + * œ ULPs *
* Execution time relative to add:   * @@ -2861,7 +2927,7 @@ public final class Real { * Equivalent double code: * this /= a; *
Error bound: - * � ULPs + * œ ULPs *
* Execution time relative to add:   * @@ -2921,7 +2987,7 @@ public final class Real { * Equivalent double code: * this /= a; *
Error bound: - * � ULPs + * œ ULPs *
* Execution time relative to add:   * @@ -2983,7 +3049,7 @@ public final class Real { * Equivalent double code: * this = a/this; *
Error bound: - * � ULPs + * œ ULPs *
* Execution time relative to add:   * @@ -2993,17 +3059,10 @@ public final class Real { * @param a the Real to be divided by this. */ public void rdiv(Real a) { - { - recipTmp.mantissa = a.mantissa; - recipTmp.exponent = a.exponent; - recipTmp.sign = a.sign; - } + final Real recipTmp = recipTmp(); + recipTmp.assign(a); recipTmp.div(this); - { - this.mantissa = recipTmp.mantissa; - this.exponent = recipTmp.exponent; - this.sign = recipTmp.sign; - } + assign(recipTmp); } /** @@ -3016,7 +3075,7 @@ public final class Real { * Equivalent double code: * this = a/this; *
Error bound: - * � ULPs + * œ ULPs *
* Execution time relative to add:   * @@ -3026,6 +3085,7 @@ public final class Real { * @param a the int to be divided by this. */ public void rdiv(int a) { + final Real tmp0 = tmp0(); tmp0.assign(a); rdiv(tmp0); } @@ -3039,7 +3099,7 @@ public final class Real { * Equivalent double code: * this = 1/this; *
Error bound: - * � ULPs + * œ ULPs *
* Execution time relative to add:   * @@ -3118,22 +3178,16 @@ public final class Real { int exp = 0x40000000 - exponent; exponent = 0x40000000; // Save -A - { - recipTmp.mantissa = this.mantissa; - recipTmp.exponent = this.exponent; - recipTmp.sign = this.sign; - } + final Real recipTmp = recipTmp(); + recipTmp.assign(this); long recipTmpExtra = extra; recipTmp.neg(); // First establish approximate result (actually 63 bit accurate) recip(); // Perform one Newton-Raphson iteration // Xn+1 = Xn + Xn*(1-A*Xn) - { - recipTmp2.mantissa = this.mantissa; - recipTmp2.exponent = this.exponent; - recipTmp2.sign = this.sign; - } + final Real recipTmp2 = recipTmp2(); + recipTmp2.assign(this); extra = mul128(0, recipTmp, recipTmpExtra); extra = add128(extra, ONE, 0); extra = mul128(extra, recipTmp2, 0); @@ -3188,11 +3242,8 @@ public final class Real { makeInfinity(sign); return; } - { - tmp0.mantissa = a.mantissa; - tmp0.exponent = a.exponent; - tmp0.sign = a.sign; - } + final Real tmp0 = tmp0(); + tmp0.assign(a); // tmp0 should be free // Perform same division as with mod, and don't round up long extra = tmp0.recip128(0); @@ -3207,11 +3258,8 @@ public final class Real { } private void modInternal(/*long thisExtra,*/ Real a, long aExtra) { - { - tmp0.mantissa = a.mantissa; - tmp0.exponent = a.exponent; - tmp0.sign = a.sign; - } + final Real tmp0 = tmp0(); + tmp0.assign(a); // tmp0 should be free long extra = tmp0.recip128(aExtra); extra = tmp0.mul128(extra, this, 0/*thisExtra*/); // tmp0 == this/a @@ -3622,6 +3670,7 @@ public final class Real { } private int compare(int a) { + final Real tmp0 = tmp0(); tmp0.assign(a); return compare(tmp0); } @@ -3665,20 +3714,15 @@ public final class Real { if ((this.exponent < 0 && this.mantissa == 0)) return; // Save X - { - recipTmp.mantissa = this.mantissa; - recipTmp.exponent = this.exponent; - recipTmp.sign = this.sign; - } + final Real recipTmp = recipTmp(); + recipTmp.assign(this); // normalize to range [0.5, 1) int e = exponent - 0x3fffffff; exponent = 0x3fffffff; // quadratic approximation, relative error 6.45e-4 - { - recipTmp2.mantissa = this.mantissa; - recipTmp2.exponent = this.exponent; - recipTmp2.sign = this.sign; - } + final Real recipTmp2 = recipTmp2(); + recipTmp2.assign(this); + final Real sqrtTmp = sqrtTmp(); { sqrtTmp.sign = (byte) 1; sqrtTmp.exponent = 0x3ffffffd; @@ -3709,11 +3753,7 @@ public final class Real { // Newton iteratios: // Yn+1 = (Yn + X/Yn)/2 for (int i = 0; i < 3; i++) { - { - recipTmp2.mantissa = recipTmp.mantissa; - recipTmp2.exponent = recipTmp.exponent; - recipTmp2.sign = recipTmp.sign; - } + recipTmp2.assign(recipTmp); recipTmp2.div(this); add(recipTmp2); scalbn(-1); @@ -3768,17 +3808,15 @@ public final class Real { // not zero, nan or infinity final long start = 0x5120000000000000L; // Save -A - { - recipTmp.mantissa = this.mantissa; - recipTmp.exponent = this.exponent; - recipTmp.sign = this.sign; - } + final Real recipTmp = recipTmp(); + recipTmp.assign(this); recipTmp.neg(); // First establish approximate result mantissa = start - (mantissa >>> 2); int expRmd = exponent == 0 ? 2 : (exponent - 1) % 3; exponent = 0x40000000 - (exponent - 0x40000000 - expRmd) / 3; normalize(); + final Real recipTmp2 = recipTmp2(); if (expRmd > 0) { { recipTmp2.sign = (byte) 0; @@ -3793,11 +3831,7 @@ public final class Real { // Now perform Newton-Raphson iteration // Xn+1 = (4*Xn - A*Xn**4)/3 for (int i = 0; i < 4; i++) { - { - recipTmp2.mantissa = this.mantissa; - recipTmp2.exponent = this.exponent; - recipTmp2.sign = this.sign; - } + recipTmp2.assign(this); sqr(); sqr(); mul(recipTmp); @@ -3848,11 +3882,8 @@ public final class Real { negative = true; abs(); } - { - tmp2.mantissa = n.mantissa; - tmp2.exponent = n.exponent; - tmp2.sign = n.sign; - } + final Real tmp2 = tmp2(); + tmp2.assign(n); // Copy to temporary location in case of x.nroot(x) tmp2.recip(); pow(tmp2); @@ -3880,11 +3911,8 @@ public final class Real { * @param a the Real argument. */ public void hypot(Real a) { - { - tmp1.mantissa = a.mantissa; - tmp1.exponent = a.exponent; - tmp1.sign = a.sign; - } + final Real tmp1 = tmp1(); + tmp1.assign(this); // Copy to temporary location in case of x.hypot(x) tmp1.sqr(); sqr(); @@ -3909,11 +3937,8 @@ public final class Real { return; } // Extract integer part - { - expTmp.mantissa = this.mantissa; - expTmp.exponent = this.exponent; - expTmp.sign = this.sign; - } + final Real expTmp = expTmp(); + expTmp.assign(this); expTmp.add(HALF); expTmp.floor(); int exp = expTmp.toInteger(); @@ -3939,14 +3964,11 @@ public final class Real { */ // Now -0.5
*/ public void exp() { + final Real expTmp = expTmp(); { expTmp.sign = (byte) 0; expTmp.exponent = 0x40000000; @@ -4069,6 +4093,7 @@ public final class Real { * */ public void exp10() { + final Real expTmp = expTmp(); { expTmp.sign = (byte) 0; expTmp.exponent = 0x40000001; @@ -4105,17 +4130,14 @@ public final class Real { int e = exponent - 0x3fffffff; exponent = 0x3fffffff; // rational appriximation - // log(1+x) = x - x�/2 + x� P(x)/Q(x) + // log(1+x) = x - x²/2 + x³ P(x)/Q(x) if (this.compare(SQRT1_2) < 0) { e--; exponent++; } sub(ONE); - { - expTmp2.mantissa = this.mantissa; - expTmp2.exponent = this.exponent; - expTmp2.sign = this.sign; - } + final Real expTmp2 = expTmp2(); + expTmp2.assign(this); // P(x) { this.sign = (byte) 0; @@ -4124,6 +4146,7 @@ public final class Real { } //4.5270000862445199635215E-5 mul(expTmp2); + final Real expTmp3 = expTmp3(); { expTmp3.sign = (byte) 0; expTmp3.exponent = 0x3ffffffe; @@ -4172,11 +4195,8 @@ public final class Real { //20.039553499201281259648 add(expTmp3); // Q(x) - { - expTmp.mantissa = expTmp2.mantissa; - expTmp.exponent = expTmp2.exponent; - expTmp.sign = expTmp2.sign; - } + final Real expTmp = expTmp(); + expTmp.assign(expTmp2); { expTmp3.sign = (byte) 0; expTmp3.exponent = 0x40000003; @@ -4258,6 +4278,7 @@ public final class Real { */ public void ln() { int exp = lnInternal(); + final Real expTmp = expTmp(); expTmp.assign(exp); expTmp.mul(LN2); add(expTmp); @@ -4304,6 +4325,7 @@ public final class Real { */ public void log10() { int exp = lnInternal(); + final Real expTmp = expTmp(); expTmp.assign(exp); expTmp.mul(LN2); add(expTmp); @@ -4324,7 +4346,7 @@ public final class Real { *
this = Math.{@link Math#pow(double, double) pow}(10, exp);
* return exp; * Error bound: - * � ULPs + * œ ULPs * * Execution time relative to add:   * @@ -4336,11 +4358,8 @@ public final class Real { public int lowPow10() { if (!(this.exponent >= 0 && this.mantissa != 0)) return 0; - { - tmp2.mantissa = this.mantissa; - tmp2.exponent = this.exponent; - tmp2.sign = this.sign; - } + final Real tmp2 = tmp2(); + tmp2.assign(this); // Approximate log10 using exponent only int e = exponent - 0x40000000; if (e < 0) // it's important to achieve floor(exponent*ln2/ln10) @@ -4354,29 +4373,18 @@ public final class Real { this.sign = TEN.sign; } pow(e); + final Real tmp3 = tmp3(); if ((this.exponent == 0 && this.mantissa == 0)) { // A *really* small number, then - { - tmp3.mantissa = TEN.mantissa; - tmp3.exponent = TEN.exponent; - tmp3.sign = TEN.sign; - } + tmp3.assign(TEN); tmp3.pow(e + 1); } else { - { - tmp3.mantissa = this.mantissa; - tmp3.exponent = this.exponent; - tmp3.sign = this.sign; - } + tmp3.assign(this); tmp3.mul10(); } if (tmp3.compare(tmp2) <= 0) { // First estimate of log10 was too low e++; - { - this.mantissa = tmp3.mantissa; - this.exponent = tmp3.exponent; - this.sign = tmp3.sign; - } + assign(tmp3); } return e; } @@ -4396,7 +4404,7 @@ public final class Real { *

  • if |this| < 1.0 and a is -Infinity then result is +Infinity *
  • if |this| > 1.0 and a is -Infinity then result is +0 *
  • if |this| < 1.0 and a is +Infinity then result is +0 - *
  • if |this| = 1.0 and a is �Infinity then result is NaN + *
  • if |this| = 1.0 and a is ±Infinity then result is NaN *
  • if this = +0 and a > 0 then result is +0 *
  • if this = +0 and a < 0 then result is +Inf *
  • if this = -0 and a > 0, and odd integer then result is -0 @@ -4445,12 +4453,9 @@ public final class Real { } if (a.compare(ONE) == 0) return; + final Real tmp1 = tmp1(); if ((a.exponent < 0 && a.mantissa == 0)) { - { - tmp1.mantissa = this.mantissa; - tmp1.exponent = this.exponent; - tmp1.sign = this.sign; - } + tmp1.assign(this); tmp1.abs(); int test = tmp1.compare(ONE); if (test > 0) { @@ -4529,34 +4534,20 @@ public final class Real { } sign = 0; } - { - tmp1.mantissa = a.mantissa; - tmp1.exponent = a.exponent; - tmp1.sign = a.sign; - } + tmp1.assign(a); + final Real tmp2 = tmp2(); + final Real tmp3 = tmp3(); if (tmp1.exponent <= 0x4000001e) { // For increased accuracy, exponentiate with integer part of // exponent by successive squaring // (I really don't know why this works) - { - tmp2.mantissa = tmp1.mantissa; - tmp2.exponent = tmp1.exponent; - tmp2.sign = tmp1.sign; - } + tmp2.assign(tmp1); tmp2.floor(); - { - tmp3.mantissa = this.mantissa; - tmp3.exponent = this.exponent; - tmp3.sign = this.sign; - } + tmp3.assign(this); tmp3.pow(tmp2.toInteger()); tmp1.sub(tmp2); } else { - { - tmp3.mantissa = ONE.mantissa; - tmp3.exponent = ONE.exponent; - tmp3.sign = ONE.sign; - } + tmp3.assign(ONE); } // Do log2 and maintain accuracy int e = lnInternal(); @@ -4592,7 +4583,7 @@ public final class Real { * Equivalent double code: * this = Math.{@link Math#pow(double, double) pow}(this, a); * Error bound: - * � ULPs + * œ ULPs * * Execution time relative to add:   * @@ -4609,16 +4600,9 @@ public final class Real { recp = true; } long extra = 0, expTmpExtra = 0; - { - expTmp.mantissa = this.mantissa; - expTmp.exponent = this.exponent; - expTmp.sign = this.sign; - } - { - this.mantissa = ONE.mantissa; - this.exponent = ONE.exponent; - this.sign = ONE.sign; - } + final Real expTmp = expTmp(); + expTmp.assign(this); + assign(ONE); for (; a != 0; a >>>= 1) { if ((a & 1) != 0) extra = mul128(extra, expTmp, expTmpExtra); @@ -4641,17 +4625,11 @@ public final class Real { */ // X */ public void tan() { - { - tmp4.mantissa = this.mantissa; - tmp4.exponent = this.exponent; - tmp4.sign = this.sign; - } + final Real tmp4 = tmp4(); + tmp4.assign(this); tmp4.cos(); sin(); div(tmp4); @@ -4935,11 +4908,8 @@ public final class Real { * */ public void asin() { - { - tmp1.mantissa = this.mantissa; - tmp1.exponent = this.exponent; - tmp1.sign = this.sign; - } + final Real tmp1 = tmp1(); + tmp1.assign(this); sqr(); neg(); add(ONE); @@ -4968,11 +4938,8 @@ public final class Real { public void acos() { boolean negative = (this.sign != 0); abs(); - { - tmp1.mantissa = this.mantissa; - tmp1.exponent = this.exponent; - tmp1.sign = this.sign; - } + final Real tmp1 = tmp1(); + tmp1.assign(this); sqr(); neg(); add(ONE); @@ -5029,11 +4996,8 @@ public final class Real { // range reduction boolean addPI_2 = false; boolean addPI_4 = false; - { - tmp1.mantissa = SQRT2.mantissa; - tmp1.exponent = SQRT2.exponent; - tmp1.sign = SQRT2.sign; - } + final Real tmp1 = tmp1(); + tmp1.assign(SQRT2); tmp1.add(ONE); if (this.compare(tmp1) > 0) { addPI_2 = true; @@ -5043,11 +5007,7 @@ public final class Real { tmp1.sub(TWO); if (this.compare(tmp1) > 0) { addPI_4 = true; - { - tmp1.mantissa = this.mantissa; - tmp1.exponent = this.exponent; - tmp1.sign = this.sign; - } + tmp1.assign(this); tmp1.add(ONE); sub(ONE); div(tmp1); @@ -5055,19 +5015,13 @@ public final class Real { } // Now |X| */ public void sinh() { - { - tmp1.mantissa = this.mantissa; - tmp1.exponent = this.exponent; - tmp1.sign = this.sign; - } + final Real tmp1 = tmp1(); + tmp1.assign(this); tmp1.neg(); tmp1.exp(); exp(); @@ -5249,11 +5197,8 @@ public final class Real { * */ public void cosh() { - { - tmp1.mantissa = this.mantissa; - tmp1.exponent = this.exponent; - tmp1.sign = this.sign; - } + final Real tmp1 = tmp1(); + tmp1.assign(this); tmp1.neg(); tmp1.exp(); exp(); @@ -5278,19 +5223,13 @@ public final class Real { * */ public void tanh() { - { - tmp1.mantissa = this.mantissa; - tmp1.exponent = this.exponent; - tmp1.sign = this.sign; - } + final Real tmp1 = tmp1(); + tmp1.assign(this); tmp1.neg(); tmp1.exp(); exp(); - { - tmp2.mantissa = this.mantissa; - tmp2.exponent = this.exponent; - tmp2.sign = this.sign; - } + final Real tmp2 = tmp2(); + tmp2.assign(this); tmp2.add(tmp1); sub(tmp1); div(tmp2); @@ -5319,11 +5258,8 @@ public final class Real { // values byte s = sign; sign = 0; - { - tmp1.mantissa = this.mantissa; - tmp1.exponent = this.exponent; - tmp1.sign = this.sign; - } + final Real tmp1 = tmp1(); + tmp1.assign(this); tmp1.sqr(); tmp1.add(ONE); tmp1.sqrt(); @@ -5350,11 +5286,8 @@ public final class Real { * */ public void acosh() { - { - tmp1.mantissa = this.mantissa; - tmp1.exponent = this.exponent; - tmp1.sign = this.sign; - } + final Real tmp1 = tmp1(); + tmp1.assign(this); tmp1.sqr(); tmp1.sub(ONE); tmp1.sqrt(); @@ -5379,11 +5312,8 @@ public final class Real { * */ public void atanh() { - { - tmp1.mantissa = this.mantissa; - tmp1.exponent = this.exponent; - tmp1.sign = this.sign; - } + final Real tmp1 = tmp1(); + tmp1.assign(this); tmp1.neg(); tmp1.add(ONE); add(ONE); @@ -5420,11 +5350,8 @@ public final class Real { gamma(); return; } - { - tmp1.mantissa = this.mantissa; - tmp1.exponent = this.exponent; - tmp1.sign = this.sign; - } + final Real tmp1 = tmp1(); + tmp1.assign(this); { this.mantissa = ONE.mantissa; this.exponent = ONE.exponent; @@ -5458,45 +5385,30 @@ public final class Real { // x<0: gamma(-x) = -pi/(x*gamma(x)*sin(pi*x)) boolean negative = (this.sign != 0); abs(); - { - tmp1.mantissa = this.mantissa; - tmp1.exponent = this.exponent; - tmp1.sign = this.sign; - } + final Real tmp1 = tmp1(); + tmp1.assign(this); // xn: gamma(x) = exp((x-1/2)*ln(x) - x + ln(2*pi)/2 + 1/12x - 1/360x� + // x>n: gamma(x) = exp((x-1/2)*ln(x) - x + ln(2*pi)/2 + 1/12x - 1/360x³ // + 1/1260x**5 - 1/1680x**7+1/1188x**9) - { - tmp3.mantissa = this.mantissa; - tmp3.exponent = this.exponent; - tmp3.sign = this.sign; - } + final Real tmp3 = tmp3(); + tmp3.assign(this); // x - { - tmp4.mantissa = this.mantissa; - tmp4.exponent = this.exponent; - tmp4.sign = this.sign; - } - tmp4.sqr(); // x� + final Real tmp4 = tmp4(); + tmp4.assign(this); + tmp4.sqr(); // x² // (x-1/2)*ln(x)-x ln(); - { - tmp5.mantissa = tmp3.mantissa; - tmp5.exponent = tmp3.exponent; - tmp5.sign = tmp3.sign; - } + final Real tmp5 = tmp5(); + tmp5.assign(tmp3); tmp5.sub(HALF); mul(tmp5); sub(tmp3); @@ -5513,7 +5425,7 @@ public final class Real { tmp5.recip(); add(tmp5); tmp3.mul(tmp4); - // - 1/360x� + // - 1/360x³ tmp5.assign(360); tmp5.mul(tmp3); tmp5.recip(); @@ -5565,26 +5477,18 @@ public final class Real { // sqrt(pi)\ 3 2!*5 3!*7 4!*9 / // long extra = 0, tmp1Extra, tmp2Extra, tmp3Extra, tmp4Extra; - { - tmp1.mantissa = this.mantissa; - tmp1.exponent = this.exponent; - tmp1.sign = this.sign; - } + final Real tmp1 = tmp1(); + tmp1.assign(this); tmp1Extra = 0; - { - tmp2.mantissa = this.mantissa; - tmp2.exponent = this.exponent; - tmp2.sign = this.sign; - } + final Real tmp2 = tmp2(); + tmp2.assign(this); tmp2Extra = tmp2.mul128(0, tmp2, 0); tmp2.neg(); - { - tmp3.mantissa = ONE.mantissa; - tmp3.exponent = ONE.exponent; - tmp3.sign = ONE.sign; - } + final Real tmp3 = tmp3(); + tmp3.assign(ONE); tmp3Extra = 0; int i = 1; + final Real tmp4 = tmp4(); do { tmp1Extra = tmp1.mul128(tmp1Extra, tmp2, tmp2Extra); tmp4.assign(i); @@ -5608,17 +5512,15 @@ public final class Real { } private void erfc2Internal() { - // -x� -1 + // -x² -1 // e x / 1 3 3*5 3*5*7 // erfc(x) = -------- | 1 - --- + ------ - ------ + ------ - ... | - // sqrt(pi) \ 2x� 2 3 4 / - // (2x�) (2x�) (2x�) + // sqrt(pi) \ 2x² 2 3 4 / + // (2x²) (2x²) (2x²) // Calculate iteration stop criteria - { - tmp1.mantissa = this.mantissa; - tmp1.exponent = this.exponent; - tmp1.sign = this.sign; - } + final Real tmp1 = tmp1(); + tmp1.assign(this); tmp1.sqr(); + final Real tmp2 = tmp2(); { tmp2.sign = (byte) 0; tmp2.exponent = 0x40000000; @@ -5632,35 +5534,17 @@ public final class Real { digits = 64; tmp1.scalbn(1); int dxq = tmp1.toInteger() + 1; - { - tmp1.mantissa = this.mantissa; - tmp1.exponent = this.exponent; - tmp1.sign = this.sign; - } + tmp1.assign(this); recip(); - { - tmp2.mantissa = this.mantissa; - tmp2.exponent = this.exponent; - tmp2.sign = this.sign; - } - { - tmp3.mantissa = this.mantissa; - tmp3.exponent = this.exponent; - tmp3.sign = this.sign; - } + tmp2.assign(this); + final Real tmp3 = tmp3(); + tmp3.assign(this); tmp3.sqr(); tmp3.neg(); tmp3.scalbn(-1); - { - this.mantissa = ONE.mantissa; - this.exponent = ONE.exponent; - this.sign = ONE.sign; - } - { - tmp4.mantissa = ONE.mantissa; - tmp4.exponent = ONE.exponent; - tmp4.sign = ONE.sign; - } + assign(ONE); + final Real tmp4 = tmp4(); + tmp4.assign(ONE); int i = 1; do { tmp4.mul(2 * i - 1); @@ -5688,7 +5572,7 @@ public final class Real { *

    *

    The complementary error function is defined as the integral from * x to infinity of 2/√π �e-t� dt. It is + * overline;">π ·e-t² dt. It is * related to the error function, erf, by the formula * erfc(x)=1-erf(x). *

    @@ -5728,6 +5612,7 @@ public final class Real { } byte s = sign; sign = 0; + final Real tmp1 = tmp1(); { tmp1.sign = (byte) 0; tmp1.exponent = 0x40000002; @@ -5791,16 +5676,14 @@ public final class Real { // Part 1: Numerical Approximation Method for Inverse Phi // This accepts input of P and outputs approximate Z as Y // Source:Odeh & Evans. 1974. AS 70. Applied Statistics. - // R = sqrt(Ln(1/(Q�))) - { - tmp1.mantissa = this.mantissa; - tmp1.exponent = this.exponent; - tmp1.sign = this.sign; - } + // R = sqrt(Ln(1/(Q²))) + final Real tmp1 = tmp1(); + tmp1.assign(this); tmp1.ln(); tmp1.mul(-2); tmp1.sqrt(); // Y = -(R+((((P4*R+P3)*R+P2)*R+P1)*R+P0)/((((Q4*R+Q3)*R*Q2)*R+Q1)*R+Q0)) + final Real tmp2 = tmp2(); { tmp2.sign = (byte) 1; tmp2.exponent = 0x3ffffff1; @@ -5808,6 +5691,7 @@ public final class Real { } // P4=-0.0000453642210148 tmp2.mul(tmp1); + final Real tmp3 = tmp3(); { tmp3.sign = (byte) 1; tmp3.exponent = 0x3ffffffa; @@ -5840,6 +5724,7 @@ public final class Real { } // Q4=0.0038560700634 tmp3.mul(tmp1); + final Real tmp4 = tmp4(); { tmp4.sign = (byte) 0; tmp4.exponent = 0x3ffffffc; @@ -5874,32 +5759,22 @@ public final class Real { tmp2.div(tmp3); tmp1.add(tmp2); tmp1.neg(); - { - sqrtTmp.mantissa = tmp1.mantissa; - sqrtTmp.exponent = tmp1.exponent; - sqrtTmp.sign = tmp1.sign; - } + final Real sqrtTmp = sqrtTmp(); + sqrtTmp.assign(tmp1); // sqrtTmp and tmp5 not used by erfc() and exp() // Part 2: Refine to accuracy of erfc Function // This accepts inputs Y and P (from above) and outputs Z // (Using Halley's third order method for finding roots of equations) // Q = erfc(-Y/sqrt(2))/2-P - { - tmp5.mantissa = sqrtTmp.mantissa; - tmp5.exponent = sqrtTmp.exponent; - tmp5.sign = sqrtTmp.sign; - } + final Real tmp5 = tmp5(); + tmp5.assign(sqrtTmp); tmp5.mul(SQRT1_2); tmp5.neg(); tmp5.erfc(); tmp5.scalbn(-1); tmp5.sub(this); - // R = Q*sqrt(2*pi)*e^(Y�/2) - { - tmp3.mantissa = sqrtTmp.mantissa; - tmp3.exponent = sqrtTmp.exponent; - tmp3.sign = sqrtTmp.sign; - } + // R = Q*sqrt(2*pi)*e^(Y²/2) + tmp3.assign(sqrtTmp); tmp3.sqr(); tmp3.scalbn(-1); tmp3.exp(); @@ -5912,11 +5787,7 @@ public final class Real { // sqrt(2*pi) tmp5.mul(tmp3); // Z = Y-R/(1+R*Y/2) - { - this.mantissa = sqrtTmp.mantissa; - this.exponent = sqrtTmp.exponent; - this.sign = sqrtTmp.sign; - } + assign(sqrtTmp); mul(tmp5); scalbn(-1); add(ONE); @@ -5972,6 +5843,7 @@ public final class Real { long h; h = toLong(); frac(); + final Real tmp1 = tmp1(); tmp1.assign(60); mul(tmp1); m = toInteger(); @@ -5979,11 +5851,8 @@ public final class Real { mul(tmp1); // MAGIC ROUNDING: Check if we are 2**-16 sec short of a whole minute // i.e. "seconds" > 59.999985 - { - tmp2.mantissa = ONE.mantissa; - tmp2.exponent = ONE.exponent; - tmp2.sign = ONE.sign; - } + final Real tmp2 = tmp2(); + tmp2.assign(ONE); tmp2.scalbn(-16); add(tmp2); if (this.compare(tmp1) >= 0) { @@ -6059,6 +5928,7 @@ public final class Real { long h; h = toLong(); frac(); + final Real tmp1 = tmp1(); tmp1.assign(100); mul(tmp1); m = toInteger(); @@ -6066,11 +5936,8 @@ public final class Real { mul(tmp1); // MAGIC ROUNDING: Check if we are 2**-10 second short of 100 seconds // i.e. "seconds" > 99.999 - { - tmp2.mantissa = ONE.mantissa; - tmp2.exponent = ONE.exponent; - tmp2.sign = ONE.sign; - } + final Real tmp2 = tmp2(); + tmp2.assign(ONE); tmp2.scalbn(-10); add(tmp2); if (this.compare(tmp1) >= 0) { @@ -6120,7 +5987,7 @@ public final class Real { * Equivalent double code: * none * Error bound: - * � ULPs + * œ ULPs * * Execution time relative to add:   * @@ -6293,14 +6160,11 @@ public final class Real { else if (base == 16) scalbn(exp * 4); else { + final Real tmp1 = tmp1(); if (exp > 300000000 || exp < -300000000) { // Kludge to be able to enter very large and very small // numbers without causing over/underflows - { - tmp1.mantissa = TEN.mantissa; - tmp1.exponent = TEN.exponent; - tmp1.sign = TEN.sign; - } + tmp1.assign(TEN); if (exp < 0) { tmp1.pow(-exp / 2); div(tmp1); @@ -6310,11 +6174,7 @@ public final class Real { } exp -= exp / 2; } - { - tmp1.mantissa = TEN.mantissa; - tmp1.exponent = TEN.exponent; - tmp1.sign = TEN.sign; - } + tmp1.assign(TEN); if (exp < 0) { tmp1.pow(-exp); div(tmp1); @@ -6364,17 +6224,11 @@ public final class Real { private int getDigits(byte[] digits, int base) { if (base == 10) { - { - tmp1.mantissa = this.mantissa; - tmp1.exponent = this.exponent; - tmp1.sign = this.sign; - } + final Real tmp1 = tmp1(); + tmp1.assign(this); tmp1.abs(); - { - tmp2.mantissa = tmp1.mantissa; - tmp2.exponent = tmp1.exponent; - tmp2.sign = tmp1.sign; - } + final Real tmp2 = tmp2(); + tmp2.assign(tmp1); int exp = exponent = tmp1.lowPow10(); exp -= 18; boolean exp_neg = exp <= 0; @@ -6497,7 +6351,7 @@ public final class Real { } } - private String align(StringBuffer s, NumberFormat format) { + private String align(StringBuilder s, NumberFormat format) { if (format.align == NumberFormat.ALIGN_LEFT) { while (s.length() < format.maxwidth) s.append(' '); @@ -6515,39 +6369,20 @@ public final class Real { } private String ftoa(NumberFormat format) { - ftoaBuf.setLength(0); - if ((this.exponent < 0 && this.mantissa != 0)) { - ftoaBuf.append("nan"); - return align(ftoaBuf, format); + buf.setLength(0); + if (this.exponent < 0 && this.mantissa != 0) { + buf.append("NaN"); + return align(buf, format); } - if ((this.exponent < 0 && this.mantissa == 0)) { - ftoaBuf.append((this.sign != 0) ? "-inf" : "inf"); - return align(ftoaBuf, format); + if (this.exponent < 0 && this.mantissa == 0) { + buf.append((this.sign != 0) ? "-∞" : "∞"); + return align(buf, format); } - int digitsPerThousand; - switch (format.base) { - case 2: - digitsPerThousand = 8; - break; - case 8: - digitsPerThousand = 1000; // Disable thousands separator - break; - case 16: - digitsPerThousand = 4; - break; - case 10: - default: - digitsPerThousand = 3; - break; - } - if (format.thousand == 0) - digitsPerThousand = 1000; // Disable thousands separator - { - tmp4.mantissa = this.mantissa; - tmp4.exponent = this.exponent; - tmp4.sign = this.sign; - } - int accurateDigits = tmp4.getDigits(ftoaDigits, format.base); + + final int digitsPerThousand = digitsPerThousand(format); + final Real tmp = new Real(); + tmp.assign(this); + int accurateDigits = tmp.getDigits(digits, format.base); if (format.base == 10 && (exponent > 0x4000003e || !isIntegral())) accurateDigits = 16; // Only display 16 digits for non-integers int precision; @@ -6557,7 +6392,7 @@ public final class Real { int prefix = 0; if (format.base != 10) prefix = 1; // want room for at least one "0" or "f/7/1" - else if ((tmp4.sign != 0)) + else if ((tmp.sign != 0)) width--; // subtract 1 for sign boolean useExp = false; switch (format.fse) { @@ -6566,7 +6401,7 @@ public final class Real { useExp = true; break; case NumberFormat.FSE_ENG: - pointPos = floorMod(tmp4.exponent, 3); + pointPos = floorMod(tmp.exponent, 3); precision = format.precision + 1 + pointPos; useExp = true; break; @@ -6576,19 +6411,19 @@ public final class Real { precision = 1000; if (format.fse == NumberFormat.FSE_FIX) precision = format.precision + 1; - if (tmp4.exponent + 1 > - width - (tmp4.exponent + prefix) / digitsPerThousand - prefix + + if (tmp.exponent + 1 > + width - (tmp.exponent + prefix) / digitsPerThousand - prefix + (format.removePoint ? 1 : 0) || - tmp4.exponent + 1 > accurateDigits || - -tmp4.exponent >= width || - -tmp4.exponent >= precision) { + tmp.exponent + 1 > accurateDigits || + -tmp.exponent >= width || + -tmp.exponent >= precision) { useExp = true; } else { - pointPos = tmp4.exponent; - precision += tmp4.exponent; - if (tmp4.exponent > 0) - width -= (tmp4.exponent + prefix) / digitsPerThousand; - if (format.removePoint && tmp4.exponent == width - prefix) { + pointPos = tmp.exponent; + precision += tmp.exponent; + if (tmp.exponent > 0) + width -= (tmp.exponent + prefix) / digitsPerThousand; + if (format.removePoint && tmp.exponent == width - prefix) { // Add 1 for the decimal point that will be removed width++; } @@ -6597,11 +6432,11 @@ public final class Real { } if (prefix != 0 && pointPos >= 0) width -= prefix; - ftoaExp.setLength(0); + exp.setLength(0); if (useExp) { - ftoaExp.append('E'); - ftoaExp.append(tmp4.exponent - pointPos); - width -= ftoaExp.length(); + exp.append('e'); + exp.append(tmp.exponent - pointPos); + width -= exp.length(); } if (precision > accurateDigits) precision = accurateDigits; @@ -6612,58 +6447,75 @@ public final class Real { if (precision <= 0) precision = 1; } - while (tmp4.carryWhenRounded(ftoaDigits, precision, format.base)); - tmp4.round(ftoaDigits, precision, format.base); + while (tmp.carryWhenRounded(digits, precision, format.base)); + tmp.round(digits, precision, format.base); // Start generating the string. First the sign - if ((tmp4.sign != 0) && format.base == 10) - ftoaBuf.append('-'); + if ((tmp.sign != 0) && format.base == 10) + buf.append('-'); // Save pointPos for hex/oct/bin prefixing with thousands-sep int pointPos2 = pointPos < 0 ? 0 : pointPos; // Add leading zeros (or f/7/1) - char prefixChar = (format.base == 10 || (tmp4.sign == 0)) ? '0' : + char prefixChar = (format.base == 10 || (tmp.sign == 0)) ? '0' : hexChar.charAt(format.base - 1); if (pointPos < 0) { - ftoaBuf.append(prefixChar); - ftoaBuf.append(format.point); + buf.append(prefixChar); + buf.append(format.point); while (pointPos < -1) { - ftoaBuf.append(prefixChar); + buf.append(prefixChar); pointPos++; } } // Add fractional part for (int i = 0; i < precision; i++) { - ftoaBuf.append(hexChar.charAt(ftoaDigits[i])); + buf.append(hexChar.charAt(digits[i])); if (pointPos > 0 && pointPos % digitsPerThousand == 0) - ftoaBuf.append(format.thousand); + buf.append(format.thousand); if (pointPos == 0) - ftoaBuf.append(format.point); + buf.append(format.point); pointPos--; } if (format.fse == NumberFormat.FSE_NONE) { // Remove trailing zeros - while (ftoaBuf.charAt(ftoaBuf.length() - 1) == '0') - ftoaBuf.setLength(ftoaBuf.length() - 1); + while (buf.charAt(buf.length() - 1) == '0') + buf.setLength(buf.length() - 1); } if (format.removePoint) { // Remove trailing point - if (ftoaBuf.charAt(ftoaBuf.length() - 1) == format.point) - ftoaBuf.setLength(ftoaBuf.length() - 1); + if (buf.charAt(buf.length() - 1) == format.point) + buf.setLength(buf.length() - 1); } // Add exponent - ftoaBuf.append(ftoaExp.toString()); + buf.append(exp); // In case hex/oct/bin number, prefix with 0's or f/7/1's if (format.base != 10) { - while (ftoaBuf.length() < format.maxwidth) { + while (buf.length() < format.maxwidth) { pointPos2++; if (pointPos2 > 0 && pointPos2 % digitsPerThousand == 0) - ftoaBuf.insert(0, format.thousand); - if (ftoaBuf.length() < format.maxwidth) - ftoaBuf.insert(0, prefixChar); + buf.insert(0, format.thousand); + if (buf.length() < format.maxwidth) + buf.insert(0, prefixChar); } - if (ftoaBuf.charAt(0) == format.thousand) - ftoaBuf.deleteCharAt(0); + if (buf.charAt(0) == format.thousand) + buf.deleteCharAt(0); + } + return align(buf, format); + } + + private int digitsPerThousand(NumberFormat format) { + if (format.thousand == 0) { + return 1000; // Disable thousands separator + } + switch (format.base) { + case 2: + return 4; + case 8: + return 4; + case 16: + return 2; + case 10: + default: + return 3; } - return align(ftoaBuf, format); } /** @@ -6686,8 +6538,9 @@ public final class Real { * @return a String representation of this Real. */ public String toString() { - tmpFormat.base = 10; - return ftoa(tmpFormat); + final NumberFormat format = new NumberFormat(); + format.base = 10; + return ftoa(format); } /** @@ -6720,8 +6573,9 @@ public final class Real { * @return a String representation of this Real. */ public String toString(int base) { - tmpFormat.base = base; - return ftoa(tmpFormat); + final NumberFormat format = new NumberFormat(); + format.base = base; + return ftoa(format); } /** @@ -6951,4 +6805,4 @@ public final class Real { */ public int align = ALIGN_NONE; } -} +} \ No newline at end of file diff --git a/jscl/src/main/java/org/solovyev/common/NumberFormatter.java b/jscl/src/main/java/org/solovyev/common/NumberFormatter.java new file mode 100644 index 00000000..dbfbfcc5 --- /dev/null +++ b/jscl/src/main/java/org/solovyev/common/NumberFormatter.java @@ -0,0 +1,136 @@ +package org.solovyev.common; + +import midpcalc.Real; + +import javax.annotation.Nonnull; +import java.math.BigDecimal; + +import static midpcalc.Real.NumberFormat.*; + +public class NumberFormatter { + + public static final int NO_GROUPING = 0; + public static final int DEFAULT_PRECISION = 16; + public static final int DEFAULT_MAGNITUDE = 5; + + private final Real.NumberFormat numberFormat = new Real.NumberFormat(); + private final Real real = new Real(); + private int format = FSE_NONE; + private int simpleFormatMagnitude = DEFAULT_MAGNITUDE; + private int precision = DEFAULT_PRECISION; + private char groupingSeparator; + + public void useEngineeringFormat(int simpleFormatMagnitude) { + this.format = FSE_ENG; + this.simpleFormatMagnitude = simpleFormatMagnitude; + } + + public void useSimpleFormat() { + this.format = FSE_NONE; + this.simpleFormatMagnitude = DEFAULT_MAGNITUDE; + } + + public void setPrecision(int precision) { + this.precision = precision; + } + + public void setGroupingSeparator(char groupingSeparator) { + this.groupingSeparator = groupingSeparator; + } + + @Nonnull + public CharSequence format(double value) { + return format(value, 10); + } + + @Nonnull + public CharSequence format(double value, int radix) { + if (radix != 2 && radix != 8 && radix != 10 && radix != 16) { + throw new IllegalArgumentException("Unsupported radix: " + radix); + } + double absValue = Math.abs(value); + final boolean dec = radix == 10; + final boolean fixedFormat = !dec || format == FSE_NONE || Math.pow(10, -simpleFormatMagnitude) <= absValue && absValue < Math.pow(10, simpleFormatMagnitude); + + if (fixedFormat) { + final int newScale = (int) (precision * Math.max(1, radix / 10f)); + value = BigDecimal.valueOf(value).setScale(newScale, BigDecimal.ROUND_HALF_UP).doubleValue(); + absValue = Math.abs(value); + } + numberFormat.fse = fixedFormat ? FSE_FIX : FSE_ENG; + numberFormat.thousand = groupingSeparator; + numberFormat.precision = precision; + numberFormat.base = radix; + numberFormat.maxwidth = fixedFormat ? 100 : 30; + + if (radix == 2 && value < 0) { + return "-" + prepare(absValue); + } + return prepare(value); + } + + @Nonnull + private CharSequence prepare(double value) { + return stripZeros(realFormat(value)).replace('e', 'E'); + } + + @Nonnull + private String realFormat(double value) { + real.assign(Double.toString(value)); + return real.toString(numberFormat); + } + + @Nonnull + private String stripZeros(@Nonnull String s) { + int dot = -1; + int firstNonZero = -1; + for (int i = 0; i < s.length(); i++) { + final char c = s.charAt(i); + if (c != '0' && c != groupingSeparator && firstNonZero == -1) { + firstNonZero = i; + } + if (c == '.') { + dot = i; + break; + } + } + if (firstNonZero == -1) { + // all zeros + return ""; + } + if (dot < 0) { + // no dot - no trailing zeros + return s.substring(firstNonZero); + } + if (firstNonZero == dot) { + // one zero before dot must be kept + firstNonZero--; + } + final int e = s.lastIndexOf('e'); + final int i = findLastNonZero(s, e); + final int end = i == dot ? i : i + 1; + return s.substring(firstNonZero, end) + getExponent(s, e); + } + + @Nonnull + private String getExponent(@Nonnull String s, int e) { + String exponent = ""; + if (e > 0) { + exponent = s.substring(e); + if (exponent.length() == 2 && exponent.charAt(1) == '0') { + exponent = ""; + } + } + return exponent; + } + + private int findLastNonZero(@Nonnull String s, int e) { + int i = e > 0 ? e - 1 : s.length() - 1; + for (; i >= 0; i--) { + if (s.charAt(i) != '0') { + break; + } + } + return i; + } +} diff --git a/jscl/src/test/java/jscl/JsclMathEngineTest.java b/jscl/src/test/java/jscl/JsclMathEngineTest.java index 3423f4f9..7abad5e0 100644 --- a/jscl/src/test/java/jscl/JsclMathEngineTest.java +++ b/jscl/src/test/java/jscl/JsclMathEngineTest.java @@ -34,24 +34,23 @@ public class JsclMathEngineTest { me.setPrecision(10); assertEquals("111 1111 0011 0110", me.format(32566d, NumeralBase.bin)); - assertEquals("100.0100 1100 11", me.format(4.3d, NumeralBase.bin)); - assertEquals("1 0001 0101 0011.0101 0101 10", me.format(4435.33423d, NumeralBase.bin)); - assertEquals("1100.0101 0101 01", me.format(12.3333d, NumeralBase.bin)); - assertEquals("1 0011 1101 1110 0100 0011 0101 0101.0001 1111 00", me.format(333333333.1212213321d, NumeralBase.bin)); + assertEquals("100.0100110011", me.format(4.3d, NumeralBase.bin)); + assertEquals("1 0001 0101 0011.010101011", me.format(4435.33423d, NumeralBase.bin)); + assertEquals("1100.0101010101", me.format(12.3333d, NumeralBase.bin)); + assertEquals("1 0011 1101 1110 0100 0011 0101 0101.00011111", me.format(333333333.1212213321d, NumeralBase.bin)); - assertEquals("0.EE EE EE EE EE", me.format(14d / 15d, NumeralBase.hex)); + assertEquals("0.EEEEEEEEEF", me.format(14d / 15d, NumeralBase.hex)); assertEquals("7F 36", me.format(32566d, NumeralBase.hex)); assertEquals("24", me.format(36d, NumeralBase.hex)); assertEquals("8", me.format(8d, NumeralBase.hex)); assertEquals("1 3D", me.format(317d, NumeralBase.hex)); - assertEquals("13 DE 43 55.1F 08 5B EF 14", me.format(333333333.1212213321d, NumeralBase.hex)); - assertEquals("D 25 0F 77 0A.6F 73 18 FC 50", me.format(56456345354.43534534523459999d, NumeralBase.hex)); - assertEquals("3 E7.4C CC CC CC CC", me.format(999.3d, NumeralBase.hex)); + assertEquals("13 DE 43 55.1F085BEF", me.format(333333333.1212213321d, NumeralBase.hex)); + assertEquals("D 25 0F 77 0A.6F7319", me.format(56456345354.43534534523459999d, NumeralBase.hex)); + assertEquals("3 E7.4CCCCCCCCD", me.format(999.3d, NumeralBase.hex)); me.setRoundResult(false); - assertEquals("0.00 00 00 00 00 00 00 00 00 6C", me.format(0.00000000000000000000009d, NumeralBase.hex)); - assertEquals("0.00 00 00 00 00 00 00 00 00 0A", me.format(0.000000000000000000000009d, NumeralBase.hex)); - + assertEquals("6.CCDA6A054226DB6E-19", me.format(0.00000000000000000000009d, NumeralBase.hex)); + assertEquals("A.E15D766ED03E2BEE-20", me.format(0.000000000000000000000009d, NumeralBase.hex)); } finally { me.setUseGroupingSeparator(false); } @@ -80,7 +79,7 @@ public class JsclMathEngineTest { @Test public void testPiComputation() throws Exception { final JsclMathEngine me = JsclMathEngine.getInstance(); - assertEquals("-1+122.46467991473532E-18*i", me.evaluate("exp(√(-1)*Π)")); + assertEquals("-1+0.0000000000000001*i", me.evaluate("exp(√(-1)*Π)")); } @Test @@ -101,8 +100,8 @@ public class JsclMathEngineTest { assertEquals("111E3", me.format(111000d)); assertEquals("110E3", me.format(110000d)); assertEquals("100E3", me.format(100000d)); - assertEquals("10E3", me.format(10000d)); - assertEquals("1E3", me.format(1000d)); + assertEquals("10000", me.format(10000d)); + assertEquals("1000", me.format(1000d)); assertEquals("100", me.format(100d)); assertEquals("100.1", me.format(100.1d)); assertEquals("100.12", me.format(100.12d)); @@ -122,8 +121,8 @@ public class JsclMathEngineTest { assertEquals("0.001", me.format(0.001)); assertEquals("0.001", me.format(0.00100000001)); assertEquals("0.0011", me.format(0.0011)); - assertEquals("999.999E-6", me.format(0.000999999)); - assertEquals("100E-6", me.format(0.0001)); + assertEquals("0.001", me.format(0.000999999)); + assertEquals("0.0001", me.format(0.0001)); assertEquals("1E-6", me.format(0.000001)); assertEquals("10E-9", me.format(0.00000001)); @@ -156,8 +155,8 @@ public class JsclMathEngineTest { assertEquals("111E3", me.format(111000d)); assertEquals("110E3", me.format(110000d)); assertEquals("100E3", me.format(100000d)); - assertEquals("10E3", me.format(10000d)); - assertEquals("1E3", me.format(1000d)); + assertEquals("10000", me.format(10000d)); + assertEquals("1000", me.format(1000d)); assertEquals("100", me.format(100d)); assertEquals("100.1", me.format(100.1d)); assertEquals("100.12", me.format(100.12d)); @@ -176,8 +175,8 @@ public class JsclMathEngineTest { assertEquals("0.01", me.format(0.01)); assertEquals("0.001", me.format(0.001)); assertEquals("0.0011", me.format(0.0011)); - assertEquals("999.999E-6", me.format(0.000999999)); - assertEquals("100E-6", me.format(0.0001)); + assertEquals("0.000999999", me.format(0.000999999)); + assertEquals("0.0001", me.format(0.0001)); assertEquals("100.001E3", me.format(100001d)); assertEquals("111.111E3", me.format(111111d)); diff --git a/jscl/src/test/java/jscl/NumeralBaseTest.java b/jscl/src/test/java/jscl/NumeralBaseTest.java index 57ef0be2..055d68b9 100644 --- a/jscl/src/test/java/jscl/NumeralBaseTest.java +++ b/jscl/src/test/java/jscl/NumeralBaseTest.java @@ -4,25 +4,22 @@ import jscl.math.function.Constant; import jscl.math.function.ExtendedConstant; import jscl.math.function.IConstant; import jscl.text.ParseException; -import junit.framework.Assert; +import org.junit.Assert; import org.junit.Test; -/** - * User: serso - * Date: 11/29/11 - * Time: 12:20 PM - */ +import static org.junit.Assert.assertEquals; + public class NumeralBaseTest { @Test public void testEvaluation() throws Exception { MathEngine me = JsclMathEngine.getInstance(); - Assert.assertEquals("3", me.evaluate("0b:1+0b:10")); - Assert.assertEquals("5", me.evaluate("0b:1+0b:100")); - Assert.assertEquals("8", me.evaluate("0b:1+0b:100+(0b:1+0b:10)")); - Assert.assertEquals("18", me.evaluate("0b:1+0b:100+(0b:1+0b:10)+10")); - Assert.assertEquals("18.5", me.evaluate("0b:1+0b:100+(0b:1+0b:10)+10.5")); + assertEquals("3", me.evaluate("0b:1+0b:10")); + assertEquals("5", me.evaluate("0b:1+0b:100")); + assertEquals("8", me.evaluate("0b:1+0b:100+(0b:1+0b:10)")); + assertEquals("18", me.evaluate("0b:1+0b:100+(0b:1+0b:10)+10")); + assertEquals("18.5", me.evaluate("0b:1+0b:100+(0b:1+0b:10)+10.5")); try { me.evaluate("0b:1+0b:100.+(0b:1+0b:10)+10.5"); Assert.fail(); @@ -35,7 +32,7 @@ public class NumeralBaseTest { } catch (ParseException e) { } - Assert.assertEquals("2748", me.evaluate("0x:ABC")); + assertEquals("2748", me.evaluate("0x:ABC")); try { me.evaluate("0x:"); @@ -43,15 +40,15 @@ public class NumeralBaseTest { } catch (ParseException e) { } - Assert.assertEquals("0", me.evaluate("0x:0")); + assertEquals("0", me.evaluate("0x:0")); IConstant constant = null; try { final ExtendedConstant.Builder a = new ExtendedConstant.Builder(new Constant("a"), 2d); constant = me.getConstantsRegistry().addOrUpdate(a.create()); - Assert.assertEquals("2748", me.evaluate("0x:ABC")); - Assert.assertEquals("5496", me.evaluate("0x:ABC*a")); - Assert.assertEquals("27480", me.evaluate("0x:ABC*0x:A")); + assertEquals("2748", me.evaluate("0x:ABC")); + assertEquals("5496", me.evaluate("0x:ABC*a")); + assertEquals("27480", me.evaluate("0x:ABC*0x:A")); } finally { if (constant != null) { final ExtendedConstant.Builder a = new ExtendedConstant.Builder(new Constant("a"), (String) null); @@ -67,24 +64,24 @@ public class NumeralBaseTest { final NumeralBase defaultNumeralBase = me.getNumeralBase(); try { me.setNumeralBase(NumeralBase.bin); - Assert.assertEquals("∞", me.evaluate("∞")); - Assert.assertEquals("-1011010+110101111.10000110101100011010*i", me.evaluate("asin(-1110100101)")); - Assert.assertEquals("11", me.evaluate("0b:1+0b:10")); - Assert.assertEquals("10", me.evaluate("0d:2")); - Assert.assertEquals("11", me.evaluate("0d:3")); - Assert.assertEquals("100", me.evaluate("0d:4")); - Assert.assertEquals("11111111", me.evaluate("0d:255")); - Assert.assertEquals("11", me.evaluate("1+10")); - Assert.assertEquals("-1", me.evaluate("1-10")); - Assert.assertEquals("11-i", me.evaluate("1+i+10-10*i")); - Assert.assertEquals("11111110", me.evaluate("111001+11000101")); - Assert.assertEquals("1101100100101111", me.evaluate("11011001001011110/10")); - Assert.assertEquals("1001000011001010", me.evaluate("11011001001011110/11")); - Assert.assertEquals("0.10101010101010101010", me.evaluate("10/11")); + assertEquals("∞", me.evaluate("∞")); + assertEquals("-1011010+110101111.100001101011001*i", me.evaluate("asin(-1110100101)")); + assertEquals("11", me.evaluate("0b:1+0b:10")); + assertEquals("10", me.evaluate("0d:2")); + assertEquals("11", me.evaluate("0d:3")); + assertEquals("100", me.evaluate("0d:4")); + assertEquals("11111111", me.evaluate("0d:255")); + assertEquals("11", me.evaluate("1+10")); + assertEquals("-1", me.evaluate("1-10")); + assertEquals("11-i", me.evaluate("1+i+10-10*i")); + assertEquals("11111110", me.evaluate("111001+11000101")); + assertEquals("1101100100101111", me.evaluate("11011001001011110/10")); + assertEquals("1001000011001010", me.evaluate("11011001001011110/11")); + assertEquals("0.1010101010101011", me.evaluate("10/11")); me.setNumeralBase(NumeralBase.hex); - org.junit.Assert.assertEquals("637B", me.evaluate("56CE+CAD")); - org.junit.Assert.assertEquals("637B", me.simplify("56CE+CAD")); + assertEquals("637B", me.evaluate("56CE+CAD")); + assertEquals("637B", me.simplify("56CE+CAD")); } finally { me.setNumeralBase(defaultNumeralBase); diff --git a/jscl/src/test/java/jscl/math/ExpressionTest.java b/jscl/src/test/java/jscl/math/ExpressionTest.java index d1d13116..8ea51f24 100644 --- a/jscl/src/test/java/jscl/math/ExpressionTest.java +++ b/jscl/src/test/java/jscl/math/ExpressionTest.java @@ -8,7 +8,6 @@ import jscl.math.function.Constant; import jscl.math.function.ExtendedConstant; import jscl.math.function.IConstant; import jscl.text.ParseException; -import org.junit.Assert; import org.junit.Test; import javax.annotation.Nonnull; @@ -21,9 +20,7 @@ import java.net.URL; import java.net.URLConnection; import java.util.Set; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; +import static org.junit.Assert.*; public class ExpressionTest { @@ -102,7 +99,7 @@ public class ExpressionTest { @Test public void testImag() throws Exception { - Assert.assertEquals("-i", Expression.valueOf("i^3").numeric().toString()); + assertEquals("-i", Expression.valueOf("i^3").numeric().toString()); } @Test @@ -127,9 +124,9 @@ public class ExpressionTest { final Expression expression = Expression.valueOf("2*t_0+5*t_1"); - Assert.assertEquals("7", expression.substitute(new Constant("t_1"), Expression.valueOf(1.0)).numeric().toString()); - Assert.assertEquals("12", expression.substitute(new Constant("t_1"), Expression.valueOf(2.0)).numeric().toString()); - Assert.assertEquals("27", expression.substitute(new Constant("t_1"), Expression.valueOf(5.0)).numeric().toString()); + assertEquals("7", expression.substitute(new Constant("t_1"), Expression.valueOf(1.0)).numeric().toString()); + assertEquals("12", expression.substitute(new Constant("t_1"), Expression.valueOf(2.0)).numeric().toString()); + assertEquals("27", expression.substitute(new Constant("t_1"), Expression.valueOf(5.0)).numeric().toString()); } finally { if (constant != null) { @@ -141,138 +138,138 @@ public class ExpressionTest { @Test public void testExpressions() throws Exception { - Assert.assertEquals("3", Expression.valueOf("3").numeric().toString()); - Assert.assertEquals("0.6931471805599453", Expression.valueOf("ln(2)").numeric().toString()); - Assert.assertEquals("1", Expression.valueOf("lg(10)").numeric().toString()); - Assert.assertEquals("0", Expression.valueOf("eq(0, 1)").numeric().toString()); - Assert.assertEquals("1", Expression.valueOf("eq(1, 1)").numeric().toString()); + assertEquals("3", Expression.valueOf("3").numeric().toString()); + assertEquals("0.6931471805599453", Expression.valueOf("ln(2)").numeric().toString()); + assertEquals("1", Expression.valueOf("lg(10)").numeric().toString()); + assertEquals("0", Expression.valueOf("eq(0, 1)").numeric().toString()); + assertEquals("1", Expression.valueOf("eq(1, 1)").numeric().toString()); - Assert.assertEquals("24", Expression.valueOf("4!").numeric().toString()); + assertEquals("24", Expression.valueOf("4!").numeric().toString()); try { Expression.valueOf("(-3+2)!").numeric().toString(); fail(); } catch (ArithmeticException e) { } - Assert.assertEquals("24", Expression.valueOf("(2+2)!").numeric().toString()); - Assert.assertEquals("120", Expression.valueOf("(2+2+1)!").numeric().toString()); - Assert.assertEquals("24", Expression.valueOf("(2.0+2.0)!").numeric().toString()); - Assert.assertEquals("24", Expression.valueOf("4.0!").numeric().toString()); - Assert.assertEquals("48", Expression.valueOf("2*4.0!").numeric().toString()); - Assert.assertEquals("40320", Expression.valueOf("(2*4.0)!").numeric().toString()); + assertEquals("24", Expression.valueOf("(2+2)!").numeric().toString()); + assertEquals("120", Expression.valueOf("(2+2+1)!").numeric().toString()); + assertEquals("24", Expression.valueOf("(2.0+2.0)!").numeric().toString()); + assertEquals("24", Expression.valueOf("4.0!").numeric().toString()); + assertEquals("48", Expression.valueOf("2*4.0!").numeric().toString()); + assertEquals("40320", Expression.valueOf("(2*4.0)!").numeric().toString()); final JsclMathEngine me = JsclMathEngine.getInstance(); final AngleUnit angleUnits = me.getAngleUnits(); try { me.setAngleUnits(AngleUnit.rad); - Assert.assertEquals("-0.9055783620066238", Expression.valueOf("sin(4!)").numeric().toString()); + assertEquals("-0.9055783620066238", Expression.valueOf("sin(4!)").numeric().toString()); } finally { me.setAngleUnits(angleUnits); } - Assert.assertEquals("1", Expression.valueOf("(3.14/3.14)!").numeric().toString()); - Assert.assertEquals("1", Expression.valueOf("2/2!").numeric().toString()); + assertEquals("1", Expression.valueOf("(3.14/3.14)!").numeric().toString()); + assertEquals("1", Expression.valueOf("2/2!").numeric().toString()); try { - Assert.assertEquals("3.141592653589793!", Expression.valueOf("3.141592653589793!").numeric().toString()); + assertEquals("3.141592653589793!", Expression.valueOf("3.141592653589793!").numeric().toString()); fail(); } catch (NotIntegerException e) { } - Assert.assertEquals("0.5235987755982988", Expression.valueOf("3.141592653589793/3!").numeric().toString()); + assertEquals("0.5235987755982988", Expression.valueOf("3.141592653589793/3!").numeric().toString()); try { - Assert.assertEquals("3.141592653589793/3.141592653589793!", Expression.valueOf("3.141592653589793/3.141592653589793!").numeric().toString()); + assertEquals("3.141592653589793/3.141592653589793!", Expression.valueOf("3.141592653589793/3.141592653589793!").numeric().toString()); fail(); } catch (ArithmeticException e) { } try { - Assert.assertEquals("7.2!", Expression.valueOf("7.2!").numeric().toString()); + assertEquals("7.2!", Expression.valueOf("7.2!").numeric().toString()); fail(); } catch (NotIntegerException e) { } try { - Assert.assertEquals("ln(7.2!)", Expression.valueOf("ln(7.2!)").numeric().toString()); + assertEquals("ln(7.2!)", Expression.valueOf("ln(7.2!)").numeric().toString()); fail(); } catch (NotIntegerException e) { } - Assert.assertEquals("ln(7.2!)", Expression.valueOf("ln(7.2!)").simplify().toString()); + assertEquals("ln(7.2!)", Expression.valueOf("ln(7.2!)").simplify().toString()); - Assert.assertEquals("36", Expression.valueOf("3!^2").numeric().toString()); - Assert.assertEquals("1", Expression.valueOf("(π/π)!").numeric().toString()); - Assert.assertEquals("720", Expression.valueOf("(3!)!").numeric().toString()); - Assert.assertEquals("36", Expression.valueOf("3!*3!").numeric().toString()); + assertEquals("36", Expression.valueOf("3!^2").numeric().toString()); + assertEquals("1", Expression.valueOf("(π/π)!").numeric().toString()); + assertEquals("720", Expression.valueOf("(3!)!").numeric().toString()); + assertEquals("36", Expression.valueOf("3!*3!").numeric().toString()); - Assert.assertEquals("100", Expression.valueOf("0.1E3").numeric().toString()); + assertEquals("100", Expression.valueOf("0.1E3").numeric().toString()); final AngleUnit defaultAngleUnits = me.getAngleUnits(); try { me.setAngleUnits(AngleUnit.rad); - Assert.assertEquals("0.017453292519943295", Expression.valueOf("1°").numeric().toString()); - Assert.assertEquals("0.03490658503988659", Expression.valueOf("2°").numeric().toString()); - Assert.assertEquals("0.05235987755982989", Expression.valueOf("3°").numeric().toString()); - Assert.assertEquals("0.26179938779914946", Expression.valueOf("3°*5").numeric().toString()); - Assert.assertEquals("0.0027415567780803775", Expression.valueOf("3°^2").numeric().toString()); - Assert.assertEquals("0.01096622711232151", Expression.valueOf("3!°^2").numeric().toString()); - Assert.assertEquals("0.0009138522593601259", Expression.valueOf("3°°").numeric().toString()); - Assert.assertEquals("0.08726646259971647", Expression.valueOf("5°").numeric().toString()); - Assert.assertEquals("2.0523598775598297", Expression.valueOf("2+3°").numeric().toString()); + assertEquals("0.0174532925199433", Expression.valueOf("1°").numeric().toString()); + assertEquals("0.0349065850398866", Expression.valueOf("2°").numeric().toString()); + assertEquals("0.0523598775598299", Expression.valueOf("3°").numeric().toString()); + assertEquals("0.2617993877991495", Expression.valueOf("3°*5").numeric().toString()); + assertEquals("0.0027415567780804", Expression.valueOf("3°^2").numeric().toString()); + assertEquals("0.0109662271123215", Expression.valueOf("3!°^2").numeric().toString()); + assertEquals("0.0009138522593601", Expression.valueOf("3°°").numeric().toString()); + assertEquals("0.0872664625997165", Expression.valueOf("5°").numeric().toString()); + assertEquals("2.05235987755983", Expression.valueOf("2+3°").numeric().toString()); } finally { me.setAngleUnits(defaultAngleUnits); } try { me.setAngleUnits(AngleUnit.deg); - Assert.assertEquals("1", Expression.valueOf("1°").numeric().toString()); - Assert.assertEquals("2", Expression.valueOf("2°").numeric().toString()); - Assert.assertEquals("3", Expression.valueOf("3°").numeric().toString()); - Assert.assertEquals("15", Expression.valueOf("3°*5").numeric().toString()); - Assert.assertEquals("9", Expression.valueOf("3°^2").numeric().toString()); - Assert.assertEquals("36", Expression.valueOf("3!°^2").numeric().toString()); - Assert.assertEquals("3", Expression.valueOf("3°°").numeric().toString()); - Assert.assertEquals("5", Expression.valueOf("5°").numeric().toString()); - Assert.assertEquals("5", Expression.valueOf("2+3°").numeric().toString()); + assertEquals("1", Expression.valueOf("1°").numeric().toString()); + assertEquals("2", Expression.valueOf("2°").numeric().toString()); + assertEquals("3", Expression.valueOf("3°").numeric().toString()); + assertEquals("15", Expression.valueOf("3°*5").numeric().toString()); + assertEquals("9", Expression.valueOf("3°^2").numeric().toString()); + assertEquals("36", Expression.valueOf("3!°^2").numeric().toString()); + assertEquals("3", Expression.valueOf("3°°").numeric().toString()); + assertEquals("5", Expression.valueOf("5°").numeric().toString()); + assertEquals("5", Expression.valueOf("2+3°").numeric().toString()); } finally { me.setAngleUnits(defaultAngleUnits); } - Assert.assertEquals("6", Expression.valueOf("2*∂(3*x,x)").expand().toString()); - Assert.assertEquals("3", Expression.valueOf("∂(3*x,x)").expand().toString()); - Assert.assertEquals("12", Expression.valueOf("∂(x^3,x,2)").expand().toString()); - Assert.assertEquals("3*a", Expression.valueOf("∂(3*x*a,x)").expand().toString()); - Assert.assertEquals("0", Expression.valueOf("∂(3*x*a,x,0.011,2)").expand().toString()); - Assert.assertEquals("0", Expression.valueOf("2*∂(3*x*a,x,0.011,2)").expand().toString()); - Assert.assertEquals("ln(8)+lg(8)*ln(8)", Expression.valueOf("ln(8)*lg(8)+ln(8)").expand().toString()); - Assert.assertEquals("3.9573643765059856", Expression.valueOf("ln(8)*lg(8)+ln(8)").numeric().toString()); + assertEquals("6", Expression.valueOf("2*∂(3*x,x)").expand().toString()); + assertEquals("3", Expression.valueOf("∂(3*x,x)").expand().toString()); + assertEquals("12", Expression.valueOf("∂(x^3,x,2)").expand().toString()); + assertEquals("3*a", Expression.valueOf("∂(3*x*a,x)").expand().toString()); + assertEquals("0", Expression.valueOf("∂(3*x*a,x,0.011,2)").expand().toString()); + assertEquals("0", Expression.valueOf("2*∂(3*x*a,x,0.011,2)").expand().toString()); + assertEquals("ln(8)+lg(8)*ln(8)", Expression.valueOf("ln(8)*lg(8)+ln(8)").expand().toString()); + assertEquals("3.957364376505986", Expression.valueOf("ln(8)*lg(8)+ln(8)").numeric().toString()); - Assert.assertEquals("4!", Expression.valueOf("4.0!").simplify().toString()); - Assert.assertEquals("4°", Expression.valueOf("4.0°").simplify().toString()); - Assert.assertEquals("30°", Expression.valueOf("30°").simplify().toString()); + assertEquals("4!", Expression.valueOf("4.0!").simplify().toString()); + assertEquals("4°", Expression.valueOf("4.0°").simplify().toString()); + assertEquals("30°", Expression.valueOf("30°").simplify().toString()); - Assert.assertEquals("1", Expression.valueOf("abs(1)").numeric().toString()); - Assert.assertEquals("0", Expression.valueOf("abs(0)").numeric().toString()); - Assert.assertEquals("0", Expression.valueOf("abs(-0)").numeric().toString()); - Assert.assertEquals("1", Expression.valueOf("abs(-1)").numeric().toString()); - Assert.assertEquals("∞", Expression.valueOf("abs(-∞)").numeric().toString()); + assertEquals("1", Expression.valueOf("abs(1)").numeric().toString()); + assertEquals("0", Expression.valueOf("abs(0)").numeric().toString()); + assertEquals("0", Expression.valueOf("abs(-0)").numeric().toString()); + assertEquals("1", Expression.valueOf("abs(-1)").numeric().toString()); + assertEquals("∞", Expression.valueOf("abs(-∞)").numeric().toString()); - Assert.assertEquals("1", Expression.valueOf("abs(i)").numeric().toString()); - Assert.assertEquals("0", Expression.valueOf("abs(0+0*i)").numeric().toString()); - Assert.assertEquals("1", Expression.valueOf("abs(-i)").numeric().toString()); - Assert.assertEquals("2.23606797749979", Expression.valueOf("abs(2-i)").numeric().toString()); - Assert.assertEquals("2.23606797749979", Expression.valueOf("abs(2+i)").numeric().toString()); - Assert.assertEquals("2.8284271247461903", Expression.valueOf("abs(2+2*i)").numeric().toString()); - Assert.assertEquals("2.8284271247461903", Expression.valueOf("abs(2-2*i)").numeric().toString()); + assertEquals("1", Expression.valueOf("abs(i)").numeric().toString()); + assertEquals("0", Expression.valueOf("abs(0+0*i)").numeric().toString()); + assertEquals("1", Expression.valueOf("abs(-i)").numeric().toString()); + assertEquals("2.23606797749979", Expression.valueOf("abs(2-i)").numeric().toString()); + assertEquals("2.23606797749979", Expression.valueOf("abs(2+i)").numeric().toString()); + assertEquals("2.82842712474619", Expression.valueOf("abs(2+2*i)").numeric().toString()); + assertEquals("2.82842712474619", Expression.valueOf("abs(2-2*i)").numeric().toString()); try { final ExtendedConstant.Builder k = new ExtendedConstant.Builder(new Constant("k"), 2.8284271247461903); me.getConstantsRegistry().addOrUpdate(k.create()); - Assert.assertEquals("k", Expression.valueOf("k").numeric().toString()); - Assert.assertEquals("k", Expression.valueOf("k").simplify().toString()); - Assert.assertEquals("k", Expression.valueOf("k").simplify().toString()); - Assert.assertEquals("k^3", Expression.valueOf("k*k*k").simplify().toString()); - Assert.assertEquals("22.627416997969526", Expression.valueOf("k*k*k").numeric().toString()); + assertEquals("k", Expression.valueOf("k").numeric().toString()); + assertEquals("k", Expression.valueOf("k").simplify().toString()); + assertEquals("k", Expression.valueOf("k").simplify().toString()); + assertEquals("k^3", Expression.valueOf("k*k*k").simplify().toString()); + assertEquals("22.62741699796953", Expression.valueOf("k*k*k").numeric().toString()); } finally { final ExtendedConstant.Builder k = new ExtendedConstant.Builder(new Constant("k"), (String) null); me.getConstantsRegistry().addOrUpdate(k.create()); @@ -281,9 +278,9 @@ public class ExpressionTest { try { final ExtendedConstant.Builder k_1 = new ExtendedConstant.Builder(new Constant("k_1"), 3d); me.getConstantsRegistry().addOrUpdate(k_1.create()); - Assert.assertEquals("k_1", Expression.valueOf("k_1").numeric().toString()); - Assert.assertEquals("k_1", Expression.valueOf("k_1[0]").numeric().toString()); - Assert.assertEquals("k_1", Expression.valueOf("k_1[2]").numeric().toString()); + assertEquals("k_1", Expression.valueOf("k_1").numeric().toString()); + assertEquals("k_1", Expression.valueOf("k_1[0]").numeric().toString()); + assertEquals("k_1", Expression.valueOf("k_1[2]").numeric().toString()); } finally { final ExtendedConstant.Builder k_1 = new ExtendedConstant.Builder(new Constant("k_1"), (String) null); me.getConstantsRegistry().addOrUpdate(k_1.create()); @@ -291,67 +288,67 @@ public class ExpressionTest { Generic expression = me.simplifyGeneric("cos(t)+∂(cos(t),t)"); Generic substituted = expression.substitute(new Constant("t"), Expression.valueOf(100d)); - Assert.assertEquals("-1.1584559306791382", substituted.numeric().toString()); + assertEquals("-1.158455930679138", substituted.numeric().toString()); expression = me.simplifyGeneric("abs(t)^2+2!"); substituted = expression.substitute(new Constant("t"), Expression.valueOf(10d)); - Assert.assertEquals("102", substituted.numeric().toString()); + assertEquals("102", substituted.numeric().toString()); expression = me.simplifyGeneric("abs(t)^2+10%"); substituted = expression.substitute(new Constant("t"), Expression.valueOf(10d)); - Assert.assertEquals("110", substituted.numeric().toString()); + assertEquals("110", substituted.numeric().toString()); expression = me.simplifyGeneric("abs(t)^2-10%"); substituted = expression.substitute(new Constant("t"), Expression.valueOf(10d)); - Assert.assertEquals("90", substituted.numeric().toString()); + assertEquals("90", substituted.numeric().toString()); expression = me.simplifyGeneric("(abs(t)^2)*10%"); substituted = expression.substitute(new Constant("t"), Expression.valueOf(10d)); - Assert.assertEquals("10", substituted.numeric().toString()); + assertEquals("10", substituted.numeric().toString()); expression = me.simplifyGeneric("(abs(t)^2)/10%"); substituted = expression.substitute(new Constant("t"), Expression.valueOf(10d)); - Assert.assertEquals("1000", substituted.numeric().toString()); + assertEquals("1000", substituted.numeric().toString()); expression = me.simplifyGeneric("abs(t)^2+t%"); substituted = expression.substitute(new Constant("t"), Expression.valueOf(10d)); - Assert.assertEquals("110", substituted.numeric().toString()); + assertEquals("110", substituted.numeric().toString()); expression = me.simplifyGeneric("abs(t)^2-t%"); substituted = expression.substitute(new Constant("t"), Expression.valueOf(10d)); - Assert.assertEquals("90", substituted.numeric().toString()); + assertEquals("90", substituted.numeric().toString()); expression = me.simplifyGeneric("(abs(t)^2)*t%"); substituted = expression.substitute(new Constant("t"), Expression.valueOf(10d)); - Assert.assertEquals("10", substituted.numeric().toString()); + assertEquals("10", substituted.numeric().toString()); expression = me.simplifyGeneric("(abs(t)^2)/t%"); substituted = expression.substitute(new Constant("t"), Expression.valueOf(10d)); - Assert.assertEquals("1000", substituted.numeric().toString()); + assertEquals("1000", substituted.numeric().toString()); expression = me.simplifyGeneric("Σ(t, t, 0, 10)"); - Assert.assertEquals("55", expression.numeric().toString()); + assertEquals("55", expression.numeric().toString()); expression = me.simplifyGeneric("Σ(t, t, 0, 10)"); substituted = expression.substitute(new Constant("t"), Expression.valueOf(10d)); - Assert.assertEquals("55", substituted.numeric().toString()); + assertEquals("55", substituted.numeric().toString()); expression = me.simplifyGeneric("10*Σ(t, t, 0, 10)"); substituted = expression.substitute(new Constant("t"), Expression.valueOf(10d)); - Assert.assertEquals("550", substituted.numeric().toString()); + assertEquals("550", substituted.numeric().toString()); expression = me.simplifyGeneric("t*Σ(t, t, 0, 10)"); substituted = expression.substitute(new Constant("t"), Expression.valueOf(10d)); - Assert.assertEquals("550", substituted.numeric().toString()); + assertEquals("550", substituted.numeric().toString()); expression = me.simplifyGeneric("t*Σ(t+100%, t, 0, 10)"); substituted = expression.substitute(new Constant("t"), Expression.valueOf(10d)); - Assert.assertEquals("1100", substituted.numeric().toString()); + assertEquals("1100", substituted.numeric().toString()); - Assert.assertEquals("i*t", Expression.valueOf("i*t").expand().simplify().toString()); - Assert.assertEquals("t", Expression.valueOf("t").simplify().toString()); - Assert.assertEquals("t^3", Expression.valueOf("t*t*t").simplify().toString()); + assertEquals("i*t", Expression.valueOf("i*t").expand().simplify().toString()); + assertEquals("t", Expression.valueOf("t").simplify().toString()); + assertEquals("t^3", Expression.valueOf("t*t*t").simplify().toString()); try { Expression.valueOf("t").numeric(); @@ -367,60 +364,60 @@ public class ExpressionTest { } catch (ArithmeticException e) { } - Assert.assertEquals("√(1+t)/(1+t)", Expression.valueOf("1/√(1+t)").simplify().toString()); + assertEquals("√(1+t)/(1+t)", Expression.valueOf("1/√(1+t)").simplify().toString()); - Assert.assertEquals("t", Expression.valueOf("t").simplify().toString()); - Assert.assertEquals("t^3", Expression.valueOf("t*t*t").simplify().toString()); + assertEquals("t", Expression.valueOf("t").simplify().toString()); + assertEquals("t^3", Expression.valueOf("t*t*t").simplify().toString()); try { me.setAngleUnits(AngleUnit.rad); - Assert.assertEquals("0.6931471805599453+Π*i", Expression.valueOf("ln(-2)").numeric().toString()); + assertEquals("0.6931471805599453+Π*i", Expression.valueOf("ln(-2)").numeric().toString()); } finally { me.setAngleUnits(AngleUnit.deg); } - Assert.assertEquals("-2/57", Expression.valueOf("1/(-57/2)").simplify().toString()); - Assert.assertEquals("sin(30)", Expression.valueOf("sin(30)").expand().toString()); - Assert.assertEquals("sin(n)", Expression.valueOf("sin(n)").expand().toString()); - Assert.assertEquals("sin(n!)", Expression.valueOf("sin(n!)").expand().toString()); - Assert.assertEquals("sin(n°)", Expression.valueOf("sin(n°)").expand().toString()); - Assert.assertEquals("sin(30°)", Expression.valueOf("sin(30°)").expand().toString()); - Assert.assertEquals("0.49999999999999994", Expression.valueOf("sin(30°)").expand().numeric().toString()); - Assert.assertEquals("sin(2!)", Expression.valueOf("sin(2!)").expand().toString()); + assertEquals("-2/57", Expression.valueOf("1/(-57/2)").simplify().toString()); + assertEquals("sin(30)", Expression.valueOf("sin(30)").expand().toString()); + assertEquals("sin(n)", Expression.valueOf("sin(n)").expand().toString()); + assertEquals("sin(n!)", Expression.valueOf("sin(n!)").expand().toString()); + assertEquals("sin(n°)", Expression.valueOf("sin(n°)").expand().toString()); + assertEquals("sin(30°)", Expression.valueOf("sin(30°)").expand().toString()); + assertEquals("0.4999999999999999", Expression.valueOf("sin(30°)").expand().numeric().toString()); + assertEquals("sin(2!)", Expression.valueOf("sin(2!)").expand().toString()); - Assert.assertEquals("12", Expression.valueOf("3*(3+1)").expand().toString()); - Assert.assertEquals("114.59155902616465", Expression.valueOf("deg(2)").numeric().toString()); + assertEquals("12", Expression.valueOf("3*(3+1)").expand().toString()); + assertEquals("114.5915590261647", Expression.valueOf("deg(2)").numeric().toString()); try { - Assert.assertEquals("-0.1425465430742778", Expression.valueOf("∏(tan(3))").numeric().toString()); + assertEquals("-0.1425465430742778", Expression.valueOf("∏(tan(3))").numeric().toString()); fail(); } catch (ParseException e) { } try { - Assert.assertEquals("-0.14255", Expression.valueOf("sin(2,2)").expand().numeric().toString()); + assertEquals("-0.14255", Expression.valueOf("sin(2,2)").expand().numeric().toString()); fail(); } catch (ParseException e) { } try { - Assert.assertEquals("114.59155902616465", Expression.valueOf("deg(2,2)").numeric().toString()); + assertEquals("114.59155902616465", Expression.valueOf("deg(2,2)").numeric().toString()); fail(); } catch (ParseException e) { } - Assert.assertEquals("0.49999999999999994", Expression.valueOf("sin(30°)").numeric().toString()); - Assert.assertEquals("π", Expression.valueOf("√(π)^2").simplify().toString()); - Assert.assertEquals("π", Expression.valueOf("√(π^2)").simplify().toString()); - Assert.assertEquals("π^2", Expression.valueOf("√(π^2*π^2)").simplify().toString()); - Assert.assertEquals("π^3", Expression.valueOf("√(π^4*π^2)").simplify().toString()); - Assert.assertEquals("e*π^2", Expression.valueOf("√(π^4*e^2)").simplify().toString()); + assertEquals("0.4999999999999999", Expression.valueOf("sin(30°)").numeric().toString()); + assertEquals("π", Expression.valueOf("√(π)^2").simplify().toString()); + assertEquals("π", Expression.valueOf("√(π^2)").simplify().toString()); + assertEquals("π^2", Expression.valueOf("√(π^2*π^2)").simplify().toString()); + assertEquals("π^3", Expression.valueOf("√(π^4*π^2)").simplify().toString()); + assertEquals("e*π^2", Expression.valueOf("√(π^4*e^2)").simplify().toString()); - Assert.assertEquals("1", Expression.valueOf("(π/π)!").numeric().toString()); + assertEquals("1", Expression.valueOf("(π/π)!").numeric().toString()); // in deg mode π=180 and factorial of 180 is calculating - Assert.assertEquals("0", Expression.valueOf("Π/Π!").numeric().toString()); + assertEquals("0", Expression.valueOf("Π/Π!").numeric().toString()); - Assert.assertEquals("122.46467991473532E-18*i", Expression.valueOf("exp((Π*i))+1").numeric().toString()); - Assert.assertEquals("20*x^3", Expression.valueOf("∂(5*x^4, x)").expand().simplify().toString()); - Assert.assertEquals("25*x", Expression.valueOf("5*x*5").expand().simplify().toString()); - Assert.assertEquals("20*x", Expression.valueOf("5*x*4").expand().simplify().toString()); + assertEquals("0.0000000000000001*i", Expression.valueOf("exp((Π*i))+1").numeric().toString()); + assertEquals("20*x^3", Expression.valueOf("∂(5*x^4, x)").expand().simplify().toString()); + assertEquals("25*x", Expression.valueOf("5*x*5").expand().simplify().toString()); + assertEquals("20*x", Expression.valueOf("5*x*4").expand().simplify().toString()); try { me.evaluate("0b:π"); @@ -439,11 +436,11 @@ public class ExpressionTest { try { me.setNumeralBase(NumeralBase.hex); - Assert.assertEquals("0.EEEEEEEEEEEEEC880AB7", me.evaluate("0x:E/0x:F")); - Assert.assertEquals("E/F", me.simplify("0x:E/0x:F")); + assertEquals("0.EEEEEEEEEEEEEC88", me.evaluate("0x:E/0x:F")); + assertEquals("E/F", me.simplify("0x:E/0x:F")); - Assert.assertEquals("0.EEEEEEEEEEEEEC880AB7", me.evaluate("E/F")); - Assert.assertEquals("E/F", me.simplify("E/F")); + assertEquals("0.EEEEEEEEEEEEEC88", me.evaluate("E/F")); + assertEquals("E/F", me.simplify("E/F")); } finally { me.setNumeralBase(NumeralBase.dec); @@ -451,58 +448,58 @@ public class ExpressionTest { try { me.setAngleUnits(AngleUnit.rad); - Assert.assertEquals("-1.5707963267948966+2.993222846126381*i", me.evaluate("asin(-10)")); - Assert.assertEquals("-1.5707963267948966+1.3169578969248166*i", me.evaluate("asin(-2)")); - Assert.assertEquals("-1.5707963267948966", me.evaluate("asin(-1)")); - Assert.assertEquals("0", me.evaluate("asin(0)")); - Assert.assertEquals("1.5707963267948966", me.evaluate("asin(1)")); - Assert.assertEquals("1.5707963267948966-1.3169578969248166*i", me.evaluate("asin(2)")); - Assert.assertEquals("1.5707963267948966-2.993222846126381*i", me.evaluate("asin(10)")); + assertEquals("-1.570796326794897+2.993222846126381*i", me.evaluate("asin(-10)")); + assertEquals("-1.570796326794897+1.316957896924817*i", me.evaluate("asin(-2)")); + assertEquals("-1.570796326794897", me.evaluate("asin(-1)")); + assertEquals("0", me.evaluate("asin(0)")); + assertEquals("1.570796326794897", me.evaluate("asin(1)")); + assertEquals("1.570796326794897-1.316957896924817*i", me.evaluate("asin(2)")); + assertEquals("1.570796326794897-2.993222846126381*i", me.evaluate("asin(10)")); - Assert.assertEquals("Π-2.9932228461263786*i", me.evaluate("acos(-10)")); - Assert.assertEquals("Π-1.3169578969248164*i", me.evaluate("acos(-2)")); - Assert.assertEquals("Π", me.evaluate("acos(-1)")); - Assert.assertEquals("1.5707963267948966", me.evaluate("acos(0)")); - Assert.assertEquals("0", me.evaluate("acos(1)")); - Assert.assertEquals("1.3169578969248164*i", me.evaluate("acos(2)")); - Assert.assertEquals("2.9932228461263786*i", me.evaluate("acos(10)")); + assertEquals("Π-2.993222846126379*i", me.evaluate("acos(-10)")); + assertEquals("Π-1.316957896924816*i", me.evaluate("acos(-2)")); + assertEquals("Π", me.evaluate("acos(-1)")); + assertEquals("1.570796326794897", me.evaluate("acos(0)")); + assertEquals("0", me.evaluate("acos(1)")); + assertEquals("1.316957896924816*i", me.evaluate("acos(2)")); + assertEquals("2.993222846126379*i", me.evaluate("acos(10)")); - Assert.assertEquals("-1.4711276743037347", me.evaluate("atan(-10)")); - Assert.assertEquals("-1.1071487177940904", me.evaluate("atan(-2)")); - Assert.assertEquals("-0.7853981633974483", me.evaluate("atan(-1)")); - Assert.assertEquals("0", me.evaluate("atan(0)")); - Assert.assertEquals("0.7853981633974483", me.evaluate("atan(1)")); - Assert.assertEquals("1.1071487177940904", me.evaluate("atan(2)")); - Assert.assertEquals("1.4711276743037347", me.evaluate("atan(10)")); + assertEquals("-1.471127674303735", me.evaluate("atan(-10)")); + assertEquals("-1.10714871779409", me.evaluate("atan(-2)")); + assertEquals("-0.7853981633974483", me.evaluate("atan(-1)")); + assertEquals("0", me.evaluate("atan(0)")); + assertEquals("0.7853981633974483", me.evaluate("atan(1)")); + assertEquals("1.10714871779409", me.evaluate("atan(2)")); + assertEquals("1.471127674303735", me.evaluate("atan(10)")); for (int i = -10; i < 10; i++) { - Assert.assertEquals(me.evaluate("3.14159265358979323846/2 - atan(" + i + ")"), me.evaluate("acot(" + i + ")")); + assertEquals(me.evaluate("3.14159265358979323846/2 - atan(" + i + ")"), me.evaluate("acot(" + i + ")")); } - Assert.assertEquals("3.0419240010986313", me.evaluate("3.14159265358979323846/2 - atan(-10)")); - Assert.assertEquals("3.0419240010986313", me.evaluate("acot(-10)")); - Assert.assertEquals("1.5707963267948966", me.evaluate("acot(0)")); - Assert.assertEquals("2.677945044588987", me.evaluate("acot(-2)")); - Assert.assertEquals("2.356194490192345", me.evaluate("acot(-1)")); - Assert.assertEquals("0.7853981633974483", me.evaluate("acot(1)")); - Assert.assertEquals("0.46364760900080615", me.evaluate("acot(2)")); - Assert.assertEquals("0.09966865249116186", me.evaluate("acot(10)")); + assertEquals("3.041924001098631", me.evaluate("3.14159265358979323846/2 - atan(-10)")); + assertEquals("3.041924001098631", me.evaluate("acot(-10)")); + assertEquals("1.570796326794897", me.evaluate("acot(0)")); + assertEquals("2.677945044588987", me.evaluate("acot(-2)")); + assertEquals("2.356194490192345", me.evaluate("acot(-1)")); + assertEquals("0.7853981633974483", me.evaluate("acot(1)")); + assertEquals("0.4636476090008062", me.evaluate("acot(2)")); + assertEquals("0.0996686524911619", me.evaluate("acot(10)")); - Assert.assertEquals("Π", me.evaluate("π")); - Assert.assertEquals("Π", me.evaluate("3.14159265358979323846")); + assertEquals("Π", me.evaluate("π")); + assertEquals("Π", me.evaluate("3.14159265358979323846")); } finally { me.setAngleUnits(AngleUnit.deg); } - Assert.assertEquals("180", me.evaluate("Π")); - Assert.assertEquals("180", me.evaluate("200-10%")); + assertEquals("180", me.evaluate("Π")); + assertEquals("180", me.evaluate("200-10%")); - Assert.assertEquals("∞", me.evaluate("1/0")); - Assert.assertEquals("-∞", me.evaluate("-1/0")); - Assert.assertEquals("-∞", me.evaluate("-1/0")); - Assert.assertEquals("∞", me.evaluate("(1 + 2) / (5 - 3 - 2)")); - Assert.assertEquals("∞", me.evaluate("(1 + 2) / (5.1 - 3.1 - 2.0 )")); - Assert.assertEquals("∞", me.evaluate("1/0")); + assertEquals("∞", me.evaluate("1/0")); + assertEquals("-∞", me.evaluate("-1/0")); + assertEquals("-∞", me.evaluate("-1/0")); + assertEquals("∞", me.evaluate("(1 + 2) / (5 - 3 - 2)")); + assertEquals("∞", me.evaluate("(1 + 2) / (5.1 - 3.1 - 2.0 )")); + assertEquals("∞", me.evaluate("1/0")); } @Test @@ -523,111 +520,111 @@ public class ExpressionTest { try { mathEngine.setAngleUnits(AngleUnit.rad); - Assert.assertEquals("Π", mathEngine.evaluate("π")); - Assert.assertEquals("π/2", mathEngine.simplify("π/2")); - Assert.assertEquals(mathEngine.evaluate("0.9092974268256816953960198659117448427022549714478902683789"), mathEngine.evaluate("sin(2)")); - Assert.assertEquals(mathEngine.evaluate("0.1411200080598672221007448028081102798469332642522655841518"), mathEngine.evaluate("sin(3)")); - Assert.assertEquals(mathEngine.evaluate("0"), mathEngine.evaluate("sin(0)")); + assertEquals("Π", mathEngine.evaluate("π")); + assertEquals("π/2", mathEngine.simplify("π/2")); + assertEquals(mathEngine.evaluate("0.9092974268256816953960198659117448427022549714478902683789"), mathEngine.evaluate("sin(2)")); + assertEquals(mathEngine.evaluate("0.1411200080598672221007448028081102798469332642522655841518"), mathEngine.evaluate("sin(3)")); + assertEquals(mathEngine.evaluate("0"), mathEngine.evaluate("sin(0)")); - Assert.assertEquals(mathEngine.evaluate("1"), mathEngine.evaluate("cos(0)")); - Assert.assertEquals(mathEngine.evaluate("0.8623188722876839341019385139508425355100840085355108292801"), mathEngine.evaluate("cos(100)")); - Assert.assertEquals(mathEngine.evaluate("-0.416146836547142386997568229500762189766000771075544890755"), mathEngine.evaluate("cos(2)")); + assertEquals(mathEngine.evaluate("1"), mathEngine.evaluate("cos(0)")); + assertEquals(mathEngine.evaluate("0.8623188722876839341019385139508425355100840085355108292801"), mathEngine.evaluate("cos(100)")); + assertEquals(mathEngine.evaluate("-0.416146836547142386997568229500762189766000771075544890755"), mathEngine.evaluate("cos(2)")); - Assert.assertEquals(mathEngine.evaluate("-2.185039863261518991643306102313682543432017746227663164562"), mathEngine.evaluate("tan(2)")); - Assert.assertEquals(mathEngine.evaluate("-0.142546543074277805295635410533913493226092284901804647633"), mathEngine.evaluate("tan(3)")); - Assert.assertEquals(mathEngine.evaluate("0.6483608274590872"), mathEngine.evaluate("tan(10)")); + assertEquals(mathEngine.evaluate("-2.185039863261518991643306102313682543432017746227663164562"), mathEngine.evaluate("tan(2)")); + assertEquals(mathEngine.evaluate("-0.142546543074277805295635410533913493226092284901804647633"), mathEngine.evaluate("tan(3)")); + assertEquals(mathEngine.evaluate("0.6483608274590872"), mathEngine.evaluate("tan(10)")); - Assert.assertEquals(mathEngine.evaluate("0.6420926159343306"), mathEngine.evaluate("cot(1)")); - Assert.assertEquals(mathEngine.evaluate("-0.457657554360285763750277410432047276428486329231674329641"), mathEngine.evaluate("cot(2)")); - Assert.assertEquals(mathEngine.evaluate("-7.015252551434533469428551379526476578293103352096353838156"), mathEngine.evaluate("cot(3)")); + assertEquals(mathEngine.evaluate("0.6420926159343306"), mathEngine.evaluate("cot(1)")); + assertEquals(mathEngine.evaluate("-0.457657554360285763750277410432047276428486329231674329641"), mathEngine.evaluate("cot(2)")); + assertEquals(mathEngine.evaluate("-7.015252551434533469428551379526476578293103352096353838156"), mathEngine.evaluate("cot(3)")); } finally { mathEngine.setAngleUnits(defaultAngleUnits); } try { mathEngine.setAngleUnits(AngleUnit.deg); - Assert.assertEquals(mathEngine.evaluate("0.9092974268256816953960198659117448427022549714478902683789"), mathEngine.evaluate("sin(deg(2))")); - Assert.assertEquals(mathEngine.evaluate("0.1411200080598672221007448028081102798469332642522655841518"), mathEngine.evaluate("sin(deg(3))")); - Assert.assertEquals(mathEngine.evaluate("0"), mathEngine.evaluate("sin(deg(0))")); + assertEquals(mathEngine.evaluate("0.9092974268256816953960198659117448427022549714478902683789"), mathEngine.evaluate("sin(deg(2))")); + assertEquals(mathEngine.evaluate("0.1411200080598672221007448028081102798469332642522655841518"), mathEngine.evaluate("sin(deg(3))")); + assertEquals(mathEngine.evaluate("0"), mathEngine.evaluate("sin(deg(0))")); - Assert.assertEquals(mathEngine.evaluate("1"), mathEngine.evaluate("cos(deg(0))")); - Assert.assertEquals(mathEngine.evaluate("0.8623188722876839341019385139508425355100840085355108292801"), mathEngine.evaluate("cos(deg(100))")); - Assert.assertEquals(mathEngine.evaluate("-0.416146836547142386997568229500762189766000771075544890755"), mathEngine.evaluate("cos(deg(2))")); + assertEquals(mathEngine.evaluate("1"), mathEngine.evaluate("cos(deg(0))")); + assertEquals(mathEngine.evaluate("0.8623188722876839341019385139508425355100840085355108292801"), mathEngine.evaluate("cos(deg(100))")); + assertEquals(mathEngine.evaluate("-0.416146836547142386997568229500762189766000771075544890755"), mathEngine.evaluate("cos(deg(2))")); - Assert.assertEquals(mathEngine.evaluate("-2.185039863261518991643306102313682543432017746227663164562"), mathEngine.evaluate("tan(deg(2))")); - Assert.assertEquals(mathEngine.evaluate("-0.142546543074277805295635410533913493226092284901804647633"), mathEngine.evaluate("tan(deg(3))")); - Assert.assertEquals(mathEngine.evaluate("0.6483608274590872"), mathEngine.evaluate("tan(deg(10))")); + assertEquals(mathEngine.evaluate("-2.185039863261518991643306102313682543432017746227663164562"), mathEngine.evaluate("tan(deg(2))")); + assertEquals(mathEngine.evaluate("-0.142546543074277805295635410533913493226092284901804647633"), mathEngine.evaluate("tan(deg(3))")); + assertEquals(mathEngine.evaluate("0.6483608274590872"), mathEngine.evaluate("tan(deg(10))")); - Assert.assertEquals(mathEngine.evaluate("0.6420926159343306"), mathEngine.evaluate("cot(deg(1))")); - Assert.assertEquals(mathEngine.evaluate("-0.457657554360285763750277410432047276428486329231674329641"), mathEngine.evaluate("cot(deg(2))")); - Assert.assertEquals(mathEngine.evaluate("-7.015252551434533469428551379526476578293103352096353838156"), mathEngine.evaluate("cot(deg(3))")); + assertEquals(mathEngine.evaluate("0.6420926159343306"), mathEngine.evaluate("cot(deg(1))")); + assertEquals(mathEngine.evaluate("-0.457657554360285763750277410432047276428486329231674329641"), mathEngine.evaluate("cot(deg(2))")); + assertEquals(mathEngine.evaluate("-7.015252551434533469428551379526476578293103352096353838156"), mathEngine.evaluate("cot(deg(3))")); } finally { mathEngine.setAngleUnits(defaultAngleUnits); } try { mathEngine.setAngleUnits(AngleUnit.rad); - Assert.assertEquals(mathEngine.evaluate("-0.5235987755982989"), mathEngine.evaluate("asin(-0.5)")); - Assert.assertEquals(mathEngine.evaluate("-0.47349551215005636"), mathEngine.evaluate("asin(-0.456)")); - Assert.assertEquals(mathEngine.evaluate("0.32784124364198347"), mathEngine.evaluate("asin(0.322)")); + assertEquals(mathEngine.evaluate("-0.5235987755982989"), mathEngine.evaluate("asin(-0.5)")); + assertEquals(mathEngine.evaluate("-0.47349551215005636"), mathEngine.evaluate("asin(-0.456)")); + assertEquals(mathEngine.evaluate("0.32784124364198347"), mathEngine.evaluate("asin(0.322)")); - Assert.assertEquals(mathEngine.evaluate("1.2429550831529133"), mathEngine.evaluate("acos(0.322)")); - Assert.assertEquals(mathEngine.evaluate("1.5587960387762325"), mathEngine.evaluate("acos(0.012)")); - Assert.assertEquals(mathEngine.evaluate("1.6709637479564563"), mathEngine.evaluate("acos(-0.1)")); + assertEquals(mathEngine.evaluate("1.2429550831529133"), mathEngine.evaluate("acos(0.322)")); + assertEquals(mathEngine.evaluate("1.5587960387762325"), mathEngine.evaluate("acos(0.012)")); + assertEquals(mathEngine.evaluate("1.6709637479564563"), mathEngine.evaluate("acos(-0.1)")); - Assert.assertEquals(mathEngine.evaluate("0.3805063771123649"), mathEngine.evaluate("atan(0.4)")); - Assert.assertEquals(mathEngine.evaluate("0.09966865249116204"), mathEngine.evaluate("atan(0.1)")); - Assert.assertEquals(mathEngine.evaluate("-0.5404195002705842"), mathEngine.evaluate("atan(-0.6)")); + assertEquals(mathEngine.evaluate("0.3805063771123649"), mathEngine.evaluate("atan(0.4)")); + assertEquals(mathEngine.evaluate("0.09966865249116204"), mathEngine.evaluate("atan(0.1)")); + assertEquals(mathEngine.evaluate("-0.5404195002705842"), mathEngine.evaluate("atan(-0.6)")); - Assert.assertEquals(mathEngine.evaluate("1.0603080048781206"), mathEngine.evaluate("acot(0.56)")); + assertEquals(mathEngine.evaluate("1.0603080048781206"), mathEngine.evaluate("acot(0.56)")); // todo serso: wolfram alpha returns -0.790423 instead of 2.3511694068615325 (-PI) - Assert.assertEquals(mathEngine.evaluate("2.3511694068615325"), mathEngine.evaluate("acot(-0.99)")); + assertEquals(mathEngine.evaluate("2.3511694068615325"), mathEngine.evaluate("acot(-0.99)")); // todo serso: wolfram alpha returns -1.373401 instead of 1.7681918866447774 (-PI) - Assert.assertEquals(mathEngine.evaluate("1.7681918866447774"), mathEngine.evaluate("acot(-0.2)")); + assertEquals(mathEngine.evaluate("1.7681918866447774"), mathEngine.evaluate("acot(-0.2)")); } finally { mathEngine.setAngleUnits(defaultAngleUnits); } try { mathEngine.setAngleUnits(AngleUnit.deg); - Assert.assertEquals(mathEngine.evaluate("deg(-0.5235987755982989)"), mathEngine.evaluate("asin(-0.5)")); - Assert.assertEquals(mathEngine.evaluate("-27.129294464583623"), mathEngine.evaluate("asin(-0.456)")); - Assert.assertEquals(mathEngine.evaluate("18.783919611005786"), mathEngine.evaluate("asin(0.322)")); + assertEquals(mathEngine.evaluate("deg(-0.5235987755982989)"), mathEngine.evaluate("asin(-0.5)")); + assertEquals(mathEngine.evaluate("-27.129294464583623"), mathEngine.evaluate("asin(-0.456)")); + assertEquals(mathEngine.evaluate("18.783919611005786"), mathEngine.evaluate("asin(0.322)")); - Assert.assertEquals(mathEngine.evaluate("71.21608038899423"), mathEngine.evaluate("acos(0.322)")); - Assert.assertEquals(mathEngine.evaluate("89.31243414358914"), mathEngine.evaluate("acos(0.012)")); - Assert.assertEquals(mathEngine.evaluate("95.73917047726678"), mathEngine.evaluate("acos(-0.1)")); + assertEquals(mathEngine.evaluate("71.21608038899423"), mathEngine.evaluate("acos(0.322)")); + assertEquals(mathEngine.evaluate("89.31243414358914"), mathEngine.evaluate("acos(0.012)")); + assertEquals(mathEngine.evaluate("95.73917047726678"), mathEngine.evaluate("acos(-0.1)")); - Assert.assertEquals(mathEngine.evaluate("deg(0.3805063771123649)"), mathEngine.evaluate("atan(0.4)")); - Assert.assertEquals(mathEngine.evaluate("deg(0.09966865249116204)"), mathEngine.evaluate("atan(0.1)")); - Assert.assertEquals(mathEngine.evaluate("deg(-0.5404195002705842)"), mathEngine.evaluate("atan(-0.6)")); + assertEquals(mathEngine.evaluate("deg(0.3805063771123649)"), mathEngine.evaluate("atan(0.4)")); + assertEquals(mathEngine.evaluate("deg(0.09966865249116204)"), mathEngine.evaluate("atan(0.1)")); + assertEquals(mathEngine.evaluate("deg(-0.5404195002705842)"), mathEngine.evaluate("atan(-0.6)")); - Assert.assertEquals(mathEngine.evaluate("deg(1.0603080048781206)"), mathEngine.evaluate("acot(0.56)")); + assertEquals(mathEngine.evaluate("deg(1.0603080048781206)"), mathEngine.evaluate("acot(0.56)")); // todo serso: wolfram alpha returns -0.790423 instead of 2.3511694068615325 (-PI) - Assert.assertEquals(mathEngine.evaluate("134.7120839334429"), mathEngine.evaluate("acot(-0.99)")); + assertEquals(mathEngine.evaluate("134.7120839334429"), mathEngine.evaluate("acot(-0.99)")); // todo serso: wolfram alpha returns -1.373401 instead of 1.7681918866447774 (-PI) - Assert.assertEquals(mathEngine.evaluate("deg(1.7681918866447774)"), mathEngine.evaluate("acot(-0.2)")); + assertEquals(mathEngine.evaluate("deg(1.7681918866447774)"), mathEngine.evaluate("acot(-0.2)")); } finally { mathEngine.setAngleUnits(defaultAngleUnits); } try { mathEngine.setAngleUnits(AngleUnit.deg); - Assert.assertEquals(mathEngine.evaluate("0.0348994967025009716459951816253329373548245760432968714250"), mathEngine.evaluate("(sin(2))")); - Assert.assertEquals(mathEngine.evaluate("0.0523359562429438327221186296090784187310182539401649204835"), mathEngine.evaluate("(sin(3))")); - Assert.assertEquals(mathEngine.evaluate("0"), mathEngine.evaluate("sin(0)")); + assertEquals(mathEngine.evaluate("0.0348994967025009716459951816253329373548245760432968714250"), mathEngine.evaluate("(sin(2))")); + assertEquals(mathEngine.evaluate("0.0523359562429438327221186296090784187310182539401649204835"), mathEngine.evaluate("(sin(3))")); + assertEquals(mathEngine.evaluate("0"), mathEngine.evaluate("sin(0)")); - Assert.assertEquals(mathEngine.evaluate("1"), mathEngine.evaluate("cos(0)")); - Assert.assertEquals(mathEngine.evaluate("-0.1736481776669303"), mathEngine.evaluate("(cos(100))")); - Assert.assertEquals(mathEngine.evaluate("0.9993908270190958"), mathEngine.evaluate("(cos(2))")); + assertEquals(mathEngine.evaluate("1"), mathEngine.evaluate("cos(0)")); + assertEquals(mathEngine.evaluate("-0.1736481776669303"), mathEngine.evaluate("(cos(100))")); + assertEquals(mathEngine.evaluate("0.9993908270190958"), mathEngine.evaluate("(cos(2))")); - Assert.assertEquals(mathEngine.evaluate("0.03492076949174773"), mathEngine.evaluate("(tan(2))")); - Assert.assertEquals(mathEngine.evaluate("0.05240777928304121"), mathEngine.evaluate("(tan(3))")); - Assert.assertEquals(mathEngine.evaluate("0.17632698070846498"), mathEngine.evaluate("(tan(10))")); + assertEquals(mathEngine.evaluate("0.03492076949174773"), mathEngine.evaluate("(tan(2))")); + assertEquals(mathEngine.evaluate("0.05240777928304121"), mathEngine.evaluate("(tan(3))")); + assertEquals(mathEngine.evaluate("0.17632698070846498"), mathEngine.evaluate("(tan(10))")); - Assert.assertEquals(mathEngine.evaluate("57.28996163075943"), mathEngine.evaluate("(cot(1))")); - Assert.assertEquals(mathEngine.evaluate("28.636253282915604"), mathEngine.evaluate("(cot(2))")); - Assert.assertEquals(mathEngine.evaluate("19.081136687728208"), mathEngine.evaluate("(cot(3))")); + assertEquals(mathEngine.evaluate("57.28996163075943"), mathEngine.evaluate("(cot(1))")); + assertEquals(mathEngine.evaluate("28.636253282915604"), mathEngine.evaluate("(cot(2))")); + assertEquals(mathEngine.evaluate("19.081136687728208"), mathEngine.evaluate("(cot(3))")); } finally { mathEngine.setAngleUnits(defaultAngleUnits); } @@ -640,7 +637,7 @@ public class ExpressionTest { testSinEqualsToSinh(mathEngine, 6d); testSinEqualsToSinh(mathEngine, -1d, "-0.8414709848078965"); testSinEqualsToSinh(mathEngine, -3.3d, "0.1577456941432482"); - testSinEqualsToSinh(mathEngine, -232.2d, "0.27429486373689577"); + testSinEqualsToSinh(mathEngine, -232.2d, "0.2742948637368958"); } finally { mathEngine.setAngleUnits(defaultAngleUnits); } @@ -648,23 +645,23 @@ public class ExpressionTest { try { mathEngine.setAngleUnits(AngleUnit.deg); testSinEqualsToSinh(mathEngine, 0d); - testSinEqualsToSinh(mathEngine, 1d, "0.01745240643728351"); - testSinEqualsToSinh(mathEngine, 3d, "0.052335956242943835"); - testSinEqualsToSinh(mathEngine, 6d, "0.10452846326765347"); - testSinEqualsToSinh(mathEngine, -1d, "-0.01745240643728351"); - testSinEqualsToSinh(mathEngine, -3.3d, "-0.05756402695956728"); + testSinEqualsToSinh(mathEngine, 1d, "0.0174524064372835"); + testSinEqualsToSinh(mathEngine, 3d, "0.0523359562429438"); + testSinEqualsToSinh(mathEngine, 6d, "0.1045284632676535"); + testSinEqualsToSinh(mathEngine, -1d, "-0.0174524064372835"); + testSinEqualsToSinh(mathEngine, -3.3d, "-0.0575640269595673"); testSinEqualsToSinh(mathEngine, -232.2d, "0.7901550123756904"); - Assert.assertEquals("Π/2", mathEngine.simplify("Π/2")); + assertEquals("Π/2", mathEngine.simplify("Π/2")); } finally { mathEngine.setAngleUnits(defaultAngleUnits); } try { mathEngine.setAngleUnits(AngleUnit.rad); - Assert.assertEquals(mathEngine.evaluate("1.5707963267948966-0.8813735870195429*i"), mathEngine.evaluate("acos(i)")); - Assert.assertEquals(mathEngine.evaluate("0.9045568943023814-1.0612750619050357*i"), mathEngine.evaluate("acos(1+i)")); - Assert.assertEquals(mathEngine.evaluate("0.9999999999999999-0.9999999999999998*i"), mathEngine.evaluate("cos(acos(1-i))")); - Assert.assertEquals(mathEngine.evaluate("-0.9045568943023814-1.0612750619050355*i"), mathEngine.evaluate("-acos(1-i)")); + assertEquals(mathEngine.evaluate("1.5707963267948966-0.8813735870195429*i"), mathEngine.evaluate("acos(i)")); + assertEquals(mathEngine.evaluate("0.9045568943023814-1.0612750619050357*i"), mathEngine.evaluate("acos(1+i)")); + assertEquals(mathEngine.evaluate("0.9999999999999999-0.9999999999999998*i"), mathEngine.evaluate("cos(acos(1-i))")); + assertEquals(mathEngine.evaluate("-0.9045568943023814-1.0612750619050355*i"), mathEngine.evaluate("-acos(1-i)")); } finally { mathEngine.setAngleUnits(defaultAngleUnits); } @@ -677,12 +674,12 @@ public class ExpressionTest { private void testSinEqualsToSinh(@Nonnull MathEngine mathEngine, @Nonnull Double x, @Nullable String expected) throws ParseException { if (expected == null) { - Assert.assertEquals(mathEngine.evaluate("sinh(i*" + x + ")/i"), mathEngine.evaluate("sin(" + x + ")")); + assertEquals(mathEngine.evaluate("sinh(i*" + x + ")/i"), mathEngine.evaluate("sin(" + x + ")")); // Assert.assertEquals(mathEngine.evaluate("exp("+x+")-sinh(" + x + ")"), mathEngine.evaluate("cosh(" + x + ")")); } else { - Assert.assertEquals(expected, mathEngine.evaluate("sin(" + x + ")")); - Assert.assertEquals(expected, mathEngine.evaluate("(exp(i * " + x + ") - cos(" + x + "))/i")); - Assert.assertEquals(expected, mathEngine.evaluate("(exp(i * " + x + ") - cos(" + x + "))/i")); + assertEquals(expected, mathEngine.evaluate("sin(" + x + ")")); + assertEquals(expected, mathEngine.evaluate("(exp(i * " + x + ") - cos(" + x + "))/i")); + assertEquals(expected, mathEngine.evaluate("(exp(i * " + x + ") - cos(" + x + "))/i")); } } @@ -693,32 +690,32 @@ public class ExpressionTest { @Test public void testIntegrals() throws Exception { - Assert.assertEquals("50", Expression.valueOf("∫ab(x, x, 0, 10)").expand().numeric().toString()); - Assert.assertEquals("1/2*a^2", Expression.valueOf("∫ab(x, x, 0, a)").expand().toString()); + assertEquals("50", Expression.valueOf("∫ab(x, x, 0, 10)").expand().numeric().toString()); + assertEquals("1/2*a^2", Expression.valueOf("∫ab(x, x, 0, a)").expand().toString()); try { - Assert.assertEquals("∫ab(x, x, 0)", Expression.valueOf("∫ab(x, x, 0)").expand().toString()); + assertEquals("∫ab(x, x, 0)", Expression.valueOf("∫ab(x, x, 0)").expand().toString()); fail(); } catch (ParseException e) { } try { - Assert.assertEquals("∫ab(x, x)", Expression.valueOf("∫ab(x, x)").expand().simplify().toString()); + assertEquals("∫ab(x, x)", Expression.valueOf("∫ab(x, x)").expand().simplify().toString()); fail(); } catch (ParseException e) { } - Assert.assertEquals("x^2/2", Expression.valueOf("∫(x, x)").expand().simplify().toString()); + assertEquals("x^2/2", Expression.valueOf("∫(x, x)").expand().simplify().toString()); try { - Assert.assertEquals("x^2/2", Expression.valueOf("∫(x, x)").expand().numeric().toString()); + assertEquals("x^2/2", Expression.valueOf("∫(x, x)").expand().numeric().toString()); fail(); } catch (ArithmeticException e) { } - Assert.assertEquals("x^2/2", Expression.valueOf("∫(x, x)").expand().simplify().toString()); - Assert.assertEquals("ln(x)", Expression.valueOf("∫(1/x, x)").expand().simplify().toString()); + assertEquals("x^2/2", Expression.valueOf("∫(x, x)").expand().simplify().toString()); + assertEquals("ln(x)", Expression.valueOf("∫(1/x, x)").expand().simplify().toString()); try { JsclMathEngine.getInstance().setAngleUnits(AngleUnit.rad); - Assert.assertEquals("2*ln(2)+ln(cosh(x))", Expression.valueOf("∫(tanh(x), x)").expand().simplify().toString()); - Assert.assertEquals("2*ln(2)+ln(sin(x))", Expression.valueOf("∫(cot(x), x)").expand().simplify().toString()); - Assert.assertEquals("-2*ln(2)-ln(cos(x))", Expression.valueOf("∫(tan(x), x)").expand().simplify().toString()); + assertEquals("2*ln(2)+ln(cosh(x))", Expression.valueOf("∫(tanh(x), x)").expand().simplify().toString()); + assertEquals("2*ln(2)+ln(sin(x))", Expression.valueOf("∫(cot(x), x)").expand().simplify().toString()); + assertEquals("-2*ln(2)-ln(cos(x))", Expression.valueOf("∫(tan(x), x)").expand().simplify().toString()); } finally { JsclMathEngine.getInstance().setAngleUnits(AngleUnit.deg); } @@ -729,12 +726,12 @@ public class ExpressionTest { final AngleUnit defaultAngleUnits = JsclMathEngine.getInstance().getAngleUnits(); try { JsclMathEngine.getInstance().setAngleUnits(AngleUnit.rad); - Assert.assertEquals("-0.9092974268256817", Expression.valueOf("∂(cos(t),t,2)").numeric().toString()); - Assert.assertEquals("∂(cos(t), t, 2, 1)", Expression.valueOf("∂(cos(t),t,2)").simplify().toString()); - Assert.assertEquals("-2.234741690198506", Expression.valueOf("∂(t*cos(t),t,2)").numeric().toString()); - Assert.assertEquals("-4.469483380397012", Expression.valueOf("2*∂(t*cos(t),t,2)").numeric().toString()); - Assert.assertEquals("-sin(2)", Expression.valueOf("∂(cos(t),t,2)").expand().toString()); - Assert.assertEquals("-sin(t)", Expression.valueOf("∂(cos(t),t)").expand().toString()); + assertEquals("-0.9092974268256817", Expression.valueOf("∂(cos(t),t,2)").numeric().toString()); + assertEquals("∂(cos(t), t, 2, 1)", Expression.valueOf("∂(cos(t),t,2)").simplify().toString()); + assertEquals("-2.234741690198506", Expression.valueOf("∂(t*cos(t),t,2)").numeric().toString()); + assertEquals("-4.469483380397012", Expression.valueOf("2*∂(t*cos(t),t,2)").numeric().toString()); + assertEquals("-sin(2)", Expression.valueOf("∂(cos(t),t,2)").expand().toString()); + assertEquals("-sin(t)", Expression.valueOf("∂(cos(t),t)").expand().toString()); assertEquals("-sin(t)", Expression.valueOf("∂(cos(t),t,t,1)").expand().simplify().toString()); assertEquals("∂(cos(t), t, t, 1°)", Expression.valueOf("∂(cos(t),t,t,1°)").expand().simplify().toString()); } finally { @@ -746,18 +743,18 @@ public class ExpressionTest { @Test public void testSum() throws Exception { - Assert.assertEquals("3", Expression.valueOf("Σ(n,n,1,2)").expand().toString()); - Assert.assertEquals("200", Expression.valueOf("Σ(n/n,n,1,200)").expand().toString()); - Assert.assertEquals("1/3", Expression.valueOf("Σ((n-1)/(n+1),n,1,2)").expand().toString()); - Assert.assertEquals("sin(1)", Expression.valueOf("Σ(sin(n),n,1,1)").expand().toString()); - Assert.assertEquals("1/1!", Expression.valueOf("Σ(n/n!,n,1,1)").expand().toString()); - Assert.assertEquals("2", Expression.valueOf("Σ(n/n!,n,1,2)").expand().numeric().toString()); - Assert.assertEquals("2.7182818284590455", Expression.valueOf("Σ(n/n!,n,1,200)").expand().numeric().toString()); - Assert.assertEquals("2.718281828459046", Expression.valueOf("Σ(n/(2*n/2)!,n,1,200)").expand().numeric().toString()); - Assert.assertEquals(Expression.valueOf("3").numeric().toString(), Expression.valueOf("Σ(n°,n,1,2)").expand().numeric().toString()); - Assert.assertEquals("200", Expression.valueOf("Σ(n°/n°,n,1,200)").expand().numeric().toString()); - Assert.assertEquals("-sin(1)-sin(2)", Expression.valueOf("Σ(∂(cos(t),t,n),n,1,2)").expand().toString()); - Assert.assertEquals("-0.05235190313978448", Expression.valueOf("Σ(∂(cos(t),t,n),n,1,2)").expand().numeric().toString()); + assertEquals("3", Expression.valueOf("Σ(n,n,1,2)").expand().toString()); + assertEquals("200", Expression.valueOf("Σ(n/n,n,1,200)").expand().toString()); + assertEquals("1/3", Expression.valueOf("Σ((n-1)/(n+1),n,1,2)").expand().toString()); + assertEquals("sin(1)", Expression.valueOf("Σ(sin(n),n,1,1)").expand().toString()); + assertEquals("1/1!", Expression.valueOf("Σ(n/n!,n,1,1)").expand().toString()); + assertEquals("2", Expression.valueOf("Σ(n/n!,n,1,2)").expand().numeric().toString()); + assertEquals("2.718281828459046", Expression.valueOf("Σ(n/n!,n,1,200)").expand().numeric().toString()); + assertEquals("2.718281828459046", Expression.valueOf("Σ(n/(2*n/2)!,n,1,200)").expand().numeric().toString()); + assertEquals(Expression.valueOf("3").numeric().toString(), Expression.valueOf("Σ(n°,n,1,2)").expand().numeric().toString()); + assertEquals("200", Expression.valueOf("Σ(n°/n°,n,1,200)").expand().numeric().toString()); + assertEquals("-sin(1)-sin(2)", Expression.valueOf("Σ(∂(cos(t),t,n),n,1,2)").expand().toString()); + assertEquals("-0.0523519031397845", Expression.valueOf("Σ(∂(cos(t),t,n),n,1,2)").expand().numeric().toString()); } @Test @@ -766,19 +763,19 @@ public class ExpressionTest { //final NumeralBase defaultNumeralBase = me.getDefaultNumeralBase(); try { //me.setDefaultNumeralBase(NumeralBase.bin); - Assert.assertEquals("10", me.evaluate("0b:01010")); - Assert.assertEquals("10", me.evaluate("0b:1010")); - Assert.assertEquals("520", me.evaluate("0o:1010")); - Assert.assertEquals("1010", me.evaluate("1010")); - Assert.assertEquals("1010.1", me.evaluate("1010.1")); + assertEquals("10", me.evaluate("0b:01010")); + assertEquals("10", me.evaluate("0b:1010")); + assertEquals("520", me.evaluate("0o:1010")); + assertEquals("1010", me.evaluate("1010")); + assertEquals("1010.1", me.evaluate("1010.1")); } finally { //me.setDefaultNumeralBase(defaultNumeralBase); } try { me.setNumeralBase(NumeralBase.hex); - Assert.assertEquals("22F", me.evaluate("22F*exp(F)/exp(F)")); - Assert.assertEquals("E", me.evaluate("E")); + assertEquals("22F", me.evaluate("22F*exp(F)/exp(F)")); + assertEquals("E", me.evaluate("E")); } finally { me.setNumeralBase(NumeralBase.dec); @@ -790,48 +787,48 @@ public class ExpressionTest { final JsclMathEngine me = JsclMathEngine.getInstance(); try { me.setUseGroupingSeparator(true); - Assert.assertEquals("123 456.7891011", Expression.valueOf("123456.7891011").numeric().toString()); - Assert.assertEquals("123 456.7891011", Expression.valueOf("123456.7891011").simplify().toString()); - Assert.assertEquals("123 456.78910111231", Expression.valueOf("123456.7891011123123123123123").simplify().toString()); - Assert.assertEquals("1.222E-6", Expression.valueOf("1222/(10^9)").numeric().toString()); - Assert.assertEquals("12 345", JsclInteger.valueOf(12345L).toString()); + assertEquals("123 456.7891011", Expression.valueOf("123456.7891011").numeric().toString()); + assertEquals("123 456.7891011", Expression.valueOf("123456.7891011").simplify().toString()); + assertEquals("123 456.7891011123", Expression.valueOf("123456.7891011123123123123123").simplify().toString()); + assertEquals("0.000001222", Expression.valueOf("1222/(10^9)").numeric().toString()); + assertEquals("12 345", JsclInteger.valueOf(12345L).toString()); me.setScienceNotation(true); - Assert.assertEquals("0", Expression.valueOf("0.0").simplify().toString()); - Assert.assertEquals("1", Expression.valueOf("1.0").simplify().toString()); - Assert.assertEquals("100", Expression.valueOf("100.0").simplify().toString()); + assertEquals("0", Expression.valueOf("0.0").simplify().toString()); + assertEquals("1", Expression.valueOf("1.0").simplify().toString()); + assertEquals("100", Expression.valueOf("100.0").simplify().toString()); me.setScienceNotation(false); me.setRoundResult(true); me.setPrecision(5); - Assert.assertEquals("0", Expression.valueOf("1222/(10^9)").numeric().toString()); + assertEquals("0", Expression.valueOf("1222/(10^9)").numeric().toString()); me.setScienceNotation(true); me.setRoundResult(true); me.setPrecision(5); - Assert.assertEquals("1.222E-6", Expression.valueOf("1222/(10^9)").numeric().toString()); + assertEquals("1.222E-6", Expression.valueOf("1222/(10^9)").numeric().toString()); me.setRoundResult(true); me.setPrecision(10); - Assert.assertEquals("1.222E-6", Expression.valueOf("1222/(10^9)").numeric().toString()); + assertEquals("1.222E-6", Expression.valueOf("1222/(10^9)").numeric().toString()); me.setRoundResult(false); - Assert.assertEquals("1.222E-6", Expression.valueOf("1222/(10^9)").numeric().toString()); + assertEquals("1.222E-6", Expression.valueOf("1222/(10^9)").numeric().toString()); me.setScienceNotation(false); - Assert.assertEquals("0.3333333333333333", Expression.valueOf("1/3").numeric().toString()); + assertEquals("0.3333333333333333", Expression.valueOf("1/3").numeric().toString()); me.setScienceNotation(true); - Assert.assertEquals("0.3333333333333333", Expression.valueOf("1/3").numeric().toString()); + assertEquals("0.3333333333333333", Expression.valueOf("1/3").numeric().toString()); me.setRoundResult(true); me.setPrecision(10); - Assert.assertEquals("0.3333333333", Expression.valueOf("1/3").numeric().toString()); + assertEquals("0.3333333333", Expression.valueOf("1/3").numeric().toString()); me.setScienceNotation(false); me.setRoundResult(true); me.setPrecision(10); - Assert.assertEquals("0.3333333333", Expression.valueOf("1/3").numeric().toString()); + assertEquals("0.3333333333", Expression.valueOf("1/3").numeric().toString()); } finally { me.setUseGroupingSeparator(false); diff --git a/jscl/src/test/java/jscl/math/function/CustomFunctionTest.java b/jscl/src/test/java/jscl/math/function/CustomFunctionTest.java index 6a08b0fc..009a91fc 100644 --- a/jscl/src/test/java/jscl/math/function/CustomFunctionTest.java +++ b/jscl/src/test/java/jscl/math/function/CustomFunctionTest.java @@ -9,6 +9,8 @@ import org.junit.Test; import java.util.Arrays; +import static org.junit.Assert.assertEquals; + /** * User: serso * Date: 11/15/11 @@ -20,20 +22,20 @@ public class CustomFunctionTest { public void testLog() throws Exception { JsclMathEngine mathEngine = JsclMathEngine.getInstance(); - Assert.assertEquals("∞", Expression.valueOf("1/0").numeric().toString()); - Assert.assertEquals("∞", Expression.valueOf("ln(10)/ln(1)").numeric().toString()); + assertEquals("∞", Expression.valueOf("1/0").numeric().toString()); + assertEquals("∞", Expression.valueOf("ln(10)/ln(1)").numeric().toString()); // logarithm final CustomFunction.Builder jBuilder = new CustomFunction.Builder(true, "log", Arrays.asList("a", "b"), "ln(b)/ln(a)"); Function function = mathEngine.getFunctionsRegistry().addOrUpdate(jBuilder.create()); - Assert.assertEquals("log(a, b)", function.toString()); - Assert.assertEquals("ln(b)/ln(a)", ((CustomFunction) mathEngine.getFunctionsRegistry().get("log")).getContent()); - Assert.assertEquals("∞", Expression.valueOf("log(1, 10)").numeric().toString()); - Assert.assertEquals("3.3219280948873626", Expression.valueOf("log(2, 10)").numeric().toString()); - Assert.assertEquals("1.4306765580733933", Expression.valueOf("log(5, 10)").numeric().toString()); - Assert.assertEquals("0.9602525677891275", Expression.valueOf("log(11, 10)").numeric().toString()); - Assert.assertEquals("1/b*1/ln(a)", Expression.valueOf("∂(log(a, b), b)").expand().toString()); - Assert.assertEquals("-1/a*(1/ln(a))^2*ln(b)", Expression.valueOf("∂(log(a, b), a)").expand().toString()); + assertEquals("log(a, b)", function.toString()); + assertEquals("ln(b)/ln(a)", ((CustomFunction) mathEngine.getFunctionsRegistry().get("log")).getContent()); + assertEquals("∞", Expression.valueOf("log(1, 10)").numeric().toString()); + assertEquals("3.321928094887363", Expression.valueOf("log(2, 10)").numeric().toString()); + assertEquals("1.430676558073393", Expression.valueOf("log(5, 10)").numeric().toString()); + assertEquals("0.9602525677891275", Expression.valueOf("log(11, 10)").numeric().toString()); + assertEquals("1/b*1/ln(a)", Expression.valueOf("∂(log(a, b), b)").expand().toString()); + assertEquals("-1/a*(1/ln(a))^2*ln(b)", Expression.valueOf("∂(log(a, b), a)").expand().toString()); } @@ -43,15 +45,15 @@ public class CustomFunctionTest { final CustomFunction.Builder jBuilder = new CustomFunction.Builder("t1", Arrays.asList("a"), "sin(a)"); mathEngine.getFunctionsRegistry().addOrUpdate(jBuilder.create()); - Assert.assertEquals("1", Expression.valueOf("t1(90)").numeric().toString()); - Assert.assertEquals("cos(t)", Expression.valueOf("∂(t1(t), t)").expand().toString()); - Assert.assertEquals("0", Expression.valueOf("∂(t1(t), t2)").expand().toString()); - Assert.assertEquals("cos(a)", Expression.valueOf("∂(t1(a), a)").expand().toString()); - Assert.assertEquals("1", Expression.valueOf("∂(t1(a), t1(a))").expand().toString()); + assertEquals("1", Expression.valueOf("t1(90)").numeric().toString()); + assertEquals("cos(t)", Expression.valueOf("∂(t1(t), t)").expand().toString()); + assertEquals("0", Expression.valueOf("∂(t1(t), t2)").expand().toString()); + assertEquals("cos(a)", Expression.valueOf("∂(t1(a), a)").expand().toString()); + assertEquals("1", Expression.valueOf("∂(t1(a), t1(a))").expand().toString()); final CustomFunction.Builder jBuilder1 = new CustomFunction.Builder("t2", Arrays.asList("a", "b"), "b*sin(a)"); mathEngine.getFunctionsRegistry().addOrUpdate(jBuilder1.create()); - Assert.assertEquals("y*cos(x)", Expression.valueOf("∂(t2(x, y), x)").expand().toString()); - Assert.assertEquals("sin(x)", Expression.valueOf("∂(t2(x, y), y)").expand().toString()); + assertEquals("y*cos(x)", Expression.valueOf("∂(t2(x, y), x)").expand().toString()); + assertEquals("sin(x)", Expression.valueOf("∂(t2(x, y), y)").expand().toString()); } @Test @@ -60,19 +62,19 @@ public class CustomFunctionTest { final CustomFunction.Builder jBuilder = new CustomFunction.Builder("t1", Arrays.asList("a"), "sin(a)"); mathEngine.getFunctionsRegistry().addOrUpdate(jBuilder.create()); - Assert.assertEquals("1", Expression.valueOf("t1(90)").numeric().toString()); + assertEquals("1", Expression.valueOf("t1(90)").numeric().toString()); try { mathEngine.setAngleUnits(AngleUnit.rad); - Assert.assertEquals("-cos(t)", Expression.valueOf("∫(t1(t), t)").expand().toString()); - Assert.assertEquals("t2*sin(t)", Expression.valueOf("∫(t1(t), t2)").expand().toString()); - Assert.assertEquals("-cos(a)", Expression.valueOf("∫(t1(a), a)").expand().toString()); - Assert.assertEquals("1/2*sin(a)^2", Expression.valueOf("∫(t1(a), t1(a))").expand().toString()); + assertEquals("-cos(t)", Expression.valueOf("∫(t1(t), t)").expand().toString()); + assertEquals("t2*sin(t)", Expression.valueOf("∫(t1(t), t2)").expand().toString()); + assertEquals("-cos(a)", Expression.valueOf("∫(t1(a), a)").expand().toString()); + assertEquals("1/2*sin(a)^2", Expression.valueOf("∫(t1(a), t1(a))").expand().toString()); final CustomFunction.Builder jBuilder1 = new CustomFunction.Builder("t2", Arrays.asList("a", "b"), "b*sin(a)"); mathEngine.getFunctionsRegistry().addOrUpdate(jBuilder1.create()); - Assert.assertEquals("-y*cos(x)", Expression.valueOf("∫(t2(x, y), x)").expand().toString()); - Assert.assertEquals("1/2*y^2*sin(x)", Expression.valueOf("∫(t2(x, y), y)").expand().toString()); + assertEquals("-y*cos(x)", Expression.valueOf("∫(t2(x, y), x)").expand().toString()); + assertEquals("1/2*y^2*sin(x)", Expression.valueOf("∫(t2(x, y), y)").expand().toString()); } finally { mathEngine.setAngleUnits(AngleUnit.deg); } @@ -85,16 +87,16 @@ public class CustomFunctionTest { final CustomFunction.Builder jBuilder = new CustomFunction.Builder("testFunction", Arrays.asList("a", "b", "c", "d"), "b*cos(a)/c+d"); mathEngine.getFunctionsRegistry().addOrUpdate(jBuilder.create()); - Assert.assertEquals("6.749543120264322", Expression.valueOf("testFunction(2, 3, 4, 6)").numeric().toString()); - Assert.assertEquals("7.749543120264322", Expression.valueOf("testFunction(2, 3, 4, 7)").numeric().toString()); - Assert.assertEquals("6.749543120264322", Expression.valueOf("testFunction(2*1, 3, 4, 6)").numeric().toString()); - Assert.assertEquals("6.749543120264322", Expression.valueOf("testFunction(2*1, 3, 4, 3!)").numeric().toString()); - Assert.assertEquals("6.749543120264322", Expression.valueOf("testFunction(2*1, 3, 2^2-1+e^0, 3!)").numeric().toString()); - Assert.assertEquals("testFunction(2, 3, 4, 3!)", Expression.valueOf("testFunction(2*1, 3, 2^2-1+e^0, 3!)").simplify().toString()); - Assert.assertEquals("3*cos(2)/4+3!", Expression.valueOf("testFunction(2*1, 3, 2^2-1+e^0, 3!)").expand().toString()); - Assert.assertEquals("3*(1/2*1/exp(2*i)+1/2*exp(2*i))/4+3!", Expression.valueOf("testFunction(2*1, 3, 2^2-1+e^0, 3!)").elementary().toString()); - Assert.assertEquals("sin(t)^2*testFunction(2, 3, 4, 3!)", Expression.valueOf("sin(t)*testFunction(2*1, 3, 2^2-1+e^0, 3!)*sin(t)").simplify().toString()); - Assert.assertEquals("testFunction(2, 3, 4, 3!)^2", Expression.valueOf("testFunction(2*1, 3, 2^2-1+e^0, 3!)*testFunction(2, 3, 4, 3!)").simplify().toString()); + assertEquals("6.749543120264322", Expression.valueOf("testFunction(2, 3, 4, 6)").numeric().toString()); + assertEquals("7.749543120264322", Expression.valueOf("testFunction(2, 3, 4, 7)").numeric().toString()); + assertEquals("6.749543120264322", Expression.valueOf("testFunction(2*1, 3, 4, 6)").numeric().toString()); + assertEquals("6.749543120264322", Expression.valueOf("testFunction(2*1, 3, 4, 3!)").numeric().toString()); + assertEquals("6.749543120264322", Expression.valueOf("testFunction(2*1, 3, 2^2-1+e^0, 3!)").numeric().toString()); + assertEquals("testFunction(2, 3, 4, 3!)", Expression.valueOf("testFunction(2*1, 3, 2^2-1+e^0, 3!)").simplify().toString()); + assertEquals("3*cos(2)/4+3!", Expression.valueOf("testFunction(2*1, 3, 2^2-1+e^0, 3!)").expand().toString()); + assertEquals("3*(1/2*1/exp(2*i)+1/2*exp(2*i))/4+3!", Expression.valueOf("testFunction(2*1, 3, 2^2-1+e^0, 3!)").elementary().toString()); + assertEquals("sin(t)^2*testFunction(2, 3, 4, 3!)", Expression.valueOf("sin(t)*testFunction(2*1, 3, 2^2-1+e^0, 3!)*sin(t)").simplify().toString()); + assertEquals("testFunction(2, 3, 4, 3!)^2", Expression.valueOf("testFunction(2*1, 3, 2^2-1+e^0, 3!)*testFunction(2, 3, 4, 3!)").simplify().toString()); try { Expression.valueOf("testFunction(2*1, 3, 2^2-1+e^0, 3!)*testFunction(2, 3, 4)"); Assert.fail(); @@ -106,31 +108,31 @@ public class CustomFunctionTest { mathEngine.getConstantsRegistry().addOrUpdate(a.create()); final CustomFunction.Builder jBuilder1 = new CustomFunction.Builder("testFunction2", Arrays.asList("a", "b", "c", "d"), "b*cos(a)/c+d"); mathEngine.getFunctionsRegistry().addOrUpdate(jBuilder1.create()); - Assert.assertEquals("6.749543120264322", Expression.valueOf("testFunction2(2, 3, 4, 6)").numeric().toString()); - Assert.assertEquals("7.749543120264322", Expression.valueOf("testFunction2(2, 3, 4, 7)").numeric().toString()); - Assert.assertEquals("6.749543120264322", Expression.valueOf("testFunction2(2*1, 3, 4, 6)").numeric().toString()); - Assert.assertEquals("6.749543120264322", Expression.valueOf("testFunction2(2*1, 3, 2^2-1+e^0, 3!)").numeric().toString()); + assertEquals("6.749543120264322", Expression.valueOf("testFunction2(2, 3, 4, 6)").numeric().toString()); + assertEquals("7.749543120264322", Expression.valueOf("testFunction2(2, 3, 4, 7)").numeric().toString()); + assertEquals("6.749543120264322", Expression.valueOf("testFunction2(2*1, 3, 4, 6)").numeric().toString()); + assertEquals("6.749543120264322", Expression.valueOf("testFunction2(2*1, 3, 2^2-1+e^0, 3!)").numeric().toString()); final CustomFunction.Builder jBuilder2 = new CustomFunction.Builder("testFunction3", Arrays.asList("a", "b", "c", "d"), "testFunction2(a, b, c, d) - testFunction(a, b, c, d)"); mathEngine.getFunctionsRegistry().addOrUpdate(jBuilder2.create()); - Assert.assertEquals("0", Expression.valueOf("testFunction3(2, 3, 4, 6)").numeric().toString()); - Assert.assertEquals("0", Expression.valueOf("testFunction3(2, 3, 4, 7)").numeric().toString()); - Assert.assertEquals("0", Expression.valueOf("testFunction3(2*1, 3, 4, 6)").numeric().toString()); - Assert.assertEquals("0", Expression.valueOf("testFunction3(2*1, 3, 2^2-1+e^0, 3!)").numeric().toString()); + assertEquals("0", Expression.valueOf("testFunction3(2, 3, 4, 6)").numeric().toString()); + assertEquals("0", Expression.valueOf("testFunction3(2, 3, 4, 7)").numeric().toString()); + assertEquals("0", Expression.valueOf("testFunction3(2*1, 3, 4, 6)").numeric().toString()); + assertEquals("0", Expression.valueOf("testFunction3(2*1, 3, 2^2-1+e^0, 3!)").numeric().toString()); final CustomFunction.Builder jBuilder3 = new CustomFunction.Builder("testFunction4", Arrays.asList("a", "b", "c", "d"), "testFunction2(a, b/2, c/3, d/4) - testFunction(a, b!, c, d)"); mathEngine.getFunctionsRegistry().addOrUpdate(jBuilder3.create()); - Assert.assertEquals("-4.874771560132161", Expression.valueOf("testFunction4(2, 3, 4, 6)").numeric().toString()); - Assert.assertEquals("-5.624771560132161", Expression.valueOf("testFunction4(2, 3, 4, 7)").numeric().toString()); - Assert.assertEquals("-4.874771560132161", Expression.valueOf("testFunction4(2*1, 3, 4, 6)").numeric().toString()); - Assert.assertEquals("-4.874771560132161", Expression.valueOf("testFunction4(2*1, 3, 2^2-1+e^0, 3!)").numeric().toString()); + assertEquals("-4.874771560132161", Expression.valueOf("testFunction4(2, 3, 4, 6)").numeric().toString()); + assertEquals("-5.624771560132161", Expression.valueOf("testFunction4(2, 3, 4, 7)").numeric().toString()); + assertEquals("-4.874771560132161", Expression.valueOf("testFunction4(2*1, 3, 4, 6)").numeric().toString()); + assertEquals("-4.874771560132161", Expression.valueOf("testFunction4(2*1, 3, 2^2-1+e^0, 3!)").numeric().toString()); final CustomFunction.Builder jBuilder4 = new CustomFunction.Builder("testFunction5", Arrays.asList("a", "b"), "testFunction2(a, b/2, 2, 1) - testFunction(a, b!, 4!, 1)"); mathEngine.getFunctionsRegistry().addOrUpdate(jBuilder4.create()); - Assert.assertEquals("0.4996954135095478", Expression.valueOf("testFunction5(2, 3)").numeric().toString()); - Assert.assertEquals("0.4996954135095478", Expression.valueOf("testFunction5(2, 3)").numeric().toString()); - Assert.assertEquals("0.4996954135095478", Expression.valueOf("testFunction5(2*1, 3)").numeric().toString()); - Assert.assertEquals("-111.02230246251565E-18", Expression.valueOf("testFunction5(2*1, 2^2-1+e^0)").numeric().toString()); + assertEquals("0.4996954135095478", Expression.valueOf("testFunction5(2, 3)").numeric().toString()); + assertEquals("0.4996954135095478", Expression.valueOf("testFunction5(2, 3)").numeric().toString()); + assertEquals("0.4996954135095478", Expression.valueOf("testFunction5(2*1, 3)").numeric().toString()); + assertEquals("-0.0000000000000001", Expression.valueOf("testFunction5(2*1, 2^2-1+e^0)").numeric().toString()); try { Expression.valueOf("testFunction5(2, 3.5)").numeric(); @@ -141,19 +143,19 @@ public class CustomFunctionTest { final CustomFunction.Builder jBuilder5 = new CustomFunction.Builder("testFunction6", Arrays.asList("a", "b"), "testFunction(a, b!, 4!, Π)"); mathEngine.getFunctionsRegistry().addOrUpdate(jBuilder5.create()); - Assert.assertEquals("180.24984770675476", Expression.valueOf("testFunction6(2, 3)").numeric().toString()); + assertEquals("180.2498477067548", Expression.valueOf("testFunction6(2, 3)").numeric().toString()); final ExtendedConstant.Builder e = new ExtendedConstant.Builder(new Constant("e"), 181d); mathEngine.getConstantsRegistry().addOrUpdate(e.create()); final CustomFunction.Builder jBuilder6 = new CustomFunction.Builder("testFunction7", Arrays.asList("a", "b"), "testFunction(a, b!, 4!, e)"); mathEngine.getFunctionsRegistry().addOrUpdate(jBuilder6.create()); - Assert.assertEquals("181.24984770675476", Expression.valueOf("testFunction7(2, 3)").numeric().toString()); + assertEquals("181.2498477067548", Expression.valueOf("testFunction7(2, 3)").numeric().toString()); final ExtendedConstant.Builder e1 = new ExtendedConstant.Builder(new Constant("e"), 181d); mathEngine.getConstantsRegistry().addOrUpdate(e1.create()); final CustomFunction.Builder jBuilder7 = new CustomFunction.Builder("testFunction8", Arrays.asList("a", "b"), "testFunction(sin(a), b!, 4!, e)"); mathEngine.getFunctionsRegistry().addOrUpdate(jBuilder7.create()); - Assert.assertEquals("181.24999995362296", Expression.valueOf("testFunction8(2, 3)").numeric().toString()); + assertEquals("181.249999953623", Expression.valueOf("testFunction8(2, 3)").numeric().toString()); } @@ -169,17 +171,17 @@ public class CustomFunctionTest { mathEngine.getFunctionsRegistry().addOrUpdate(jBuilder2.create()); try { - Assert.assertEquals("1", Expression.valueOf("f(1, 1)").numeric().toString()); + assertEquals("1", Expression.valueOf("f(1, 1)").numeric().toString()); Assert.fail(); } catch (ArithmeticException e) { //ok } - Assert.assertEquals("1.4142135623730951", Expression.valueOf("f2(1, 1)").numeric().toString()); - Assert.assertEquals("5", Expression.valueOf("f2(4, 3)").numeric().toString()); + assertEquals("1.414213562373095", Expression.valueOf("f2(1, 1)").numeric().toString()); + assertEquals("5", Expression.valueOf("f2(4, 3)").numeric().toString()); - Assert.assertEquals("2*z1", Expression.valueOf("∂(f3(z1, z2), z1)").expand().toString()); - Assert.assertEquals("2*z2", Expression.valueOf("∂(f3(z1, z2), z2)").expand().toString()); + assertEquals("2*z1", Expression.valueOf("∂(f3(z1, z2), z1)").expand().toString()); + assertEquals("2*z2", Expression.valueOf("∂(f3(z1, z2), z2)").expand().toString()); // test symbols final CustomFunction.Builder jBuilder3 = new CustomFunction.Builder("f4", Arrays.asList("x", "y"), "2 000*x^2+y^2"); diff --git a/jscl/src/test/java/jscl/math/function/RadTest.java b/jscl/src/test/java/jscl/math/function/RadTest.java index cbafa4e4..5f9b3719 100644 --- a/jscl/src/test/java/jscl/math/function/RadTest.java +++ b/jscl/src/test/java/jscl/math/function/RadTest.java @@ -1,34 +1,30 @@ package jscl.math.function; import jscl.JsclMathEngine; -import junit.framework.Assert; import org.junit.Test; -/** - * User: serso - * Date: 11/12/11 - * Time: 4:00 PM - */ +import static org.junit.Assert.assertEquals; + public class RadTest { @Test public void testRad() throws Exception { - final JsclMathEngine mathEngine = JsclMathEngine.getInstance(); + final JsclMathEngine mathEngine = new JsclMathEngine(); - Assert.assertEquals("0.03490658503988659", mathEngine.evaluate("rad(2)")); - Assert.assertEquals("0.03490658503988659", mathEngine.evaluate("rad(1+1)")); - Assert.assertEquals("-0.03490658503988659", mathEngine.evaluate("rad(-2)")); - Assert.assertEquals("-0.03490658503988659", mathEngine.evaluate("rad(-1-1)")); - Assert.assertEquals("π", mathEngine.evaluate("rad(180)")); - Assert.assertEquals(String.valueOf(-Math.PI), mathEngine.evaluate("rad(-180)")); + assertEquals("0.0349065850398866", mathEngine.evaluate("rad(2)")); + assertEquals("0.0349065850398866", mathEngine.evaluate("rad(1+1)")); + assertEquals("-0.0349065850398866", mathEngine.evaluate("rad(-2)")); + assertEquals("-0.0349065850398866", mathEngine.evaluate("rad(-1-1)")); + assertEquals("π", mathEngine.evaluate("rad(180)")); + assertEquals(String.valueOf(-Math.PI), mathEngine.evaluate("rad(-180)")); // todo serso: think about zeroes - Assert.assertEquals("rad(-180, 0, 0)", mathEngine.simplify("rad(-180)")); - Assert.assertEquals("rad(2, 0, 0)", mathEngine.simplify("rad(1+1)")); + assertEquals("rad(-180, 0, 0)", mathEngine.simplify("rad(-180)")); + assertEquals("rad(2, 0, 0)", mathEngine.simplify("rad(1+1)")); - Assert.assertEquals("rad(-180, 0, 0)", mathEngine.elementary("rad(-180)")); - Assert.assertEquals("rad(2, 0, 0)", mathEngine.elementary("rad(1+1)")); + assertEquals("rad(-180, 0, 0)", mathEngine.elementary("rad(-180)")); + assertEquals("rad(2, 0, 0)", mathEngine.elementary("rad(1+1)")); - Assert.assertEquals(mathEngine.evaluate("rad(43.1025)"), mathEngine.evaluate("rad(43,6,9)")); - Assert.assertEquals(mathEngine.evaluate("rad(102.765)"), mathEngine.evaluate("rad(102, 45, 54)")); + assertEquals(mathEngine.evaluate("rad(43.1025)"), mathEngine.evaluate("rad(43,6,9)")); + assertEquals(mathEngine.evaluate("rad(102.765)"), mathEngine.evaluate("rad(102, 45, 54)")); } } diff --git a/jscl/src/test/java/jscl/math/function/SqrtTest.java b/jscl/src/test/java/jscl/math/function/SqrtTest.java index ace8e26f..0d365858 100644 --- a/jscl/src/test/java/jscl/math/function/SqrtTest.java +++ b/jscl/src/test/java/jscl/math/function/SqrtTest.java @@ -2,14 +2,10 @@ package jscl.math.function; import jscl.AngleUnit; import jscl.JsclMathEngine; -import junit.framework.Assert; import org.junit.Test; -/** - * User: serso - * Date: 5/14/12 - * Time: 1:15 PM - */ +import static org.junit.Assert.assertEquals; + public class SqrtTest { @Test @@ -17,13 +13,13 @@ public class SqrtTest { final JsclMathEngine me = JsclMathEngine.getInstance(); final AngleUnit defaultAngleUnits = me.getAngleUnits(); - Assert.assertEquals("0.9999060498015505+0.013707354604707477*i", me.evaluate("√(√(-1))")); - Assert.assertEquals("0.9984971498638638+0.05480366514878954*i", me.evaluate("√(√(-1))^4")); + assertEquals("0.9999060498015505+0.0137073546047075*i", me.evaluate("√(√(-1))")); + assertEquals("0.9984971498638638+0.0548036651487895*i", me.evaluate("√(√(-1))^4")); try { me.setAngleUnits(AngleUnit.rad); - Assert.assertEquals("0.7071067811865476+0.7071067811865475*i", me.evaluate("√(√(-1))")); - Assert.assertEquals("-1+277.55575615628914E-18*i", me.evaluate("√(√(-1))^4")); + assertEquals("0.7071067811865476+0.7071067811865475*i", me.evaluate("√(√(-1))")); + assertEquals("-1+0.0000000000000003*i", me.evaluate("√(√(-1))^4")); } finally { me.setAngleUnits(defaultAngleUnits); } diff --git a/jscl/src/test/java/jscl/math/function/trigonometric/CosTest.java b/jscl/src/test/java/jscl/math/function/trigonometric/CosTest.java index 9fe23a6c..1fa9e052 100644 --- a/jscl/src/test/java/jscl/math/function/trigonometric/CosTest.java +++ b/jscl/src/test/java/jscl/math/function/trigonometric/CosTest.java @@ -20,9 +20,9 @@ public class CosTest { me.getConstantsRegistry().addOrUpdate(t.create()); Assert.assertEquals("-sin(t)", me.simplify("∂(cos(t),t,t,1)")); Assert.assertEquals("∂(cos(t), t, t, 1°)", me.simplify("∂(cos(t),t,t,1°)")); - Assert.assertEquals("-0.17364817766693033", me.evaluate("∂(cos(t),t,t,1)")); + Assert.assertEquals("-0.1736481776669303", me.evaluate("∂(cos(t),t,t,1)")); Assert.assertEquals("∂(cos(t), t, t, 1°)", me.evaluate("∂(cos(t),t,t,1°)")); - Assert.assertEquals("-0.17364817766693033", me.evaluate("∂(cos(t),t,t,2-1)")); - Assert.assertEquals("-0.17364817766693033", me.evaluate("∂(cos(t),t,t,2^5-31)")); + Assert.assertEquals("-0.1736481776669303", me.evaluate("∂(cos(t),t,t,2-1)")); + Assert.assertEquals("-0.1736481776669303", me.evaluate("∂(cos(t),t,t,2^5-31)")); } } diff --git a/jscl/src/test/java/jscl/math/function/trigonometric/SinTest.java b/jscl/src/test/java/jscl/math/function/trigonometric/SinTest.java index 02c07ce2..f9285248 100644 --- a/jscl/src/test/java/jscl/math/function/trigonometric/SinTest.java +++ b/jscl/src/test/java/jscl/math/function/trigonometric/SinTest.java @@ -28,7 +28,7 @@ public class SinTest { me.setAngleUnits(AngleUnit.rad); Assert.assertEquals("0.5403023058681398", me.evaluate("cos(1)")); Assert.assertEquals("0.3623577544766736", me.evaluate("cos(1.2)")); - Assert.assertEquals("0.17794455139146614", me.evaluate("∫ab(sin(x), x, 1, 1.2)")); + Assert.assertEquals("0.1779445513914661", me.evaluate("∫ab(sin(x), x, 1, 1.2)")); } finally { me.setAngleUnits(AngleUnit.deg); } diff --git a/jscl/src/test/java/jscl/math/function/trigonometric/TanTest.java b/jscl/src/test/java/jscl/math/function/trigonometric/TanTest.java index 9c79b535..45f1358f 100644 --- a/jscl/src/test/java/jscl/math/function/trigonometric/TanTest.java +++ b/jscl/src/test/java/jscl/math/function/trigonometric/TanTest.java @@ -29,9 +29,9 @@ public class TanTest { me.setAngleUnits(AngleUnit.rad); assertEquals("-2*ln(2)-ln(cos(x))", me.simplify("∫(tan(x), x)")); assertEquals("-(2*ln(2)+ln(cos(x*π)))/π", me.simplify("∫(tan(π*x), x)")); - assertEquals("-0.015308831465985804", me.evaluate("ln(cos(10*π/180))")); + assertEquals("-0.0153088314659858", me.evaluate("ln(cos(10*π/180))")); assertEquals("-0.1438410362258904", me.evaluate("ln(cos(30*π/180))")); - assertEquals("0.12853220475990468", me.evaluate("∫ab(tan(x), x, 10*π/180, 30*π/180)")); + assertEquals("0.1285322047599047", me.evaluate("∫ab(tan(x), x, 10*π/180, 30*π/180)")); } finally { me.setAngleUnits(AngleUnit.deg); } diff --git a/jscl/src/test/java/jscl/math/numeric/ComplexTest.java b/jscl/src/test/java/jscl/math/numeric/ComplexTest.java index 828aa1a1..e884b8fd 100644 --- a/jscl/src/test/java/jscl/math/numeric/ComplexTest.java +++ b/jscl/src/test/java/jscl/math/numeric/ComplexTest.java @@ -7,17 +7,12 @@ import org.junit.Test; import static junit.framework.Assert.assertEquals; -/** - * User: serso - * Date: 5/14/12 - * Time: 2:24 PM - */ public class ComplexTest { @Test public void testSmallImag() throws Exception { - assertEquals("1+100E-18*i", Complex.valueOf(1, 0.0000000000000001).toString()); - assertEquals("1-100E-18*i", Complex.valueOf(1, -0.0000000000000001).toString()); + assertEquals("1+0.0000000000000001*i", Complex.valueOf(1, 0.0000000000000001).toString()); + assertEquals("1-0.0000000000000001*i", Complex.valueOf(1, -0.0000000000000001).toString()); } @Test @@ -25,10 +20,10 @@ public class ComplexTest { try { JsclMathEngine.getInstance().setAngleUnits(AngleUnit.rad); assertEquals("1.543080634815244", Expression.valueOf("cos(i)").numeric().toString()); - assertEquals("1.1752011936438014*i", Expression.valueOf("sin(i)").numeric().toString()); - assertEquals("11013.232874703395*i", Expression.valueOf("sin(10*i)").numeric().toString()); - assertEquals("11013.232920103324", Expression.valueOf("cos(10*i)").numeric().toString()); - assertEquals("0.46211715726000974*i", Expression.valueOf("tan(i/2)").numeric().toString()); + assertEquals("1.175201193643801*i", Expression.valueOf("sin(i)").numeric().toString()); + assertEquals("11013.2328747034*i", Expression.valueOf("sin(10*i)").numeric().toString()); + assertEquals("11013.23292010332", Expression.valueOf("cos(10*i)").numeric().toString()); + assertEquals("0.4621171572600097*i", Expression.valueOf("tan(i/2)").numeric().toString()); assertEquals("-2.163953413738653*i", Expression.valueOf("cot(i/2)").numeric().toString()); } finally { JsclMathEngine.getInstance().setAngleUnits(JsclMathEngine.DEFAULT_ANGLE_UNITS); diff --git a/jscl/src/test/java/jscl/math/operator/PercentTest.java b/jscl/src/test/java/jscl/math/operator/PercentTest.java index 9fdccafb..86755ab3 100644 --- a/jscl/src/test/java/jscl/math/operator/PercentTest.java +++ b/jscl/src/test/java/jscl/math/operator/PercentTest.java @@ -48,7 +48,7 @@ public class PercentTest { Assert.assertEquals("-49", mathEngine.evaluate("1-(100-50%)")); Assert.assertEquals("50", mathEngine.evaluate("100-50%")); Assert.assertEquals("2600", mathEngine.evaluate("100+50%^2")); - Assert.assertEquals("101.08138265680029", mathEngine.evaluate("100+50^2%")); + Assert.assertEquals("101.0813826568003", mathEngine.evaluate("100+50^2%")); Assert.assertEquals("22500", mathEngine.evaluate("(100+50%)^2")); Assert.assertEquals("225", mathEngine.evaluate("(100+50%)+50%")); Assert.assertEquals("225", mathEngine.evaluate("(100+50%)+(abs(-50)+10-10)%")); diff --git a/jscl/src/test/java/jscl/math/operator/SumTest.java b/jscl/src/test/java/jscl/math/operator/SumTest.java index 0e6be747..10ea0106 100644 --- a/jscl/src/test/java/jscl/math/operator/SumTest.java +++ b/jscl/src/test/java/jscl/math/operator/SumTest.java @@ -3,14 +3,10 @@ package jscl.math.operator; import jscl.JsclMathEngine; import jscl.math.function.Constant; import jscl.math.function.ExtendedConstant; -import org.junit.Assert; import org.junit.Test; -/** - * User: serso - * Date: 1/30/12 - * Time: 4:17 PM - */ +import static org.junit.Assert.assertEquals; + public class SumTest { @Test @@ -20,7 +16,7 @@ public class SumTest { me.getConstantsRegistry().addOrUpdate(x.create()); final ExtendedConstant.Builder i = new ExtendedConstant.Builder(new Constant("i"), (String) null); me.getConstantsRegistry().addOrUpdate(i.create()); - Assert.assertEquals("51.735296462438285", me.evaluate("Σ((1+x/i)^i, i, 1, 10)")); - Assert.assertEquals("686.0048440525586", me.evaluate("Σ((1+x/i)^i, i, 1, 100)")); + assertEquals("51.73529646243829", me.evaluate("Σ((1+x/i)^i, i, 1, 10)")); + assertEquals("686.0048440525586", me.evaluate("Σ((1+x/i)^i, i, 1, 100)")); } } diff --git a/jscl/src/test/java/org/solovyev/common/NumberFormatterTest.java b/jscl/src/test/java/org/solovyev/common/NumberFormatterTest.java new file mode 100644 index 00000000..6ac9c5d0 --- /dev/null +++ b/jscl/src/test/java/org/solovyev/common/NumberFormatterTest.java @@ -0,0 +1,36 @@ +package org.solovyev.common; + +import org.junit.Before; +import org.junit.Test; + +import static org.junit.Assert.assertEquals; + +public class NumberFormatterTest { + + private NumberFormatter numberFormatter; + + @Before + public void setUp() throws Exception { + numberFormatter = new NumberFormatter(); + } + + @Test + public void testEngineeringFormat() throws Exception { + numberFormatter.useEngineeringFormat(5); + assertEquals("0.1", numberFormatter.format(0.1d)); + assertEquals("0.01", numberFormatter.format(0.01d)); + assertEquals("0.001", numberFormatter.format(0.001d)); + assertEquals("5", numberFormatter.format(5d)); + assertEquals("5000", numberFormatter.format(5000d)); + } + + @Test + public void testSimpleFormat() throws Exception { + numberFormatter.useSimpleFormat(); + assertEquals("5000000000000000000", numberFormatter.format(5000000000000000000d)); + assertEquals("5000000000000000000", numberFormatter.format(5000000000000000001d)); + assertEquals("5999999999999994900", numberFormatter.format(5999999999999994999d)); + assertEquals("5E19", numberFormatter.format(50000000000000000000d)); + assertEquals("5E40", numberFormatter.format(50000000000000000000000000000000000000000d)); + } +} \ No newline at end of file