Fix class cast exceptions inside UnaryMathOp

Perform proper class casts to prevent exceptions
from occuring and add unit tests around them.