It's already discussed here that hypot
is slower than sqrt
because it handles cases when the two inputs are huge or tiny so that sqrt
would wrongly return inf or 0.
However, according to some test, hypot
is even slower than first convert to higher precision and then do trivial calculation and convert back.
So, in what case should I use hypot
? Only when there's no larger float type and overflow is an issue?
EDIT: sqrtl
and hypot
differ on input a=1,b=1.8e-8
, but sqrtl
returns more accurate result:
hypot 1.0
sqrtl 1.000000000000000222044604925031
exact 1.000000000000000161999999999999986878000000000002125764000...
1+eps 1.0000000000000002220446049250313080847263336181640625
Exact lines where an eps is added to result is shown here