It would be good to have an explicit check for `d == 0`, to make the error messages nicer.