Skip to content

Commit

Permalink
Fix bug with 'return -100 + 100'
Browse files Browse the repository at this point in the history
  • Loading branch information
Nicolas CHABALIER committed Jan 31, 2024
1 parent 172c994 commit 00ddd45
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/main/java/org/math/R/AbstractR2jsSession.java
Original file line number Diff line number Diff line change
Expand Up @@ -2307,7 +2307,7 @@ private static String replaceOperators(String expr) {

// We consider differently the '-' operator in '2-3' to the '-' negative: '-3'.
// So we replace -3 by î3 first, but 2-3 stays 2-3
expr = expr.replaceAll("([\\[\\{\\(\\-\\\\=*\\/^;%+:,><&|ôâêŝĝ\\n]) *-", "$1 î");
expr = expr.replaceAll("([\\[\\{\\(\\-\\\\=*\\/^;%+:,><&|ôâêŝĝ\\n]|return) *-", "$1 î");

String stoppingCharacters = "-=*/^;%+:,><&|ôâêŝĝ\n"; // all operators but 'î'
expr = expr.replaceAll("[)]/", ") /");
Expand Down
12 changes: 12 additions & 0 deletions src/test/java/org/math/R/R2jsSessionTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -1483,6 +1483,18 @@ public void testDecimalPoints() throws RException {
assertEquals((Double) engine.eval("1e-31*1e-32"), 1e-63, 1e-75); //"normal" floating error of 1e-15 on multiplication
}

@Test
public void testMinus() throws RException {
engine.eval("test <- function() { -100+100+5 }");
assert (Double) engine.eval("test()") == 5.0;
assert (Double) engine.eval("-3 -4") == -7;
assert (Double) engine.eval(" -3 -4") == -7;
assert (Double) engine.eval("( -3 -4)") == -7;
assert (Double) engine.eval(" ( -3 -4)") == -7;
assert (Double) engine.eval(" { -3 -4}") == -7;
assert ((double[]) engine.eval(" [ -3 -4]"))[0] == -7;
}

// @Test
// public void testEvaluationTime() throws RException, ScriptException, IOException {
// String MATH_JS_FILE = "/org/math/R/math.js";
Expand Down

0 comments on commit 00ddd45

Please sign in to comment.