Templated a lot of maths functions to avoid implicit type conversions.