Test your math

I’m using this as to work out positions on a bezier cubic curve:

t = Math.min(Math.max((position/total),0),1);
    
    var cx = 3* (control1.x - begin.x);
    var bx = 3* (control2.x - control1.x) - cx;
    var ax = end.x - begin.x - cx - bx;
    var zx = ax*t*t*t + bx*t*t + cx*t + begin.x;

    var cy = 3* (control1.y - begin.y);
    var by = 3* (control2.y - control1.x) - cy;
    var ay = end.y - begin.y - cy - by;
    var zy = ay*t*t*t + by*t*t + cy*t +begin.y;

Where zx,zy are the final position. Position is the step in the curve and total is the total number of steps in the curve.
It’s working fine with posotive values but it messes up a bit with negative values. I’m tierd of looking at it, and tierd anyway. Can anyone see why it doesn’t work for negative values?
Thanks!