I mean it should be

    (val + w) > wmax ? (wmax - w) : val