# Painless using comparing to a float

**URL:** <https://discuss.elastic.co/t/painless-using-comparing-to-a-float/102013>\
**Category:** Elasticsearch\
**Created:** [September 27, 2017, 5:37pm UTC](https://discuss.elastic.co/t/painless-using-comparing-to-a-float/102013 "2017-09-27T17:37:42Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![jspeer](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jspeer](https://discuss.elastic.co/u/jspeer)\
**Post date:** [September 27, 2017, 5:37pm UTC](https://discuss.elastic.co/t/painless-using-comparing-to-a-float/102013/1 "2017-09-27T17:37:42Z")

</div>

I'm trying to divide 2 buckets, and check against a float to see if the value has decreased  
This works

```
"condition": {
"script": {
  "inline": "return ((ctx.payload.aggregations.today.buckets.latest60.doc_count)/(ctx.payload.aggregations.today.buckets.previous24.doc_count) < 1)",
  "lang": "painless"
}
},

```

however when I try to test against a float it's not working

```
"condition": {
"script": {
  "inline": "return ((ctx.payload.aggregations.today.buckets.latest60.doc_count)/(ctx.payload.aggregations.today.buckets.previous24.doc_count) < .75F)",
  "lang": "painless"
}

```

},

Here is what the buckets are returning

```
"aggregations": {
      "today": {
        "buckets": {
          "latest60": {
            "doc_count": 54983
          },
          "previous24": {
            "doc_count": 54999
          }
        }
      }
    }
```

---

<div class="post-metadata">

**Author:** ![jspeer](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jspeer](https://discuss.elastic.co/u/jspeer)\
**Post date:** [September 27, 2017, 8:45pm UTC](https://discuss.elastic.co/t/painless-using-comparing-to-a-float/102013/2 "2017-09-27T20:45:55Z")

</div>

I'm removed the fields all together to test... This always comes back as true

```
"condition": {
"script": {
  "inline": "int total=0; total = ((54983/55000) * 100); if (total < 75) {return true}",
  "lang": "painless"
}

```

},

total should equal 99.969 so the logic should return false.

---

<div class="post-metadata">

**Author:** ![jspeer](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jspeer](https://discuss.elastic.co/u/jspeer)\
**Post date:** [September 27, 2017, 9:45pm UTC](https://discuss.elastic.co/t/painless-using-comparing-to-a-float/102013/3 "2017-09-27T21:45:26Z")

</div>

and I've simplified that to

```
"condition": {
"script": {
  "inline": "return (((54983/55000) * 100) < 75)",
  "lang": "painless"
}

```

}

Which also fails.

---

<div class="post-metadata">

**Author:** ![jspeer](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jspeer](https://discuss.elastic.co/u/jspeer)\
**Post date:** [September 28, 2017, 3:27pm UTC](https://discuss.elastic.co/t/painless-using-comparing-to-a-float/102013/4 "2017-09-28T15:27:36Z")

</div>

moved to x-pack sub

---

<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, 2017, 1:14pm UTC](https://discuss.elastic.co/t/painless-using-comparing-to-a-float/102013/5 "2017-10-02T13:14:15Z")

</div>

Hey,

welcome to the intricacies of java based rounding. As you are dividing to integers, the return value is `0`, which then is multiplied with 100.

Change your calculation to `return (((54983.0/55000) * 100) < 75)` and suddenly everything works as expected as floating point numbers are used for the calculation...

--Alex

---

<div class="post-metadata">

**Author:** ![jspeer](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jspeer](https://discuss.elastic.co/u/jspeer)\
**Post date:** [October 2, 2017, 4:47pm UTC](https://discuss.elastic.co/t/painless-using-comparing-to-a-float/102013/6 "2017-10-02T16:47:32Z")

</div>

Thanks Alex, I was also given another solution that does the same, from support:

return ((((double)54983/55000) \* 100) \< 75)

but I appreciate the reply!

---

<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, 2017, 4:47pm UTC](https://discuss.elastic.co/t/painless-using-comparing-to-a-float/102013/7 "2017-10-30T16:47:33Z")

</div>

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