jspeer
(Josh Speer)
September 27, 2017, 5:37pm
1
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
}
}
}
}
jspeer
(Josh Speer)
September 27, 2017, 8:45pm
2
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.
jspeer
(Josh Speer)
September 27, 2017, 9:45pm
3
and I've simplified that to
"condition": {
"script": {
"inline": "return (((54983/55000) * 100) < 75)",
"lang": "painless"
}
}
Which also fails.
spinscale
(Alexander Reelsen)
October 2, 2017, 1:14pm
5
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
jspeer
(Josh Speer)
October 2, 2017, 4:47pm
6
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!
system
(system)
Closed
October 30, 2017, 4:47pm
7
This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.