The reason your code sometimes shows 2 instead of 0 or 1 is due to floating-point numbers. You can try using min
:root { --w: min(mod(tan(atan2(var(--vw), 1px)), 2), 1.9999); }