math: improve how casts are handled