# Painless integer division

**URL:** <https://discuss.elastic.co/t/painless-integer-division/150386>\
**Category:** Elasticsearch\
**Tags:** elastic-stack-alerting\
**Created:** [September 28, 2018, 8:33pm UTC](https://discuss.elastic.co/t/painless-integer-division/150386 "2018-09-28T20:33:23Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![chapmantrain](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/chapmantrain/32/22646_2.png) [@chapmantrain](https://discuss.elastic.co/u/chapmantrain)\
**Post date:** [September 28, 2018, 8:33pm UTC](https://discuss.elastic.co/t/painless-integer-division/150386/1 "2018-09-28T20:33:23Z")

</div>

I am dividing two doc\_counts from an aggregate to produce a percentage and compare it to a percentage. Does integer division in painless produce a float or another integer? I'm using the ctx.vars to get it into a log message to try to debug. Does the log message go to the elastic log or somewhere else?

"inline": "if ((ctx.payload.current\_poll.aggregations.fandp.buckets.0.doc\_count \> 0) && (ctx.payload.current\_poll.aggregations.fandp.buckets.1.doc\_count \> 0)) { ctx.vars.xx = (ctx.payload.current\_poll.aggregations.fandp.buckets.1.doc\_count / (ctx.payload.current\_poll.aggregations.fandp.buckets.0.doc\_count + ctx.payload.current\_poll.aggregations.fandp.buckets.1.doc\_count)); return ctx.vars.xxx \> ctx.payload.week\_poll.aggregations.avg\_fail; } else { return false; }"

---

<div class="post-metadata">

**Author:** ![chapmantrain](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/chapmantrain/32/22646_2.png) [@chapmantrain](https://discuss.elastic.co/u/chapmantrain)\
**Post date:** [October 1, 2018, 7:24pm UTC](https://discuss.elastic.co/t/painless-integer-division/150386/2 "2018-10-01T19:24:51Z")

</div>

Found that painless does do int division (1/3 = 0). I'm not sure of the 'proper' way to convert but I used the python trick (1\*1.0)/3 and I'm getting a decimal number.

---

<div class="post-metadata">

**Author:** ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)\
**Post date:** [October 2, 2018, 6:42am UTC](https://discuss.elastic.co/t/painless-integer-division/150386/3 "2018-10-02T06:42:59Z")

</div>

this is indeed the correct 'trick'. Painless copies its division logic from java, where dividing two integers will result in an integer.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [October 30, 2018, 6:43am UTC](https://discuss.elastic.co/t/painless-integer-division/150386/4 "2018-10-30T06:43:06Z")

</div>

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