Type cast


#1

Hi,
How does one type cast from real to int?
Thank you


#2

You cannot, see 3.2 in the manual.


#3

Thanks.


#4

One ad hoc option is to write your own convert function.


#5

Nono, it might be possible to achieve a real to int cast in some way, but it’d break the autodiff if it were on a var. What are you trying to do with this?


#6

It makes sense to do it with data where, e.g., you need a time measurement as a real but also need to index observation times. No sense in passing both in so I always keep a cast function around.


#7

What exactly are you trying to do?

That’s not always true. The one case where we fail to support something reasonable is when you want to do some kind of interpolation. There, you often branch on the continuous parameters to make a discrete choice, but are then careful to make sure the discrete choices can still be differentiated.


#8

Yeah that’s what I was getting at – shoulda been clearer. A dirt simple 1d interpolation function is something I’ve wanted in Stan a few times so I’m on the lookout for other people who want it.


#9

You’re in a good position to implement one. There’s an interpolation function in BUGS that we’ve been meaning to implement for ages.