Casting types (int as real)

1,178 views
Skip to first unread message

FHRB Toledo

unread,
Nov 28, 2013, 12:22:38 PM11/28/13
to stan-...@googlegroups.com
Hi everyone,

I have just a simple question...

I was declaring a input variable as INT, however in the generated quantities block I will use this INT variable as a denominator of one ratio with REAL values.

When I compile this model I have not any error or warning related to this.

The Stan is casting the INT as REAL to make the ratio or it is casting the REAL values to INT?

Thank you any answer.

Cheers,
FH


FHRB Toledo

unread,
Nov 28, 2013, 1:06:49 PM11/28/13
to stan-...@googlegroups.com
Hi,

I did some tests looking for this things and as I was expecting the Stan are doing the things in the that is properly for my model.

When the INT variable was divided by a REAL ones it was interpreted as REAL and then the ratio is REAL to.

Cheers,
FH

Bob Carpenter

unread,
Nov 28, 2013, 1:10:52 PM11/28/13
to stan-...@googlegroups.com
Right. Stan's arithmetic is just translated to C++
arithmetic, so that's what's supposed to happen --- the "lower"
type gets promoted.

There's a warning if you try to divide int by int, because it will
be rounded.

- Bob
> --
> You received this message because you are subscribed to the Google Groups "stan users mailing list" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to stan-users+...@googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.

FHRB Toledo

unread,
Nov 28, 2013, 2:57:01 PM11/28/13
to stan-...@googlegroups.com
Thank you Bob,

In this thread, is there a way to declare casting type within Stan? Just to avoid "semantic" errors in the same way as in C++ native code!

Cheers,
FH


To unsubscribe from this group and stop receiving emails from it, send an email to stan-users+unsubscribe@googlegroups.com.

For more options, visit https://groups.google.com/groups/opt_out.
--
You received this message because you are subscribed to the Google Groups "stan users mailing list" group.
To unsubscribe from this group and stop receiving emails from it, send an email to stan-users+unsubscribe@googlegroups.com.

Bob Carpenter

unread,
Nov 28, 2013, 3:24:52 PM11/28/13
to stan-...@googlegroups.com


On 11/28/13, 2:57 PM, FHRB Toledo wrote:
> Thank you Bob,
>
> In this thread, is there a way to declare casting type within Stan?

I'm not sure what you're asking for.

If you have:

int n;
int m;
double frac;

frac <- n / m;

then it's probably not going to do what you expect if
your expectations come from R, where "1/4" evaluates to 0.25.
In Stan, "1/4" evaluates to 0.

If you want to force a double-based division on two
integers, you can rely on promotion:

frac <- n;
frac <- frac / m;

We don't have any plans to add C-style or C++-style casts.

> Just to avoid "semantic" errors in the same way as
> in C++ native code!

int / int is defined to return an int, so it's
a feature, not an error, semantic or otherwise.

It absolutely needs to be this way in order to preserve return
types and bottom-up evaluations. Expressions like "m / n" get evaluated
bottom-up in the sense that they don't depend on the return type.
My understanding is that in R, everything's underlyingly a double.

- Bob

FHRB Toledo

unread,
Nov 28, 2013, 3:57:34 PM11/28/13
to stan-...@googlegroups.com
Thank you Bob,

Please let me seem more clear:

int n;
real z;

If I divide z / n the int n shall be promoted to real, and it is fine.

I know that the division of int need to be int and it is fine too.

Looking to your explanations everything seems to be more clear for me.

Thank you again.

Cheers,
FH




Reply all
Reply to author
Forward
0 new messages