Handle negative numbers in `sqrt` properly.

Closes #9987.