Unicode degree symbol ° in math formulas: \textdegree invalid in math mode

The code below should work:

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{textcomp}
\usepackage{gensymb}
\begin{document}
Degree symbol in plain text works: 90° is a right angle.
Degree in math now work: $\cos(90\degree) = 0$.
\end{document}

That is why you have the siunitx package:

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{textcomp}
\usepackage{siunitx}
\begin{document}
Degree symbol in plain text works: 90\si{\degree} (or \SI{90}{\degree}) is a right angle.
Degree in math would work: $\cos(90\si{\degree}) = 0$.
\end{document}

enter image description here

As noted by cgnieder, \ang can also be used for this purpose:

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{textcomp}
\usepackage{siunitx}
\begin{document}
Degree symbol in plain text works: \ang{90} is a right angle.
Degree in math would work: $\cos(\ang{90}) = 0$.
\end{document}

I use ^{\circ} in math mode to get the degree symbol. It's worked every time. I have run into compatibility issues with some other code/package combinations.