% is a proper modulus operator, not reminder. #fixes 842, #fixes 843

This commit is contained in:
gnembon committed 2021-05-10 00:49:04 -03:00
1 parent 6a06b0fb61
commit 451b35b6c5
3 files changed
+21 -4

No files matched your search

@@ -80,7 +80,7 @@ public class Operators {
expression.addFunctionalEquivalence("/", "quotient");
expression.addBinaryOperator("%", precedence.get("multiplication*/%"), true, (v1, v2) ->
new NumericValue(NumericValue.asNumber(v1).getDouble() % NumericValue.asNumber(v2).getDouble()));
NumericValue.asNumber(v1).mod(NumericValue.asNumber(v2)));
expression.addBinaryOperator("^", precedence.get("exponent^"), false, (v1, v2) ->
new NumericValue(java.lang.Math.pow(NumericValue.asNumber(v1).getDouble(), NumericValue.asNumber(v2).getDouble())));
@@ -313,4 +313,14 @@ public class NumericValue extends Value
{
return longValue!= null || getDouble() == (double)getLong();
}
public Value mod(NumericValue n2)
{
if (this.longValue != null && n2.longValue != null)
return new NumericValue(Math.floorMod(longValue, n2.longValue));
double x = value;
double y = n2.value;
if (y == 0) throw new ArithmeticException("Division by zero");
return new NumericValue( x - Math.floor(x / y) * y);
}
}