Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue #64 #74

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 23 additions & 1 deletion src/main/java/net/objecthunter/exp4j/operator/Operators.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,9 @@ public abstract class Operators {
private static final int INDEX_MODULO = 5;
private static final int INDEX_UNARYMINUS = 6;
private static final int INDEX_UNARYPLUS = 7;
private static final int INDEX_FACTORIAL = 8;

private static final Operator[] builtinOperators = new Operator[8];
private static final Operator[] builtinOperators = new Operator[9];

static {
builtinOperators[INDEX_ADDITION]= new Operator("+", 2, true, Operator.PRECEDENCE_ADDITION) {
Expand Down Expand Up @@ -82,6 +83,23 @@ public double apply(final double... args) {
return args[0] % args[1];
}
};
builtinOperators[INDEX_FACTORIAL]= new Operator("!", 1, true, Operator.PRECEDENCE_POWER + 1) {
@Override
public double apply(double... args) {
final int arg = (int) args[0];
if ((double) arg != args[0]) {
throw new IllegalArgumentException("Operand for factorial has to be an integer");
}
if (arg < 0) {
throw new IllegalArgumentException("The operand of the factorial can not be less than zero");
}
double result = 1;
for (int i = 1; i <= arg; i++) {
result *= i;
}
return result;
}
};
}

public static Operator getBuiltinOperator(final char symbol, final int numArguments) {
Expand All @@ -106,6 +124,10 @@ public static Operator getBuiltinOperator(final char symbol, final int numArgume
return builtinOperators[INDEX_POWER];
case '%':
return builtinOperators[INDEX_MODULO];
case '!':
if (numArguments != 1) {
return builtinOperators[INDEX_FACTORIAL];
}
default:
return null;
}
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/net/objecthunter/exp4j/tokenizer/Tokenizer.java
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,8 @@ private Operator getOperator(String symbol) {

}
op = Operators.getBuiltinOperator(symbol.charAt(0), argc);

return op;
}
return op;
}
Expand Down
54 changes: 54 additions & 0 deletions src/test/java/net/objecthunter/exp4j/ExpressionBuilderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2720,4 +2720,58 @@ public void testSignum() {
.build();
assertEquals(-1, e.evaluate(), 0d);
}

@Test
public void testOperatorFactorial() throws Exception {
Expression exp = new ExpressionBuilder("3!").build();
assertEquals(6, exp.evaluate(), 0);
assertTrue(exp.validate().isValid());
exp = new ExpressionBuilder("3!!").build();
assertEquals(720, exp.evaluate(), 0);
assertTrue(exp.validate().isValid());
exp = new ExpressionBuilder("4 + 3!").build();
assertEquals(10, exp.evaluate(), 0);
assertTrue(exp.validate().isValid());
exp = new ExpressionBuilder("3! * 2").build();
assertEquals(12, exp.evaluate(), 0);
assertTrue(exp.validate().isValid());
exp = new ExpressionBuilder("2 * 3!").build();
assertEquals(12, exp.evaluate(), 0);
assertTrue(exp.validate().isValid());
exp = new ExpressionBuilder("4 + (3!)").build();
assertEquals(10, exp.evaluate(), 0);
assertTrue(exp.validate().isValid());
exp = new ExpressionBuilder("4 + 3! + 2 * 6").build();
assertEquals(22, exp.evaluate(), 0);
assertTrue(exp.validate().isValid());
}

@Test(expected = IllegalArgumentException.class)
public void testOperatorFactorial2() throws Exception {
//Issue 64
Expression exp = new ExpressionBuilder("!3").build();
}

@Test(expected = IllegalArgumentException.class)
public void testOperatorFactorial3() throws Exception {
Expression exp = new ExpressionBuilder("!!3").build();
}

@Test(expected = IllegalArgumentException.class)
public void testOperatorFactorial4() throws Exception {
Expression exp = new ExpressionBuilder("1.5!").build();
exp.evaluate();
}

@Test(expected = IllegalArgumentException.class)
public void testOperatorFactorial5() throws Exception {
Expression exp = new ExpressionBuilder("(-1)!").build();
exp.evaluate();
}

@Test
public void testFactorialIssue75() {
Expression exp = new ExpressionBuilder("3!-2!").build();
assertEquals(4, exp.evaluate(), 1e-12);
}
}