gen changing names of parameters, screwing up my formula, causing errors.

elanhickler's icon

i type this in to codebox:

round(num)
{
    b = abs(num);
    a = ceil(b - 1);
    a = b - a;

    if(a >= .5){
        b = ceil(b);
    }
    else{
        b = floor(b);
    }

    if(num < 0){
        num = -b;
    }
    else{
        num = b;
    }

    return num;
}

out1 = round(in1/in2)*in2;

i get this in "code" tab:

round(num) {
    b = abs(b);
    a = ceil(b - 1);
    a_1 = b - a;
    if(a_1 >= 0.5) {
        b = ceil(b);
    }
    else {
        b = floor(b);
    }
    if(b < 0) {
        b = -b;
    }
    return b;
}

expr_2 = round(in1 / in2) * in2;
out1 = expr_2;

notice num is not even passed into the function.

Max Patch
Copy patch and select New From Clipboard in Max.

whyyyyyy? :(
btw,

elanhickler's icon

it's obviously gen not liking the fact im using the same variable multiple times or something. so there's a formula that gen doesn't mind:

round(num)
{
    rounded=0.0;
    pos = abs(num);
    up = ceil(pos);
    down = floor(pos);

    checkup = up - pos;
    checkdown = pos - down;

    if(checkup
        rounded = up;
    }
    else{
        rounded = down;
    }

    return sign(num)*rounded;
}

out1 = round(in1/in2)*in2;

Wesley Smith's icon

This is definitely a bug. Thanks for the example. Here's a slight modification to the code that will compiler properly:

round(num) {
    b = abs(num);
    a = ceil(b - 1);
    a = b-a;
    if(a > 0.5) {
        b = ceil(b);
    }
    else {
        b = floor(b);
    }

    res = 0;
    if(num < 0) {
        res = -b;
    }
    else {
        res = b;
    }
    return res;
}

elanhickler's icon

another problem

x=1;
y=2;
x,y=y,x;
out1 = x;
out2 = y;

turns into

expr_1 = 2;
expr_2 = 2;
out2 = expr_2;
out1 = expr_1;

notice how both outputs are now equal to 2.