# Calcul producing a negative result

**URL:** <https://discuss.elastic.co/t/calcul-producing-a-negative-result/314141>\
**Category:** Elasticsearch\
**Tags:** painless\
**Created:** [September 11, 2022, 1:34pm UTC](https://discuss.elastic.co/t/calcul-producing-a-negative-result/314141 "2022-09-11T13:34:40Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![hnf](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/hnf/32/109840_2.png) [@hnf](https://discuss.elastic.co/u/hnf)\
**Post date:** [September 11, 2022, 1:34pm UTC](https://discuss.elastic.co/t/calcul-producing-a-negative-result/314141/1 "2022-09-11T13:34:40Z")

</div>

Hello Everyone.  
I'm facing to a negative value while calculting a value from a field.

After creating the index, I add a new field using the path `Data Views -> "index_name"` in Kibana and set value for this new field like this:

`emit(( 100000 * (double) doc['Cell_Out_of_Service_Duration'].value ) / (8302 * 86400 * 5))`

For example, when `doc['Cell_Out_of_Service_Duration'].value` is 900, the result must be `0.025094355` but the result from calculation is `-0.127`.

Can somebody help why this kind of result.  
Thanks in advance

---

<div class="post-metadata">

**Author:** ![hnf](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/hnf/32/109840_2.png) [@hnf](https://discuss.elastic.co/u/hnf)\
**Post date:** [September 11, 2022, 10:40pm UTC](https://discuss.elastic.co/t/calcul-producing-a-negative-result/314141/2 "2022-09-11T22:40:41Z")

</div>

Hello,  
I forgot to mention the version:

**Elasticsearch : 8.3**  
**Kibana : 8.3**  
**Operating Sys : Red Hat Enterprise Linux 8.5 (Ootpa)**

Thanks for your help.

---

<div class="post-metadata">

**Author:** ![stephenb](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/stephenb/32/40856_2.png) [@stephenb](https://discuss.elastic.co/u/stephenb)\
**Post date:** [September 11, 2022, 11:07pm UTC](https://discuss.elastic.co/t/calcul-producing-a-negative-result/314141/3 "2022-09-11T23:07:39Z")

</div>

> [@hnf](#):
>
> emit(( 100000 \* (double) doc['Cell\_Out\_of\_Service\_Duration'].value ) / (8302 \* 86400 \* 5))

Assuming you are making the field type a `double` try

> [@hnf](#):
>
> `emit(( 100000.0 * (double) doc['Cell_Out_of_Service_Duration'].value ) / (8302.0 * 86400.0 * 5.0))`

---

<div class="post-metadata">

**Author:** ![hnf](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/hnf/32/109840_2.png) [@hnf](https://discuss.elastic.co/u/hnf)\
**Post date:** [September 11, 2022, 11:47pm UTC](https://discuss.elastic.co/t/calcul-producing-a-negative-result/314141/4 "2022-09-11T23:47:43Z")

</div>

Hello @stephenb ,

Yes, the field is construct with `double` as type. And thanks a lots. It's work perfectly! The logic is really correct. I thought that the conversion will be implicite as I have C++ as background.

Thanks a lots!

---

<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 9, 2022, 11:47pm UTC](https://discuss.elastic.co/t/calcul-producing-a-negative-result/314141/5 "2022-10-09T23:47:49Z")

</div>

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