Fix tests

This commit is contained in:
serso 2016-05-02 22:48:12 +02:00
parent 4e3527c91c
commit 49ab48a44c
13 changed files with 97 additions and 121 deletions

View File

@ -1,17 +1,5 @@
package jscl; package jscl;
import org.solovyev.common.NumberFormatter;
import org.solovyev.common.math.MathRegistry;
import org.solovyev.common.msg.MessageRegistry;
import org.solovyev.common.msg.Messages;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.List;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import jscl.math.Expression; import jscl.math.Expression;
import jscl.math.Generic; import jscl.math.Generic;
import jscl.math.NotIntegerException; import jscl.math.NotIntegerException;
@ -27,6 +15,10 @@ import jscl.math.operator.Percent;
import jscl.math.operator.Rand; import jscl.math.operator.Rand;
import jscl.math.operator.matrix.OperatorsRegistry; import jscl.math.operator.matrix.OperatorsRegistry;
import jscl.text.ParseException; import jscl.text.ParseException;
import org.solovyev.common.NumberFormatter;
import org.solovyev.common.math.MathRegistry;
import org.solovyev.common.msg.MessageRegistry;
import org.solovyev.common.msg.Messages;
import static midpcalc.Real.NumberFormat.FSE_ENG; import static midpcalc.Real.NumberFormat.FSE_ENG;
import static midpcalc.Real.NumberFormat.FSE_NONE; import static midpcalc.Real.NumberFormat.FSE_NONE;
@ -177,7 +169,7 @@ public class JsclMathEngine implements MathEngine {
private NumberFormatter prepareNumberFormatter(@Nonnull NumeralBase nb) { private NumberFormatter prepareNumberFormatter(@Nonnull NumeralBase nb) {
final NumberFormatter nf = numberFormatter.get(); final NumberFormatter nf = numberFormatter.get();
nf.setGroupingSeparator(hasGroupingSeparator() ? getGroupingSeparatorChar(nb) : NumberFormatter.NO_GROUPING); nf.setGroupingSeparator(hasGroupingSeparator() ? getGroupingSeparator(nb) : NumberFormatter.NO_GROUPING);
nf.setPrecision(precision); nf.setPrecision(precision);
switch (notation) { switch (notation) {
case FSE_ENG: case FSE_ENG:
@ -263,7 +255,7 @@ public class JsclMathEngine implements MathEngine {
@Nonnull @Nonnull
@Override @Override
public String format(@Nonnull String value, @Nonnull NumeralBase nb) { public String format(@Nonnull String value, @Nonnull NumeralBase nb) {
if (!useGroupingSeparator) { if (!hasGroupingSeparator()) {
return value; return value;
} }
final int dot = value.indexOf('.'); final int dot = value.indexOf('.');
@ -281,8 +273,8 @@ public class JsclMathEngine implements MathEngine {
@Nonnull @Nonnull
public String insertSeparators(@Nonnull String value, @Nonnull NumeralBase nb) { public String insertSeparators(@Nonnull String value, @Nonnull NumeralBase nb) {
final String separator = getGroupingSeparator(nb); final char separator = getGroupingSeparator(nb);
final StringBuilder result = new StringBuilder(value.length() + nb.getGroupingSize() * separator.length()); final StringBuilder result = new StringBuilder(value.length() + nb.getGroupingSize());
for (int i = value.length() - 1; i >= 0; i--) { for (int i = value.length() - 1; i >= 0; i--) {
result.append(value.charAt(i)); result.append(value.charAt(i));
if (i != 0 && (value.length() - i) % nb.getGroupingSize() == 0) { if (i != 0 && (value.length() - i) % nb.getGroupingSize() == 0) {
@ -296,19 +288,10 @@ public class JsclMathEngine implements MathEngine {
return groupingSeparator != NumberFormatter.NO_GROUPING; return groupingSeparator != NumberFormatter.NO_GROUPING;
} }
@Nonnull private char getGroupingSeparator(@Nonnull NumeralBase nb) {
private String getGroupingSeparator(@Nonnull NumeralBase nb) {
return nb == NumeralBase.dec ? String.valueOf(groupingSeparator) : " ";
}
private char getGroupingSeparatorChar(@Nonnull NumeralBase nb) {
return nb == NumeralBase.dec ? groupingSeparator : ' '; return nb == NumeralBase.dec ? groupingSeparator : ' ';
} }
public void setRoundResult(boolean roundResult) {
this.roundResult = roundResult;
}
public void setPrecision(int precision) { public void setPrecision(int precision) {
this.precision = precision; this.precision = precision;
} }

View File

@ -1,17 +1,13 @@
package org.solovyev.common; package org.solovyev.common;
import java.math.BigDecimal;
import java.math.BigInteger;
import javax.annotation.Nonnull;
import midpcalc.Real; import midpcalc.Real;
import javax.annotation.Nonnull;
import java.math.BigDecimal;
import java.math.BigInteger;
import static java.lang.Math.pow; import static java.lang.Math.pow;
import static midpcalc.Real.NumberFormat.FSE_ENG; import static midpcalc.Real.NumberFormat.*;
import static midpcalc.Real.NumberFormat.FSE_FIX;
import static midpcalc.Real.NumberFormat.FSE_NONE;
import static midpcalc.Real.NumberFormat.FSE_SCI;
public class NumberFormatter { public class NumberFormatter {

View File

@ -90,7 +90,7 @@ public class JsclMathEngineTest {
@Test @Test
public void testPiComputation() throws Exception { public void testPiComputation() throws Exception {
assertEquals("-1+0.0000000000000001*i", me.evaluate("exp(√(-1)*Π)")); assertEquals("-1+0*i", me.evaluate("exp(√(-1)*Π)"));
} }
@Test @Test

View File

@ -77,7 +77,7 @@ public class NumeralBaseTest {
assertEquals("11111110", me.evaluate("111001+11000101")); assertEquals("11111110", me.evaluate("111001+11000101"));
assertEquals("1101100100101111", me.evaluate("11011001001011110/10")); assertEquals("1101100100101111", me.evaluate("11011001001011110/10"));
assertEquals("1001000011001010", me.evaluate("11011001001011110/11")); assertEquals("1001000011001010", me.evaluate("11011001001011110/11"));
assertEquals("0.1010101010101011", me.evaluate("10/11")); assertEquals("0.101010101010101", me.evaluate("10/11"));
me.setNumeralBase(NumeralBase.hex); me.setNumeralBase(NumeralBase.hex);
assertEquals("637B", me.evaluate("56CE+CAD")); assertEquals("637B", me.evaluate("56CE+CAD"));

View File

@ -1,19 +1,5 @@
package jscl.math; package jscl.math;
import org.junit.Test;
import org.solovyev.common.NumberFormatter;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.Set;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import jscl.AngleUnit; import jscl.AngleUnit;
import jscl.JsclMathEngine; import jscl.JsclMathEngine;
import jscl.MathEngine; import jscl.MathEngine;
@ -23,10 +9,20 @@ import jscl.math.function.ExtendedConstant;
import jscl.math.function.IConstant; import jscl.math.function.IConstant;
import jscl.text.ParseException; import jscl.text.ParseException;
import midpcalc.Real; import midpcalc.Real;
import org.junit.Test;
import org.solovyev.common.NumberFormatter;
import static org.junit.Assert.assertEquals; import javax.annotation.Nonnull;
import static org.junit.Assert.assertTrue; import javax.annotation.Nullable;
import static org.junit.Assert.fail; import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.Set;
import static org.junit.Assert.*;
public class ExpressionTest { public class ExpressionTest {
@ -145,7 +141,7 @@ public class ExpressionTest {
@Test @Test
public void testExpressions() throws Exception { public void testExpressions() throws Exception {
assertEquals("3", Expression.valueOf("3").numeric().toString()); assertEquals("3", Expression.valueOf("3").numeric().toString());
assertEquals("0.6931471805599453", Expression.valueOf("ln(2)").numeric().toString()); assertEquals("0.693147180559945", Expression.valueOf("ln(2)").numeric().toString());
assertEquals("1", Expression.valueOf("lg(10)").numeric().toString()); assertEquals("1", Expression.valueOf("lg(10)").numeric().toString());
assertEquals("0", Expression.valueOf("eq(0, 1)").numeric().toString()); assertEquals("0", Expression.valueOf("eq(0, 1)").numeric().toString());
assertEquals("1", Expression.valueOf("eq(1, 1)").numeric().toString()); assertEquals("1", Expression.valueOf("eq(1, 1)").numeric().toString());
@ -168,7 +164,7 @@ public class ExpressionTest {
final AngleUnit angleUnits = me.getAngleUnits(); final AngleUnit angleUnits = me.getAngleUnits();
try { try {
me.setAngleUnits(AngleUnit.rad); me.setAngleUnits(AngleUnit.rad);
assertEquals("-0.9055783620066238", Expression.valueOf("sin(4!)").numeric().toString()); assertEquals("-0.905578362006624", Expression.valueOf("sin(4!)").numeric().toString());
} finally { } finally {
me.setAngleUnits(angleUnits); me.setAngleUnits(angleUnits);
} }
@ -180,7 +176,7 @@ public class ExpressionTest {
} catch (NotIntegerException e) { } catch (NotIntegerException e) {
} }
assertEquals("0.5235987755982988", Expression.valueOf("3.141592653589793/3!").numeric().toString()); assertEquals("0.523598775598299", Expression.valueOf("3.141592653589793/3!").numeric().toString());
try { try {
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(); fail();
@ -212,14 +208,14 @@ public class ExpressionTest {
final AngleUnit defaultAngleUnits = me.getAngleUnits(); final AngleUnit defaultAngleUnits = me.getAngleUnits();
try { try {
me.setAngleUnits(AngleUnit.rad); me.setAngleUnits(AngleUnit.rad);
assertEquals("0.0174532925199433", Expression.valueOf("").numeric().toString()); assertEquals("0.017453292519943", Expression.valueOf("").numeric().toString());
assertEquals("0.0349065850398866", Expression.valueOf("").numeric().toString()); assertEquals("0.034906585039887", Expression.valueOf("").numeric().toString());
assertEquals("0.0523598775598299", Expression.valueOf("").numeric().toString()); assertEquals("0.05235987755983", Expression.valueOf("").numeric().toString());
assertEquals("0.2617993877991495", Expression.valueOf("3°*5").numeric().toString()); assertEquals("0.261799387799149", Expression.valueOf("3°*5").numeric().toString());
assertEquals("0.0027415567780804", Expression.valueOf("3°^2").numeric().toString()); assertEquals("0.00274155677808", Expression.valueOf("3°^2").numeric().toString());
assertEquals("0.0109662271123215", Expression.valueOf("3!°^2").numeric().toString()); assertEquals("0.010966227112322", Expression.valueOf("3!°^2").numeric().toString());
assertEquals("0.0009138522593601", Expression.valueOf("3°°").numeric().toString()); assertEquals("0.00091385225936", Expression.valueOf("3°°").numeric().toString());
assertEquals("0.0872664625997165", Expression.valueOf("").numeric().toString()); assertEquals("0.087266462599716", Expression.valueOf("").numeric().toString());
assertEquals("2.05235987755983", Expression.valueOf("2+3°").numeric().toString()); assertEquals("2.05235987755983", Expression.valueOf("2+3°").numeric().toString());
} finally { } finally {
me.setAngleUnits(defaultAngleUnits); me.setAngleUnits(defaultAngleUnits);
@ -377,7 +373,7 @@ public class ExpressionTest {
try { try {
me.setAngleUnits(AngleUnit.rad); me.setAngleUnits(AngleUnit.rad);
assertEquals("0.6931471805599453+Π*i", Expression.valueOf("ln(-2)").numeric().toString()); assertEquals("0.693147180559945+Π*i", Expression.valueOf("ln(-2)").numeric().toString());
} finally { } finally {
me.setAngleUnits(AngleUnit.deg); me.setAngleUnits(AngleUnit.deg);
} }
@ -387,7 +383,7 @@ public class ExpressionTest {
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(n°)", Expression.valueOf("sin(n°)").expand().toString());
assertEquals("sin(30°)", Expression.valueOf("sin(30°)").expand().toString()); assertEquals("sin(30°)", Expression.valueOf("sin(30°)").expand().toString());
assertEquals("0.4999999999999999", Expression.valueOf("sin(30°)").expand().numeric().toString()); assertEquals("0.5", Expression.valueOf("sin(30°)").expand().numeric().toString());
assertEquals("sin(2!)", Expression.valueOf("sin(2!)").expand().toString()); assertEquals("sin(2!)", Expression.valueOf("sin(2!)").expand().toString());
assertEquals("12", Expression.valueOf("3*(3+1)").expand().toString()); assertEquals("12", Expression.valueOf("3*(3+1)").expand().toString());
@ -408,7 +404,7 @@ public class ExpressionTest {
} catch (ParseException e) { } catch (ParseException e) {
} }
assertEquals("0.4999999999999999", Expression.valueOf("sin(30°)").numeric().toString()); assertEquals("0.5", Expression.valueOf("sin(30°)").numeric().toString());
assertEquals("π", Expression.valueOf("√(π)^2").simplify().toString()); assertEquals("π", Expression.valueOf("√(π)^2").simplify().toString());
assertEquals("π", Expression.valueOf("√(π^2)").simplify().toString()); assertEquals("π", Expression.valueOf("√(π^2)").simplify().toString());
assertEquals("π^2", Expression.valueOf("√(π^2*π^2)").simplify().toString()); assertEquals("π^2", Expression.valueOf("√(π^2*π^2)").simplify().toString());
@ -420,7 +416,7 @@ public class ExpressionTest {
// in deg mode π=180 and factorial of 180 is calculating // in deg mode π=180 and factorial of 180 is calculating
assertEquals("0", Expression.valueOf("Π/Π!").numeric().toString()); assertEquals("0", Expression.valueOf("Π/Π!").numeric().toString());
assertEquals("0.0000000000000001*i", Expression.valueOf("exp((Π*i))+1").numeric().toString()); assertEquals("0*i", Expression.valueOf("exp((Π*i))+1").numeric().toString());
assertEquals("20*x^3", Expression.valueOf("∂(5*x^4, x)").expand().simplify().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("25*x", Expression.valueOf("5*x*5").expand().simplify().toString());
assertEquals("20*x", Expression.valueOf("5*x*4").expand().simplify().toString()); assertEquals("20*x", Expression.valueOf("5*x*4").expand().simplify().toString());
@ -442,10 +438,10 @@ public class ExpressionTest {
try { try {
me.setNumeralBase(NumeralBase.hex); me.setNumeralBase(NumeralBase.hex);
assertEquals("0.EEEEEEEEEEEEEC88", me.evaluate("0x:E/0x:F")); assertEquals("0.EEEEEEEEEEEEEC9", me.evaluate("0x:E/0x:F"));
assertEquals("E/F", me.simplify("0x:E/0x:F")); assertEquals("E/F", me.simplify("0x:E/0x:F"));
assertEquals("0.EEEEEEEEEEEEEC88", me.evaluate("E/F")); assertEquals("0.EEEEEEEEEEEEEC9", me.evaluate("E/F"));
assertEquals("E/F", me.simplify("E/F")); assertEquals("E/F", me.simplify("E/F"));
} finally { } finally {
@ -472,9 +468,9 @@ public class ExpressionTest {
assertEquals("-1.471127674303735", me.evaluate("atan(-10)")); assertEquals("-1.471127674303735", me.evaluate("atan(-10)"));
assertEquals("-1.10714871779409", me.evaluate("atan(-2)")); assertEquals("-1.10714871779409", me.evaluate("atan(-2)"));
assertEquals("-0.7853981633974483", me.evaluate("atan(-1)")); assertEquals("-0.785398163397448", me.evaluate("atan(-1)"));
assertEquals("0", me.evaluate("atan(0)")); assertEquals("0", me.evaluate("atan(0)"));
assertEquals("0.7853981633974483", me.evaluate("atan(1)")); assertEquals("0.785398163397448", me.evaluate("atan(1)"));
assertEquals("1.10714871779409", me.evaluate("atan(2)")); assertEquals("1.10714871779409", me.evaluate("atan(2)"));
assertEquals("1.471127674303735", me.evaluate("atan(10)")); assertEquals("1.471127674303735", me.evaluate("atan(10)"));
@ -487,9 +483,9 @@ public class ExpressionTest {
assertEquals("1.570796326794897", me.evaluate("acot(0)")); assertEquals("1.570796326794897", me.evaluate("acot(0)"));
assertEquals("2.677945044588987", me.evaluate("acot(-2)")); assertEquals("2.677945044588987", me.evaluate("acot(-2)"));
assertEquals("2.356194490192345", me.evaluate("acot(-1)")); assertEquals("2.356194490192345", me.evaluate("acot(-1)"));
assertEquals("0.7853981633974483", me.evaluate("acot(1)")); assertEquals("0.785398163397448", me.evaluate("acot(1)"));
assertEquals("0.4636476090008062", me.evaluate("acot(2)")); assertEquals("0.463647609000806", me.evaluate("acot(2)"));
assertEquals("0.0996686524911619", me.evaluate("acot(10)")); assertEquals("0.099668652491162", me.evaluate("acot(10)"));
assertEquals("Π", me.evaluate("π")); assertEquals("Π", me.evaluate("π"));
assertEquals("Π", me.evaluate("3.14159265358979323846")); assertEquals("Π", me.evaluate("3.14159265358979323846"));
@ -638,12 +634,12 @@ public class ExpressionTest {
try { try {
mathEngine.setAngleUnits(AngleUnit.rad); mathEngine.setAngleUnits(AngleUnit.rad);
testSinEqualsToSinh(mathEngine, 0d); testSinEqualsToSinh(mathEngine, 0d);
testSinEqualsToSinh(mathEngine, 1d, "0.8414709848078965"); testSinEqualsToSinh(mathEngine, 1d, "0.841470984807897");
testSinEqualsToSinh(mathEngine, 3d, "0.1411200080598672"); testSinEqualsToSinh(mathEngine, 3d, "0.141120008059867");
testSinEqualsToSinh(mathEngine, 6d); testSinEqualsToSinh(mathEngine, 6d);
testSinEqualsToSinh(mathEngine, -1d, "-0.8414709848078965"); testSinEqualsToSinh(mathEngine, -1d, "-0.841470984807897");
testSinEqualsToSinh(mathEngine, -3.3d, "0.1577456941432482"); testSinEqualsToSinh(mathEngine, -3.3d, "0.157745694143248");
testSinEqualsToSinh(mathEngine, -232.2d, "0.2742948637368958"); testSinEqualsToSinh(mathEngine, -232.2d, "0.274294863736896");
} finally { } finally {
mathEngine.setAngleUnits(defaultAngleUnits); mathEngine.setAngleUnits(defaultAngleUnits);
} }
@ -651,12 +647,12 @@ public class ExpressionTest {
try { try {
mathEngine.setAngleUnits(AngleUnit.deg); mathEngine.setAngleUnits(AngleUnit.deg);
testSinEqualsToSinh(mathEngine, 0d); testSinEqualsToSinh(mathEngine, 0d);
testSinEqualsToSinh(mathEngine, 1d, "0.0174524064372835"); testSinEqualsToSinh(mathEngine, 1d, "0.017452406437284");
testSinEqualsToSinh(mathEngine, 3d, "0.0523359562429438"); testSinEqualsToSinh(mathEngine, 3d, "0.052335956242944");
testSinEqualsToSinh(mathEngine, 6d, "0.1045284632676535"); testSinEqualsToSinh(mathEngine, 6d, "0.104528463267653");
testSinEqualsToSinh(mathEngine, -1d, "-0.0174524064372835"); testSinEqualsToSinh(mathEngine, -1d, "-0.017452406437284");
testSinEqualsToSinh(mathEngine, -3.3d, "-0.0575640269595673"); testSinEqualsToSinh(mathEngine, -3.3d, "-0.057564026959567");
testSinEqualsToSinh(mathEngine, -232.2d, "0.7901550123756904"); testSinEqualsToSinh(mathEngine, -232.2d, "0.79015501237569");
assertEquals("Π/2", mathEngine.simplify("Π/2")); assertEquals("Π/2", mathEngine.simplify("Π/2"));
} finally { } finally {
mathEngine.setAngleUnits(defaultAngleUnits); mathEngine.setAngleUnits(defaultAngleUnits);
@ -732,7 +728,7 @@ public class ExpressionTest {
final AngleUnit defaultAngleUnits = JsclMathEngine.getInstance().getAngleUnits(); final AngleUnit defaultAngleUnits = JsclMathEngine.getInstance().getAngleUnits();
try { try {
JsclMathEngine.getInstance().setAngleUnits(AngleUnit.rad); JsclMathEngine.getInstance().setAngleUnits(AngleUnit.rad);
assertEquals("-0.9092974268256817", Expression.valueOf("∂(cos(t),t,2)").numeric().toString()); assertEquals("-0.909297426825682", Expression.valueOf("∂(cos(t),t,2)").numeric().toString());
assertEquals("∂(cos(t), t, 2, 1)", Expression.valueOf("∂(cos(t),t,2)").simplify().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("-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("-4.469483380397012", Expression.valueOf("2*∂(t*cos(t),t,2)").numeric().toString());
@ -760,7 +756,7 @@ public class ExpressionTest {
assertEquals(Expression.valueOf("3").numeric().toString(), Expression.valueOf("Σ(n°,n,1,2)").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("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("-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()); assertEquals("-0.052351903139784", Expression.valueOf("Σ(∂(cos(t),t,n),n,1,2)").expand().numeric().toString());
} }
@Test @Test
@ -819,10 +815,10 @@ public class ExpressionTest {
assertEquals("1.222E-6", Expression.valueOf("1222/(10^9)").numeric().toString()); assertEquals("1.222E-6", Expression.valueOf("1222/(10^9)").numeric().toString());
me.setNotation(Real.NumberFormat.FSE_NONE); me.setNotation(Real.NumberFormat.FSE_NONE);
assertEquals("0.3333333333333333", Expression.valueOf("1/3").numeric().toString()); assertEquals("0.333333333333333", Expression.valueOf("1/3").numeric().toString());
me.setNotation(Real.NumberFormat.FSE_SCI); me.setNotation(Real.NumberFormat.FSE_SCI);
assertEquals("0.3333333333333333", Expression.valueOf("1/3").numeric().toString()); assertEquals("0.333333333333333", Expression.valueOf("1/3").numeric().toString());
me.setPrecision(10); me.setPrecision(10);
assertEquals("0.3333333333", Expression.valueOf("1/3").numeric().toString()); assertEquals("0.3333333333", Expression.valueOf("1/3").numeric().toString());

View File

@ -37,7 +37,7 @@ public class CustomFunctionTest {
assertEquals("", Expression.valueOf("log(1, 10)").numeric().toString()); assertEquals("", Expression.valueOf("log(1, 10)").numeric().toString());
assertEquals("3.321928094887363", Expression.valueOf("log(2, 10)").numeric().toString()); assertEquals("3.321928094887363", Expression.valueOf("log(2, 10)").numeric().toString());
assertEquals("1.430676558073393", Expression.valueOf("log(5, 10)").numeric().toString()); assertEquals("1.430676558073393", Expression.valueOf("log(5, 10)").numeric().toString());
assertEquals("0.9602525677891275", Expression.valueOf("log(11, 10)").numeric().toString()); assertEquals("0.960252567789128", Expression.valueOf("log(11, 10)").numeric().toString());
assertEquals("1/b*1/ln(a)", Expression.valueOf("∂(log(a, b), b)").expand().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()); assertEquals("-1/a*(1/ln(a))^2*ln(b)", Expression.valueOf("∂(log(a, b), a)").expand().toString());
@ -133,10 +133,10 @@ public class CustomFunctionTest {
final CustomFunction.Builder jBuilder4 = new CustomFunction.Builder("testFunction5", asList("a", "b"), "testFunction2(a, b/2, 2, 1) - testFunction(a, b!, 4!, 1)"); final CustomFunction.Builder jBuilder4 = new CustomFunction.Builder("testFunction5", asList("a", "b"), "testFunction2(a, b/2, 2, 1) - testFunction(a, b!, 4!, 1)");
mathEngine.getFunctionsRegistry().addOrUpdate(jBuilder4.create()); mathEngine.getFunctionsRegistry().addOrUpdate(jBuilder4.create());
assertEquals("0.4996954135095478", Expression.valueOf("testFunction5(2, 3)").numeric().toString()); assertEquals("0.499695413509548", Expression.valueOf("testFunction5(2, 3)").numeric().toString());
assertEquals("0.4996954135095478", Expression.valueOf("testFunction5(2, 3)").numeric().toString()); assertEquals("0.499695413509548", Expression.valueOf("testFunction5(2, 3)").numeric().toString());
assertEquals("0.4996954135095478", Expression.valueOf("testFunction5(2*1, 3)").numeric().toString()); assertEquals("0.499695413509548", Expression.valueOf("testFunction5(2*1, 3)").numeric().toString());
assertEquals("-0.0000000000000001", Expression.valueOf("testFunction5(2*1, 2^2-1+e^0)").numeric().toString()); assertEquals("0", Expression.valueOf("testFunction5(2*1, 2^2-1+e^0)").numeric().toString());
try { try {
Expression.valueOf("testFunction5(2, 3.5)").numeric(); Expression.valueOf("testFunction5(2, 3.5)").numeric();

View File

@ -10,10 +10,10 @@ public class RadTest {
public void testRad() throws Exception { public void testRad() throws Exception {
final JsclMathEngine mathEngine = new JsclMathEngine(); final JsclMathEngine mathEngine = new JsclMathEngine();
assertEquals("0.0349065850398866", mathEngine.evaluate("rad(2)")); assertEquals("0.034906585039887", mathEngine.evaluate("rad(2)"));
assertEquals("0.0349065850398866", mathEngine.evaluate("rad(1+1)")); assertEquals("0.034906585039887", mathEngine.evaluate("rad(1+1)"));
assertEquals("-0.0349065850398866", mathEngine.evaluate("rad(-2)")); assertEquals("-0.034906585039887", mathEngine.evaluate("rad(-2)"));
assertEquals("-0.0349065850398866", mathEngine.evaluate("rad(-1-1)")); assertEquals("-0.034906585039887", mathEngine.evaluate("rad(-1-1)"));
assertEquals("π", mathEngine.evaluate("rad(180)")); assertEquals("π", mathEngine.evaluate("rad(180)"));
assertEquals(String.valueOf(-Math.PI), mathEngine.evaluate("rad(-180)")); assertEquals(String.valueOf(-Math.PI), mathEngine.evaluate("rad(-180)"));

View File

@ -13,13 +13,13 @@ public class SqrtTest {
final JsclMathEngine me = JsclMathEngine.getInstance(); final JsclMathEngine me = JsclMathEngine.getInstance();
final AngleUnit defaultAngleUnits = me.getAngleUnits(); final AngleUnit defaultAngleUnits = me.getAngleUnits();
assertEquals("0.9999060498015505+0.0137073546047075*i", me.evaluate("√(√(-1))")); assertEquals("0.999906049801551+0.013707354604707*i", me.evaluate("√(√(-1))"));
assertEquals("0.9984971498638638+0.0548036651487895*i", me.evaluate("√(√(-1))^4")); assertEquals("0.998497149863864+0.05480366514879*i", me.evaluate("√(√(-1))^4"));
try { try {
me.setAngleUnits(AngleUnit.rad); me.setAngleUnits(AngleUnit.rad);
assertEquals("0.7071067811865476+0.7071067811865475*i", me.evaluate("√(√(-1))")); assertEquals("0.707106781186548+0.707106781186548*i", me.evaluate("√(√(-1))"));
assertEquals("-1+0.0000000000000003*i", me.evaluate("√(√(-1))^4")); assertEquals("-1+0*i", me.evaluate("√(√(-1))^4"));
} finally { } finally {
me.setAngleUnits(defaultAngleUnits); me.setAngleUnits(defaultAngleUnits);
} }

View File

@ -20,9 +20,9 @@ public class CosTest {
me.getConstantsRegistry().addOrUpdate(t.create()); me.getConstantsRegistry().addOrUpdate(t.create());
Assert.assertEquals("-sin(t)", me.simplify("∂(cos(t),t,t,1)")); 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("∂(cos(t), t, t, 1°)", me.simplify("∂(cos(t),t,t,1°)"));
Assert.assertEquals("-0.1736481776669303", me.evaluate("∂(cos(t),t,t,1)")); Assert.assertEquals("-0.17364817766693", me.evaluate("∂(cos(t),t,t,1)"));
Assert.assertEquals("∂(cos(t), t, t, 1°)", me.evaluate("∂(cos(t),t,t,1°)")); Assert.assertEquals("∂(cos(t), t, t, 1°)", me.evaluate("∂(cos(t),t,t,1°)"));
Assert.assertEquals("-0.1736481776669303", me.evaluate("∂(cos(t),t,t,2-1)")); Assert.assertEquals("-0.17364817766693", me.evaluate("∂(cos(t),t,t,2-1)"));
Assert.assertEquals("-0.1736481776669303", me.evaluate("∂(cos(t),t,t,2^5-31)")); Assert.assertEquals("-0.17364817766693", me.evaluate("∂(cos(t),t,t,2^5-31)"));
} }
} }

View File

@ -26,9 +26,9 @@ public class SinTest {
try { try {
me.setAngleUnits(AngleUnit.rad); me.setAngleUnits(AngleUnit.rad);
Assert.assertEquals("0.5403023058681398", me.evaluate("cos(1)")); Assert.assertEquals("0.54030230586814", me.evaluate("cos(1)"));
Assert.assertEquals("0.3623577544766736", me.evaluate("cos(1.2)")); Assert.assertEquals("0.362357754476674", me.evaluate("cos(1.2)"));
Assert.assertEquals("0.1779445513914661", me.evaluate("∫ab(sin(x), x, 1, 1.2)")); Assert.assertEquals("0.177944551391466", me.evaluate("∫ab(sin(x), x, 1, 1.2)"));
} finally { } finally {
me.setAngleUnits(AngleUnit.deg); me.setAngleUnits(AngleUnit.deg);
} }
@ -38,7 +38,7 @@ public class SinTest {
try { try {
me.setAngleUnits(AngleUnit.rad); me.setAngleUnits(AngleUnit.rad);
Assert.assertEquals("0.1339745962155613", me.evaluate("∫ab(sin(x), x, 0, 30°)")); Assert.assertEquals("0.133974596215561", me.evaluate("∫ab(sin(x), x, 0, 30°)"));
} finally { } finally {
me.setAngleUnits(AngleUnit.deg); me.setAngleUnits(AngleUnit.deg);
} }

View File

@ -29,9 +29,9 @@ public class TanTest {
me.setAngleUnits(AngleUnit.rad); 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("-(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.0153088314659858", me.evaluate("ln(cos(10*π/180))")); assertEquals("-0.015308831465986", me.evaluate("ln(cos(10*π/180))"));
assertEquals("-0.1438410362258904", me.evaluate("ln(cos(30*π/180))")); assertEquals("-0.14384103622589", me.evaluate("ln(cos(30*π/180))"));
assertEquals("0.1285322047599047", me.evaluate("∫ab(tan(x), x, 10*π/180, 30*π/180)")); assertEquals("0.128532204759905", me.evaluate("∫ab(tan(x), x, 10*π/180, 30*π/180)"));
} finally { } finally {
me.setAngleUnits(AngleUnit.deg); me.setAngleUnits(AngleUnit.deg);
} }

View File

@ -11,8 +11,8 @@ public class ComplexTest {
@Test @Test
public void testSmallImag() throws Exception { public void testSmallImag() throws Exception {
assertEquals("1+0.0000000000000001*i", Complex.valueOf(1, 0.0000000000000001).toString()); assertEquals("1+0.000000000000001*i", Complex.valueOf(1, 0.000000000000001).toString());
assertEquals("1-0.0000000000000001*i", Complex.valueOf(1, -0.0000000000000001).toString()); assertEquals("1-0.000000000000001*i", Complex.valueOf(1, -0.000000000000001).toString());
} }
@Test @Test
@ -23,7 +23,7 @@ public class ComplexTest {
assertEquals("1.175201193643801*i", Expression.valueOf("sin(i)").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.2328747034*i", Expression.valueOf("sin(10*i)").numeric().toString());
assertEquals("11013.23292010332", Expression.valueOf("cos(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("0.46211715726001*i", Expression.valueOf("tan(i/2)").numeric().toString());
assertEquals("-2.163953413738653*i", Expression.valueOf("cot(i/2)").numeric().toString()); assertEquals("-2.163953413738653*i", Expression.valueOf("cot(i/2)").numeric().toString());
} finally { } finally {
JsclMathEngine.getInstance().setAngleUnits(JsclMathEngine.DEFAULT_ANGLE_UNITS); JsclMathEngine.getInstance().setAngleUnits(JsclMathEngine.DEFAULT_ANGLE_UNITS);

View File

@ -36,11 +36,11 @@ public class NumberFormatterTest {
numberFormatter.setPrecision(NO_ROUNDING); numberFormatter.setPrecision(NO_ROUNDING);
assertEquals("1", numberFormatter.format(1d)); assertEquals("1", numberFormatter.format(1d));
assertEquals("0.3333333333333333", numberFormatter.format(1d / 3)); assertEquals("0.333333333333333", numberFormatter.format(1d / 3));
assertEquals("3.333333333333333E-19", numberFormatter.format(pow(10, -18) / 3)); assertEquals("3.333333333333333E-19", numberFormatter.format(pow(10, -18) / 3));
assertEquals("1.23456789E18", numberFormatter.format(123456789 * pow(10, 10))); assertEquals("1.23456789E18", numberFormatter.format(123456789 * pow(10, 10)));
assertEquals("1E-16", numberFormatter.format(pow(10, -16))); assertEquals("1E-16", numberFormatter.format(pow(10, -16)));
assertEquals("5.9999999999999949E18", numberFormatter.format(5999999999999994999d)); assertEquals("5.999999999999995E18", numberFormatter.format(5999999999999994999d));
testScientificFormat(); testScientificFormat();
} }
@ -68,10 +68,10 @@ public class NumberFormatterTest {
assertEquals("1", numberFormatter.format(1d)); assertEquals("1", numberFormatter.format(1d));
assertEquals("0.000001", numberFormatter.format(pow(10, -6))); assertEquals("0.000001", numberFormatter.format(pow(10, -6)));
assertEquals("0.3333333333333333", numberFormatter.format(1d / 3)); assertEquals("0.333333333333333", numberFormatter.format(1d / 3));
assertEquals("3.333333333333333E-19", numberFormatter.format(pow(10, -18) / 3)); assertEquals("3.333333333333333E-19", numberFormatter.format(pow(10, -18) / 3));
assertEquals("1234567890000000000", numberFormatter.format(123456789 * pow(10, 10))); assertEquals("1234567890000000000", numberFormatter.format(123456789 * pow(10, 10)));
assertEquals("0.0000000000000001", numberFormatter.format(pow(10, -16))); assertEquals("1E-16", numberFormatter.format(pow(10, -16)));
assertEquals("1E-17", numberFormatter.format(pow(10, -17))); assertEquals("1E-17", numberFormatter.format(pow(10, -17)));
assertEquals("1E-18", numberFormatter.format(pow(10, -18))); assertEquals("1E-18", numberFormatter.format(pow(10, -18)));
assertEquals("1.5E-18", numberFormatter.format(1.5 * pow(10, -18))); assertEquals("1.5E-18", numberFormatter.format(1.5 * pow(10, -18)));
@ -102,6 +102,7 @@ public class NumberFormatterTest {
// testing simple format with and without rounding // testing simple format with and without rounding
private void testSimpleFormat() { private void testSimpleFormat() {
assertEquals("0.00001", numberFormatter.format(pow(10, -5))); assertEquals("0.00001", numberFormatter.format(pow(10, -5)));
assertEquals("0.01", numberFormatter.format(3.11 - 3.1));
assertEquals("100", numberFormatter.format(pow(10, 2))); assertEquals("100", numberFormatter.format(pow(10, 2)));
assertEquals("1", numberFormatter.format(BigInteger.ONE)); assertEquals("1", numberFormatter.format(BigInteger.ONE));