Integer division

I had this line in my Stan program:
L = sqrt(1/sigma[n]^2 + 1/tau^2);

In compilation, I got this warning:

Info: Found int division at ‘/Users/andrew/AndrewFiles/research/stan/schools/eight_schools_kleppe.stan’, line 18, column 13 to column 14:
1 / sigma[n] ^ 2
Positive values rounded down, negative values rounded up or down in platform-dependent way.
Info: Found int division at ‘/Users/andrew/AndrewFiles/research/stan/schools/eight_schools_kleppe.stan’, line 18, column 30 to column 31:
1 / tau ^ 2
Positive values rounded down, negative values rounded up or down in platform-dependent way.

I switched to the following and it was fine:
L = sqrt(1./sigma[n]^2 + 1./tau^2);

OK, OK, I understand that Stan is strongly typed, etc., I should read the damn manual, etc etc.

But as a user, I’d like to be able to type 1/tau^2 and get the obvious meaning. Maybe it would make sense to change these sorts of arithmetic functions so that when you combine an integer and a real, it promotes the integer to real. This seems natural to me.

And it will make a difference. Code that’s littered with “1.”, “0.0”, etc., is harder to read, and it leads to a paranoia among casual users who will feel the need to add decimal places everywhere, just in case. It’s just one little thing that makes the code look less like math, less like stat, and more like machine code.

2 Likes

This comes up every couple of years. I think the obstacle was we couldn’t recognize when someone wants to do integer division if we always promote literal numbers to double precision. In general, people should be using the inv(x) function instead of 1 / x. But in this case, it could be L = inv_sqrt(square(sigma[n]) + square(tau)); or L = inv(hypot(sigma[n], tau));.

2 posts were split to a new topic: Proposal: Explicit integer division