Merge pull request #46 from DanilaFe/isint-idiom

Add and use isInteger function where appropriate.
This commit is contained in:
Danila Fedorin 2017-11-14 23:13:10 -08:00 committed by GitHub
commit 07abe4d17a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 14 additions and 7 deletions

View File

@ -221,6 +221,13 @@ abstract class NumberInterface: Comparable<NumberInterface> {
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

View File

@ -17,7 +17,7 @@ class OperatorCaret: NumberOperator(OperatorAssociativity.RIGHT, OperatorType.BI
override fun matchesParams(context: MutableEvaluationContext, params: Array<out NumberInterface>) =
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<out NumberInterface>): 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())

View File

@ -15,7 +15,7 @@ class OperatorFactorial: NumberOperator(OperatorAssociativity.LEFT, OperatorType
override fun matchesParams(context: MutableEvaluationContext, params: Array<out NumberInterface>) =
params.size == 1
&& params[0].fractionalPart().signum() == 0
&& params[0].isInteger()
&& params[0].signum() >= 0
override fun applyInternal(context: MutableEvaluationContext, params: Array<out NumberInterface>): NumberInterface {

View File

@ -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<out NumberInterface>) =
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<out NumberInterface>) =
OP_NPR.apply(context, *params) / OP_FACTORIAL.apply(context, params[1])

View File

@ -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<out NumberInterface>) =
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<out NumberInterface>): NumberInterface {
val implementation = context.inheritedNumberImplementation