Skip to content

Commit 0bdd212

Browse files
committed
Added the method "isFunctionalOperator()" to avoid redundancies
1 parent c96c47c commit 0bdd212

File tree

1 file changed

+31
-42
lines changed

1 file changed

+31
-42
lines changed

jcalc/src/main/java/cu/lt/joe/jcalc/algorithms/AlgorithmImplementation.java

Lines changed: 31 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -37,30 +37,9 @@ protected static boolean isOperator(String possibleOperator)
3737

3838
protected static boolean isUnaryOperator(String possibleUnaryOperator)
3939
{
40-
switch (possibleUnaryOperator)
41-
{
42-
case "sin":
43-
case "cos":
44-
case "tan":
45-
case "asin":
46-
case "arcsin":
47-
case "acos":
48-
case "arccos":
49-
case "atan":
50-
case "arctan":
51-
case "csc":
52-
case "sec":
53-
case "cot":
54-
case "ln":
55-
case "log":
56-
case "log2":
57-
case "sqrt":
58-
case "cbrt":
59-
return true;
60-
default:
61-
return possibleUnaryOperator.equals("u-") || isFactorialOperator(possibleUnaryOperator)
62-
|| isSquareRootOperator(possibleUnaryOperator);
63-
}
40+
return isFunctionalOperator(possibleUnaryOperator) || possibleUnaryOperator.equals("u-")
41+
|| isFactorialOperator(possibleUnaryOperator) || isSquareRootOperator(possibleUnaryOperator);
42+
6443
}
6544

6645
/**
@@ -104,6 +83,32 @@ protected static boolean isMathConstant(char possibleConstant)
10483
return possibleConstant == 'e' || possibleConstant == 'π';
10584
}
10685

86+
protected static boolean isFunctionalOperator(String possibleFunctionalOperator)
87+
{
88+
switch (possibleFunctionalOperator)
89+
{
90+
case "sin":
91+
case "cos":
92+
case "tan":
93+
case "asin":
94+
case "arcsin":
95+
case "acos":
96+
case "arccos":
97+
case "atan":
98+
case "arctan":
99+
case "csc":
100+
case "sec":
101+
case "cot":
102+
case "ln":
103+
case "log":
104+
case "log2":
105+
case "sqrt":
106+
case "cbrt":
107+
return true;
108+
}
109+
return false;
110+
}
111+
107112
/**
108113
* Takes two operands and an operator to perform the required operation with those operands given
109114
* a specific operator.
@@ -145,6 +150,8 @@ protected static BigDecimal makeOperation(BigDecimal secondOperand, String opera
145150
*/
146151
protected static BigDecimal makeUnaryOperation(BigDecimal operand, String operator)
147152
{
153+
if (isFunctionalOperator(operator))
154+
return useFastMathAndSolve(operand, operator, null);
148155
switch (operator)
149156
{
150157
case "u-":
@@ -153,24 +160,6 @@ protected static BigDecimal makeUnaryOperation(BigDecimal operand, String operat
153160
if (operand.compareTo(BigDecimal.ZERO) < 0)
154161
throw new NumericalDomainErrorException("Square root is not defined for negative numbers");
155162
return makeOperation(new BigDecimal("0.5"), "^", operand);
156-
case "sin":
157-
case "cos":
158-
case "tan":
159-
case "asin":
160-
case "arcsin":
161-
case "acos":
162-
case "arccos":
163-
case "atan":
164-
case "arctan":
165-
case "csc":
166-
case "sec":
167-
case "cot":
168-
case "ln":
169-
case "log":
170-
case "log2":
171-
case "sqrt":
172-
case "cbrt":
173-
return useFastMathAndSolve(operand, operator, null);
174163
case "!":
175164
if (operand.compareTo(BigDecimal.ZERO) < 0)
176165
throw new NumericalDomainErrorException("Factorial is not defined for negative numbers");

0 commit comments

Comments
 (0)