Skip to content

Commit 2e5a6c9

Browse files
committed
Updated "useFastMathAndSolve()" to take the values directly as double
1 parent 1b2f5a2 commit 2e5a6c9

File tree

1 file changed

+21
-20
lines changed

1 file changed

+21
-20
lines changed

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

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ protected static BigDecimal makeOperation(BigDecimal secondOperand, String opera
142142
case "/":
143143
return firstOperand.divide(secondOperand, MathContext.DECIMAL64);
144144
case "^":
145-
return useFastMathAndSolve(firstOperand, operator, secondOperand);
145+
return useFastMathAndSolve(firstOperand.doubleValue(), operator, secondOperand.doubleValue());
146146
default:
147147
return BigDecimal.ZERO;
148148
}
@@ -160,7 +160,7 @@ protected static BigDecimal makeOperation(BigDecimal secondOperand, String opera
160160
protected static BigDecimal makeUnaryOperation(BigDecimal operand, String operator)
161161
{
162162
if (isFunctionalOperator(operator))
163-
return useFastMathAndSolve(operand, operator, null);
163+
return useFastMathAndSolve(operand.doubleValue(), operator, 0);
164164
switch (operator)
165165
{
166166
case "u-":
@@ -216,61 +216,62 @@ protected static String formatResult(BigDecimal bigDecimal, int precision)
216216
return bigDecimal.stripTrailingZeros().toPlainString();
217217
}
218218

219-
private static BigDecimal useFastMathAndSolve(BigDecimal firstOperand, String operator, BigDecimal secondOperand)
219+
private static BigDecimal useFastMathAndSolve(double firstOperand, String operator, double secondOperand)
220220
{
221221
double result = 0;
222222
switch (operator)
223223
{
224224
case "^":
225-
result = FastMath.pow(firstOperand.doubleValue(), secondOperand.doubleValue());
225+
result = FastMath.pow(firstOperand, secondOperand);
226226
break;
227227
case "sin":
228-
result = FastMath.sin(firstOperand.doubleValue());
228+
result = FastMath.sin(firstOperand);
229229
break;
230230
case "cos":
231-
result = FastMath.cos(firstOperand.doubleValue());
231+
result = FastMath.cos(firstOperand);
232232
break;
233233
case "tan":
234-
result = FastMath.tan(firstOperand.doubleValue());
234+
result = FastMath.tan(firstOperand);
235235
break;
236236
case "asin":
237237
case "arcsin":
238-
result = FastMath.asin(firstOperand.doubleValue());
238+
result = FastMath.asin(firstOperand);
239239
break;
240240
case "acos":
241241
case "arccos":
242-
result = FastMath.acos(firstOperand.doubleValue());
242+
result = FastMath.acos(firstOperand);
243243
break;
244244
case "atan":
245245
case "arctan":
246-
result = FastMath.atan(firstOperand.doubleValue());
246+
result = FastMath.atan(firstOperand);
247247
break;
248248
case "csc":
249-
result = 1 / FastMath.sin(firstOperand.doubleValue());
249+
result = 1 / FastMath.sin(firstOperand);
250250
break;
251251
case "sec":
252-
result = 1 / FastMath.cos(firstOperand.doubleValue());
252+
result = 1 / FastMath.cos(firstOperand);
253253
break;
254254
case "cot":
255-
result = 1 / FastMath.tan(firstOperand.doubleValue());
255+
result = 1 / FastMath.tan(firstOperand);
256256
break;
257257
case "ln":
258-
result = FastMath.log(firstOperand.doubleValue());
258+
result = FastMath.log(firstOperand);
259259
break;
260260
case "log":
261-
result = FastMath.log10(firstOperand.doubleValue());
261+
result = FastMath.log10(firstOperand);
262262
break;
263263
case "log2":
264-
result = FastMath.log(2, firstOperand.doubleValue());
264+
result = FastMath.log(2, firstOperand);
265265
break;
266266
case "sqrt":
267-
return makeUnaryOperation(firstOperand, "√");
267+
result = FastMath.sqrt(firstOperand);
268+
break;
268269
case "cbrt":
269-
result = FastMath.cbrt(firstOperand.doubleValue());
270+
result = FastMath.cbrt(firstOperand);
270271
break;
271272
}
272-
String operationData = secondOperand != null ? firstOperand.toPlainString() + "^" + secondOperand.toPlainString()
273-
: operator + "(" + firstOperand.toPlainString() + ")";
273+
String operationData = secondOperand != 0 ? firstOperand + "^" + secondOperand
274+
: operator + "(" + firstOperand + ")";
274275
if (Double.isNaN(result))
275276
throw new NotNumericResultException("Not numeric result obtained when trying to solve " + operationData);
276277
else if (Double.isInfinite(result))

0 commit comments

Comments
 (0)