Painless - way to test if division results in a whole number (no remainder)?

In javascript Number.isInteger() will return a boolean indicating whether or not the given value is an integer. Is there an equivalent function in Painless I can call to test if a division calculation results in a whole number or another way to perform this test?

E.g. if the calculation in my condition resulted in 1.0 it would return true, but if 1.1 it would return false.

Note: I don't want to enable regular expressions in scripts if it can be helped at this point.

Also Note: I'm multiplying the commonDifference and termValue by 1.0 because painless/Java does int division on integers and only returns an int even if there is a remainder and I need to evaluate if there is a remainder.

Another Note: This is arithmetic sequence math and I'm trying to determine if a certain value exists in a sequence.

I don't see any equivalent functions in the Integer or Double API, but maybe I missed some functions that may be useful for my use case. If it is not in the API, any bright ideas on how I can test if this is a whole number w/out regex?

"script": {
  "source": """
    def commonDifference = doc['repeated_every'].value * 1.0;
    def termValue = params.term_value * 1.0 - 1;

    if (Number.isInteger(termValue / commonDifference)) { 
        return true;
    }
    """,
  "lang": "painless",
  "params": {
    "term_value": 6
  }
}

Hi Zelfapp,

I believe what you're trying to do can be accomplished by leaving commonDifference and termValue as int types (don't multiply them by 1.0 to make them doubles), and then using the remainder operator (https://www.elastic.co/guide/en/elasticsearch/painless/current/painless-operators-numeric.html#remainder-operator) on them. By leaving them as int types, there is a guarantee that the remainder will also be an int type.

def commonDifference = doc['repeated_every'].value;
def termValue = params.term_value - 1;

return commonDifference % termValue == 0;

Edit: The formal Java spec for the remainder operator which Painless utilizes (https://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#jls-15.17.3)

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.