# "cannot divide by 0" TinyMath error in Visual builder

**URL:** <https://discuss.elastic.co/t/cannot-divide-by-0-tinymath-error-in-visual-builder/268251>\
**Category:** Kibana\
**Created:** [March 24, 2021, 5:40pm UTC](https://discuss.elastic.co/t/cannot-divide-by-0-tinymath-error-in-visual-builder/268251 "2021-03-24T17:40:58Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![preetish\_P](https://avatars.discourse-cdn.com/v4/letter/p/77aa72/32.png) [@preetish\_P](https://discuss.elastic.co/u/preetish_P)\
**Post date:** [March 24, 2021, 5:40pm UTC](https://discuss.elastic.co/t/cannot-divide-by-0-tinymath-error-in-visual-builder/268251/1 "2021-03-24T17:40:58Z")

</div>

Hello Elastic community,

I am trying to build a metric in visual builder with 2 aggregations and a Math aggregation below them. But I am getting "Cannot divide by 0".

Below is my structure of data:

| sr\_number | some\_other\_column |
| --- | --- |
| sr1 | abcd |
| sr1 | abcd |
| sr1 | abcd |
| sr1 | abcd |
| sr2 | abcd |
| sr2 | abcd |

I want to calculate : (Count of docs / unique of sr\_number) .i.e. average per sr.

Below is the error I get when using a visual builder:

 ![Screen Shot 2021-03-24 at 17.30.55](https://us1.discourse-cdn.com/elastic/original/3X/8/c/8c9935154b2ce10c1470898b4074f3c62b6fcb4d.png)

Surprisingly the two variables: `params.count_of_events` and `params.count_of_srs` have the correct values and work when used in other math functions.

E.g: when using add(a,b)

 ![Screen Shot 2021-03-24 at 17.35.14](https://us1.discourse-cdn.com/elastic/original/3X/5/5/55078df18cae0939cc50a5f7a4a337f6b5820a96.png)

When using mutitply(a,b)

 ![Screen Shot 2021-03-24 at 17.35.56](https://us1.discourse-cdn.com/elastic/original/3X/a/7/a775a392e3d36ae4922a1dae41c152ed06c67945.png)

The variables show values individually too:

 ![Screen Shot 2021-03-24 at 17.36.42](https://us1.discourse-cdn.com/elastic/original/3X/f/f/ff270ea1a623c1597675a41578aa793a76dae2d0.png) ![Screen Shot 2021-03-24 at 17.36.31](https://us1.discourse-cdn.com/elastic/original/3X/5/c/5cc2ecba9b5b52f657efc54ea4ca9bbb911c9993.png)

Has anyone faced such issues ? Any advice/workarounds is appreciated.

Thank you

---

<div class="post-metadata">

**Author:** ![tsullivan](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/tsullivan/32/31077_2.png) [@tsullivan](https://discuss.elastic.co/u/tsullivan)\
**Post date:** [March 26, 2021, 11:21pm UTC](https://discuss.elastic.co/t/cannot-divide-by-0-tinymath-error-in-visual-builder/268251/2 "2021-03-26T23:21:21Z")

</div>

It's not possible to divide by zero in math. As the visual builder calculates this expression for every value in the date histogram, there seems to be a section of time where the document count was zero for the query.

That said, which version of Kibana are you using? I am unable to reproduce this in 7.12.

In the mean-time, you could potentially use the [clamp](https://github.com/elastic/tinymath/blob/master/docs/functions.md#clamp-a--min--max-) function to prevent this from being zero.

---

<div class="post-metadata">

**Author:** ![preetish\_P](https://avatars.discourse-cdn.com/v4/letter/p/77aa72/32.png) [@preetish\_P](https://discuss.elastic.co/u/preetish_P)\
**Post date:** [March 29, 2021, 3:26pm UTC](https://discuss.elastic.co/t/cannot-divide-by-0-tinymath-error-in-visual-builder/268251/3 "2021-03-29T15:26:53Z")

</div>

Thank you @tsullivan

I am running ES v6.8.3. I wasn't aware that visual builder first creates a date histogram and then combines the result.

Using `clamp` function solved the error and now I am able to see the result of division.

However I have concerns with the accuracy of `cardinality` function as JSON input cannot be provided to visual builder as per this : [Support json\_input for visual builder or shard\_size · Issue #23399 · elastic/kibana · GitHub](https://github.com/elastic/kibana/issues/23399)

Hence I have decided to use Canvas dashboard for my implementation.

---

<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:** [April 26, 2021, 3:27pm UTC](https://discuss.elastic.co/t/cannot-divide-by-0-tinymath-error-in-visual-builder/268251/4 "2021-04-26T15:27:09Z")

</div>

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