I think Scala's math library may have a function to replace this; leaving this here as a note for myself to look into later.