# Advanced JSON Inputs Math functions

**URL:** <https://discuss.elastic.co/t/advanced-json-inputs-math-functions/179358>\
**Category:** Elasticsearch\
**Tags:** painless\
**Created:** [May 2, 2019, 12:21pm UTC](https://discuss.elastic.co/t/advanced-json-inputs-math-functions/179358 "2019-05-02T12:21:08Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![6tUBp3gwd9Zp](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/6tubp3gwd9zp/32/45240_2.png) [@6tUBp3gwd9Zp](https://discuss.elastic.co/u/6tUBp3gwd9Zp)\
**Post date:** [May 2, 2019, 12:21pm UTC](https://discuss.elastic.co/t/advanced-json-inputs-math-functions/179358/1 "2019-05-02T12:21:08Z")

</div>

I want to calculate an angle from a x and a y value with this script:

```
{
  "script": {
    "inline": "if (doc['x'].value == 0 and doc['y'].value > 0{return 270;}if (doc['x'].value == 0 and doc['y'].value < 0 {return 90;}return Math.atan(-doc['y'].value/doc['x'].value);",
    "lang": "painless"
  }
}

```

...but it doesnt work. What am i doing wrong?

---

<div class="post-metadata">

**Author:** ![BenTrent](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/bentrent/32/33915_2.png) [@BenTrent](https://discuss.elastic.co/u/BenTrent)\
**Post date:** [May 2, 2019, 1:24pm UTC](https://discuss.elastic.co/t/advanced-json-inputs-math-functions/179358/2 "2019-05-02T13:24:55Z")

</div>

@6tUBp3gwd9Zp there are many syntax issues in the script, mainly forgetting to put the closing parenthesis on your `if` predicates and using `and` instead of `&&`

```auto
{
  "script": {
    "inline": """
    if (doc['x'].value == 0 && doc['y'].value > 0) {
        return 270;
    }
    if (doc['x'].value == 0 && doc['y'].value < 0) {
        return 90;
    }
    return Math.atan(-doc['y'].value/doc['x'].value);
    """,
    "lang": "painless"
  }
}

```

---

<div class="post-metadata">

**Author:** ![6tUBp3gwd9Zp](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/6tubp3gwd9zp/32/45240_2.png) [@6tUBp3gwd9Zp](https://discuss.elastic.co/u/6tUBp3gwd9Zp)\
**Post date:** [May 3, 2019, 8:59am UTC](https://discuss.elastic.co/t/advanced-json-inputs-math-functions/179358/3 "2019-05-03T08:59:37Z")

</div>

Thank you very much this helped a lot.

---

<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:** [May 31, 2019, 8:59am UTC](https://discuss.elastic.co/t/advanced-json-inputs-math-functions/179358/4 "2019-05-31T08:59:39Z")

</div>

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