The C++ analog of norm in C I noticed that the C++ version of &lt;complex&gt; has norm.

However, I am programming in C, and there is no cnorm.  So I made one to define before my main function:

#include &lt;complex.h&gt;

double cnorm(double _Complex z)
{
    return cpow(creal(z),2) + cpow(cimag(z),2);
}


How would you improve this definition? Is the name ok?  or will it break when it is eventually added to &lt;complex.h&gt;.  Is the return type appropriate?  Would it be more consistent to use double _Complex?  And is the implementation efficient (for rapid numerical evaluation)?
I most cases the norm for complex numbers means the magnitude/absolute value, so if you want Gauss' definition you might think about a different name.

If you want it really fast and and with an error of at most one ulp it gets quite complicated. You might download a version of Cygwin's new-libc (e.g. from the git here: https://github.com/mirror/newlib-cygwin) and take a look at newlib/libm/math/e_hypot.c to get the gist of it.

What you have now is quite slow and imprecise, for two reasons:


the return of creal()/cimag() is a double, no need for the complex function cpow().
you have two squarings and one addition; all three together have a good chance of unnecessarily adding error.


There is the function cabs() which computes the Euclidean norm sqrt(a²+b²). I would propose to use that function and simply square the outcome if you do not want to go the long and complicated way. If cabs() is implemented well (error less than 1 ulp) you should get a good error value for your own function, too.