Compilation error - passes syntax check

The following function passes syntax check using cmdstanr, but it will not compile:

        real beta_PDF(real x, real a, real b) {
                if(a<=0 || b<=0) {
                        print("bad a or b");
                        return 0;
                }
                if(x<=0 || x>=1) {
                        if(x<0 || x>1) {
                                print("bad x input into beta_pdf: ", x);
                        }
                        return 0;
                }
                return (1 / beta(a,b)) * pow(x, a-1) * pow((1-x), b-1);
        }

        real[] fX_ODE(real x, array[] real y, array[] real theta, array[] real x_r, array[] int x_i ) {
                real m = theta[1];
                real M = theta[2];
                real sO1 = theta[3];
                real sO2 = theta[4];
                real sD1 = theta[5];
                real sD2 = theta[6];
                real t = theta[7];
                if(M==x) {
                        return { 0.0 };
                }
                real res = (beta_PDF((x-m)/(M-m), sO1, sO2)/(M-m)) * (1-beta_cdf((t-x)/(M-x), sD1, sD2));
                return { res };
        }

However, the following will compile:

[...]
real[] fX_ODE(real x, array[] real y, array[] real theta, array[] real x_r, array[] int x_i ) {
                real m = theta[1];
                real M = theta[2];
                real sO1 = theta[3];
                real sO2 = theta[4];
                real sD1 = theta[5];
                real sD2 = theta[6];
                real t = theta[7];
                if(M==x) {
                        real resB = 0.0;
                        return { resB };
                }
                real res = (beta_PDF((x-m)/(M-m), sO1, sO2)/(M-m)) * (1-beta_cdf((t-x)/(M-x), sD1, sD2));
                return { res };
        }

The issue seems to be in the “return { 0.0 };” statement. Why doesn’t the compiler recognize the correct data type here?

This isn’t a big deal, but I couldn’t find it documented, and it took a while to discover the hack that made the program compile.

Thanks for your thoughts.

I don’t know why the return(0.0) statement doesn’t work, but in the interest of having one point of return from a function, you might consider writing

real res;
if (M == x) {
   res = 0.0;
} else
   res = (beta_PDF((x-m)/(M-m), sO1, sO2)/(M-m)) * (1-beta_cdf((t-x)/(M-x), sD1, sD2));
}
return res;

I haven’t checked, but presumably that will also work.

1 Like

Thank you for the reply!

Just out of curiosity, why is it preferred to have one point of return from a function?

This seems like a bug in our compiler! Thanks for reporting, we will have this fixed in the next release. In the mean time, assigning a variable seems like a good work around for this

1 Like

Thanks for the reply. Glad to know I wasn’t misunderstanding something about variables and return types.

I find a single return clearer and easier to maintain, but your mileage may vary: Should a function or method have only one return statement? | by Atakan DemircioÄźlu | Medium.

Thanks for the response and the link!