# Convert real to int

**URL:** <https://discourse.mc-stan.org/t/convert-real-to-int/1092>\
**Category:** General\
**Tags:** stan-math\
**Created:** [June 29, 2017, 8:04am UTC](https://discourse.mc-stan.org/t/convert-real-to-int/1092 "2017-06-29T08:04:34Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dirk\_Nachbar](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/dirk_nachbar/32/4271_2.png) [@Dirk\_Nachbar](https://discourse.mc-stan.org/u/Dirk_Nachbar)\
**Post date:** [June 29, 2017, 8:04am UTC](https://discourse.mc-stan.org/t/convert-real-to-int/1092/1 "2017-06-29T08:04:35Z")

</div>

how can I convert a real to an int. I want to round

int x;  
x = a \* b;  
where a and b and reals

---

<div class="post-metadata">

**Author:** ![bgoodri](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/bgoodri/32/4451_2.png) [@bgoodri](https://discourse.mc-stan.org/u/bgoodri)\
**Post date:** [June 29, 2017, 12:04pm UTC](https://discourse.mc-stan.org/t/convert-real-to-int/1092/2 "2017-06-29T12:04:10Z")

</div>

You can’t. Also, you don’t want to if `a` and `b` are unknown as it would mess up NUTS.

---

<div class="post-metadata">

**Author:** ![Bob\_Carpenter](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/bob_carpenter/32/9230_2.png) [@Bob\_Carpenter](https://discourse.mc-stan.org/u/Bob_Carpenter)\
**Post date:** [July 4, 2017, 1:26am UTC](https://discourse.mc-stan.org/t/convert-real-to-int/1092/3 "2017-07-04T01:26:27Z")

</div>

Are you trying to do some kind of interpolation problem? That’s the only example we’ve seen where this would be useful and not violate continuous differentiability for the posterior. But we’ve never gotten around to building in an interpolation.

---

<div class="post-metadata">

**Author:** ![Dirk\_Nachbar](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/dirk_nachbar/32/4271_2.png) [@Dirk\_Nachbar](https://discourse.mc-stan.org/u/Dirk_Nachbar)\
**Post date:** [July 4, 2017, 7:28am UTC](https://discourse.mc-stan.org/t/convert-real-to-int/1092/5 "2017-07-04T07:28:59Z")

</div>

Actually I wanted x to be the size of a new variable

real xx[x];

---

<div class="post-metadata">

**Author:** ![aaronjg](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/aaronjg/32/3768_2.png) [@aaronjg](https://discourse.mc-stan.org/u/aaronjg)\
**Post date:** [July 4, 2017, 1:07pm UTC](https://discourse.mc-stan.org/t/convert-real-to-int/1092/6 "2017-07-04T13:07:31Z")

</div>

Could you use a work around like this:

> [@Real to Int](http://discourse.mc-stan.org/t/real-to-int/1144/3):
>
> You have to stick to Stan syntax. The while loop would look like this: real a; ... { int i = 1; while (i \< a) { ... go to town ... i = i + 1; } } The extra layer of brackes is to let you define the variable i as a local variable for the loop.

---

<div class="post-metadata">

**Author:** ![Bob\_Carpenter](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/bob_carpenter/32/9230_2.png) [@Bob\_Carpenter](https://discourse.mc-stan.org/u/Bob_Carpenter)\
**Post date:** [July 5, 2017, 8:47pm UTC](https://discourse.mc-stan.org/t/convert-real-to-int/1092/7 "2017-07-05T20:47:50Z")

</div>

Just keep in mind that the posterior should be continuously differentiable. You can write an inefficient linear function to truncate or a more efficient logarithmetic one with binary search, but however you do it, it’s going to break differentiability w.r.t. the integer.

You can also allocate things that are larger and only use some of them if it’s just a placeholder. But no way to get it the right size without hacking.

---

<div class="post-metadata">

**Author:** ![Cyril](https://avatars.discourse-cdn.com/v4/letter/c/35a633/32.png) [@Cyril](https://discourse.mc-stan.org/u/Cyril)\
**Post date:** [April 27, 2020, 2:26am UTC](https://discourse.mc-stan.org/t/convert-real-to-int/1092/8 "2020-04-27T02:26:08Z")

</div>

What about if you wanted to do it in the generated quantities. I was trying to calculate the saturated likelihood for a Poisson model. I was just trying to do something like.

`real saturated_ll = poisson_lpmf(y| y);`

but I get the error,

```
No matches for: 
      poisson_lpmf(int[], int[])

```

any quick suggestions for this calculation?

---

<div class="post-metadata">

**Author:** ![anon75146577](https://avatars.discourse-cdn.com/v4/letter/a/f07891/32.png) [@anon75146577](https://discourse.mc-stan.org/u/anon75146577)\
**Post date:** [April 27, 2020, 3:55am UTC](https://discourse.mc-stan.org/t/convert-real-to-int/1092/9 "2020-04-27T03:55:21Z")

</div>

The second command is the mean of the Poisson. Stan expects its to be real (or vector) not int.

This is the signature from the doc: `real poisson_lpmf(ints n | reals lambda)`.

If you want the posterior predictive, put the mean in the second parameter.

---

<div class="post-metadata">

**Author:** ![Cyril](https://avatars.discourse-cdn.com/v4/letter/c/35a633/32.png) [@Cyril](https://discourse.mc-stan.org/u/Cyril)\
**Post date:** [April 27, 2020, 4:33am UTC](https://discourse.mc-stan.org/t/convert-real-to-int/1092/10 "2020-04-27T04:33:25Z")

</div>

Sorry I should have been clearer. I was more just giving an example that I had come across where I wanted to convert an `int[]` to a `real[] ` that wasn’t related to the differentiation.
