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

Updated to use BigInteger.mod function for integer values, or #132

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
60 changes: 54 additions & 6 deletions CURRENT/java/src/org/mariuszgromada/math/mxparser/Expression.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@
package org.mariuszgromada.math.mxparser;

import java.io.ByteArrayInputStream;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.List;
import java.util.ArrayList;
import java.util.Stack;
Expand Down Expand Up @@ -2550,6 +2552,15 @@ private void RANDOM_VARIABLE(int pos) {
private double getTokenValue(int tokenIndex) {
return tokensList.get(tokenIndex).tokenValue;
}

/**
* Gets token value and returns as String
* @param tokenIndex the token index
* @return the token value as String
*/
private String getTokenValueAsString (int tokenIndex) {
return tokensList.get(tokenIndex).tokenStr;
}
/**
* Tetration handling.
*
Expand All @@ -2576,9 +2587,36 @@ private void POWER(int pos) {
* @param pos the token position
*/
private void MODULO(int pos) {
double a = getTokenValue(pos-1);
double b = getTokenValue(pos+1);
opSetDecreaseRemove(pos, MathFunctions.mod(a, b) );
String a = getTokenValueAsString(pos-1);
String b = getTokenValueAsString(pos+1);

if (isInteger(a) && isInteger(b)) {
BigInteger bigA = new BigInteger(a);
BigInteger bigB = new BigInteger(b);
opSetDecreaseRemove(pos, bigA.mod(bigB).doubleValue());

} else {
BigDecimal doubleA = new BigDecimal(a);
BigDecimal doubleB = new BigDecimal(b);
opSetDecreaseRemove(pos, doubleA.remainder(doubleB).doubleValue());
}
}

/**
* Check if the param is an Integer.
* @param s the value to be checked
* @return true if it's an Integer
*/
private boolean isInteger(String s) {
try {
Integer.parseInt(s);
} catch(NumberFormatException e) {
return false;
} catch(NullPointerException e) {
return false;
}
// only got here if we didn't return false
return true;
}
/**
* Division handling.
Expand Down Expand Up @@ -3563,9 +3601,19 @@ private List<Double> getNumbers(int pos) {
* @param pos the token position
*/
private void MOD(int pos) {
double a = getTokenValue(pos+1);
double b = getTokenValue(pos+2);
f2SetDecreaseRemove(pos, MathFunctions.mod(a, b) );
String a = getTokenValueAsString(pos+1);
String b = getTokenValueAsString(pos+2);

if (isInteger(a) && isInteger(b)) {
BigInteger bigA = new BigInteger(a);
BigInteger bigB = new BigInteger(b);
f2SetDecreaseRemove(pos, bigA.mod(bigB).doubleValue());

} else {
BigDecimal doubleA = new BigDecimal(a);
BigDecimal doubleB = new BigDecimal(b);
f2SetDecreaseRemove(pos, doubleA.remainder(doubleB).doubleValue());
}
}
/**
* Binomial Coefficient
Expand Down