From 2a9026f7489d36ae9633de6229f0e217803cf5ad Mon Sep 17 00:00:00 2001 From: Danila Fedorin Date: Fri, 3 Nov 2017 21:41:46 -0700 Subject: [PATCH] Add and use isInteger function where appropriate. --- .../main/kotlin/org/nwapw/abacus/number/NumberInterface.kt | 7 +++++++ .../nwapw/abacus/plugin/standard/operator/OperatorCaret.kt | 4 ++-- .../abacus/plugin/standard/operator/OperatorFactorial.kt | 2 +- .../nwapw/abacus/plugin/standard/operator/OperatorNcr.kt | 4 ++-- .../nwapw/abacus/plugin/standard/operator/OperatorNpr.kt | 4 ++-- 5 files changed, 14 insertions(+), 7 deletions(-) diff --git a/core/src/main/kotlin/org/nwapw/abacus/number/NumberInterface.kt b/core/src/main/kotlin/org/nwapw/abacus/number/NumberInterface.kt index 3279af6..9b14ae9 100644 --- a/core/src/main/kotlin/org/nwapw/abacus/number/NumberInterface.kt +++ b/core/src/main/kotlin/org/nwapw/abacus/number/NumberInterface.kt @@ -221,6 +221,13 @@ abstract class NumberInterface: Comparable { return fractionalPartInternal() } + /** + * Checks whether the given number is an integer or not. + * + * @return whether the number is an integer or not. + */ + fun isInteger() = fractionalPart().signum() == 0 + /** * Returns a NumberRangeBuilder object, which is used to create a range. * The reason that this returns a builder and not an actual range is that diff --git a/core/src/main/kotlin/org/nwapw/abacus/plugin/standard/operator/OperatorCaret.kt b/core/src/main/kotlin/org/nwapw/abacus/plugin/standard/operator/OperatorCaret.kt index 7dfc804..7d67545 100644 --- a/core/src/main/kotlin/org/nwapw/abacus/plugin/standard/operator/OperatorCaret.kt +++ b/core/src/main/kotlin/org/nwapw/abacus/plugin/standard/operator/OperatorCaret.kt @@ -17,7 +17,7 @@ class OperatorCaret: NumberOperator(OperatorAssociativity.RIGHT, OperatorType.BI override fun matchesParams(context: MutableEvaluationContext, params: Array) = params.size == 2 && !(params[0].signum() == 0 && params[1].signum() == 0) - && !(params[0].signum() == -1 && params[1].fractionalPart().signum() != 0) + && !(params[0].signum() == -1 && !params[1].isInteger()) override fun applyInternal(context: MutableEvaluationContext, params: Array): NumberInterface { val implementation = context.inheritedNumberImplementation @@ -26,7 +26,7 @@ class OperatorCaret: NumberOperator(OperatorAssociativity.RIGHT, OperatorType.BI else if (params[1].signum() == 0) return implementation.instanceForString("1") //Detect integer bases: - if (params[0].fractionalPart().signum() == 0 + if (params[0].isInteger() && FUNCTION_ABS.apply(context, params[1]) < implementation.instanceForString(Integer.toString(Integer.MAX_VALUE)) && FUNCTION_ABS.apply(context, params[1]) >= implementation.instanceForString("1")) { val newParams = arrayOf(params[0], params[1].fractionalPart()) diff --git a/core/src/main/kotlin/org/nwapw/abacus/plugin/standard/operator/OperatorFactorial.kt b/core/src/main/kotlin/org/nwapw/abacus/plugin/standard/operator/OperatorFactorial.kt index df62e0c..2d04c50 100644 --- a/core/src/main/kotlin/org/nwapw/abacus/plugin/standard/operator/OperatorFactorial.kt +++ b/core/src/main/kotlin/org/nwapw/abacus/plugin/standard/operator/OperatorFactorial.kt @@ -15,7 +15,7 @@ class OperatorFactorial: NumberOperator(OperatorAssociativity.LEFT, OperatorType override fun matchesParams(context: MutableEvaluationContext, params: Array) = params.size == 1 - && params[0].fractionalPart().signum() == 0 + && params[0].isInteger() && params[0].signum() >= 0 override fun applyInternal(context: MutableEvaluationContext, params: Array): NumberInterface { diff --git a/core/src/main/kotlin/org/nwapw/abacus/plugin/standard/operator/OperatorNcr.kt b/core/src/main/kotlin/org/nwapw/abacus/plugin/standard/operator/OperatorNcr.kt index b51ceca..df632ac 100644 --- a/core/src/main/kotlin/org/nwapw/abacus/plugin/standard/operator/OperatorNcr.kt +++ b/core/src/main/kotlin/org/nwapw/abacus/plugin/standard/operator/OperatorNcr.kt @@ -16,8 +16,8 @@ import org.nwapw.abacus.plugin.standard.StandardPlugin.* class OperatorNcr: NumberOperator(OperatorAssociativity.RIGHT, OperatorType.BINARY_INFIX, 0) { override fun matchesParams(context: MutableEvaluationContext, params: Array) = - params.size == 2 && params[0].fractionalPart().signum() == 0 - && params[1].fractionalPart().signum() == 0 + params.size == 2 && params[0].isInteger() + && params[1].isInteger() override fun applyInternal(context: MutableEvaluationContext, params: Array) = OP_NPR.apply(context, *params) / OP_FACTORIAL.apply(context, params[1]) diff --git a/core/src/main/kotlin/org/nwapw/abacus/plugin/standard/operator/OperatorNpr.kt b/core/src/main/kotlin/org/nwapw/abacus/plugin/standard/operator/OperatorNpr.kt index a316663..588fe8c 100644 --- a/core/src/main/kotlin/org/nwapw/abacus/plugin/standard/operator/OperatorNpr.kt +++ b/core/src/main/kotlin/org/nwapw/abacus/plugin/standard/operator/OperatorNpr.kt @@ -15,8 +15,8 @@ import org.nwapw.abacus.number.NumberInterface class OperatorNpr: NumberOperator(OperatorAssociativity.RIGHT, OperatorType.BINARY_INFIX, 0) { override fun matchesParams(context: MutableEvaluationContext, params: Array) = - params.size == 2 && params[0].fractionalPart().signum() == 0 - && params[1].fractionalPart().signum() == 0 + params.size == 2 && params[0].isInteger() + && params[1].isInteger() override fun applyInternal(context: MutableEvaluationContext, params: Array): NumberInterface { val implementation = context.inheritedNumberImplementation