superellipsoid: problem in understanding parametric formula

hi i've found this interesting page: superellipse and superellipsoid and i used the formula for one of my computer graphics applications. i used (the most usefull for computer graphics) the parametric formula:

but for correclty draw the superellipsod i had to calculate also some absolute values of sine and cosine and deal with signs like in this piece of code (it's ANSI C code, i hope it make sense for you):

void EvalSuperEllipse(double t1,double t2,double p1,double p2,XYZ *p)
{
double tmp;
double ct1,ct2,st1,st2;

ct1 = cos(t1);
ct2 = cos(t2);
st1 = sin(t1);
st2 = sin(t2);

// SIGN function return 1 if input is positive, -1 if it is negative
// fabs function calculate absolute value

tmp  = SIGN(ct1) * pow(fabs(ct1),n1);
p->x = tmp * SIGN(ct2) * pow(fabs(ct2),n2);
p->y = SIGN(st1) * pow(fabs(st1),n1);
p->z = tmp * SIGN(st2) * pow(fabs(st2),n2);
}


the first question is how to write in pure math formalism the parametric formula including those sign change (obtained with SIGN and fabs function), and the second question is why i have to make this manipulations and how understand that i had to do that in new geometric adventure if i don't find the code ready-to-use.

i hope i do not make an off-topic with this programming question! (i know it's weird to post ANSI C code here but i think is more attinent here instad of in stackoverflow.com)

-

The equation for an axis-aligned ellipse is $$\left(\frac{x}{a}\right)^2 + \left(\frac{y}{b}\right)^2 = 1.$$ Because $x^2 = (-x)^2$, the ellipse is symmetric under $x \rightarrow -x$ and $y \rightarrow -y$ reflections. The superellipse is a generalization where the exponent is some $\alpha \neq 2$. However, $x^\alpha$ is not well-defined for negative $x$ and fractional $\alpha$; and for odd integer $\alpha$, $x^\alpha \neq (-x)^\alpha$, so the reflection symmetries are broken. One way to patch the symmetries is to use $|x|$ instead of $x$ in the generalized equation: $$\left|\frac{x}{a}\right|^\alpha + \left|\frac{y}{b}\right|^\alpha = 1.$$ Note that this still reduces to the ellipse for $\alpha = 2$. Similar considerations apply to the parametric representation of the ellipse: $$\left(x(\theta),y(\theta)\right) = (a\cos\theta, b\sin\theta)$$ is symmetric under $(x,y,\theta)\rightarrow(-x,y,\pi-\theta)$ and $(x,y,\theta)\rightarrow(x,-y,-\theta)$, and the expressions for $x$ and $y$ become ill-defined when the $\sin$ and $\cos$ terms are raised to the power of $2/\alpha$. To preserve the symmetries and still solve the defining equation, the correct generalization is $$\left(x(\theta),y(\theta)\right) = \left(a|\cos\theta|^{2/\alpha} \text{sgn}(\cos\theta), b|\sin\theta|^{2/\alpha} \text{sgn}(\sin\theta)\right).$$ The motivation for the definition of the superellipsoid is completely analogous, and the same type of corrections need to be made to the parametric representation of its surface. Specifically, its definition is $$\left|\frac{x}{a}\right|^\alpha + \left|\frac{y}{b}\right|^\alpha + \left|\frac{z}{c}\right|^\beta = 1,$$ and its surface can be parametrized as $$\begin{eqnarray} x(\theta,\phi) &=& a \left(\cos\theta\right)^{2/\beta} |\cos\phi|^{2/\alpha} \text{sgn}(\cos\phi) \\ y(\theta,\phi) &=& b \left(\cos\theta\right)^{2/\beta} |\sin\phi|^{2/\alpha} \text{sgn}(\sin\phi) \\ z(\theta,\phi) &=& c |\sin\theta|^{2/\beta} \text{sgn}(\sin\theta) \end{eqnarray}$$ for $-\pi/2 \le \theta \le \pi/2$ and $0 \le \phi < 2\pi$.
You are right that in $\mathbb{R}$ non-integral powers are not defined for negative bases. The easiest way is to restrict the angles $\beta$ and $\phi$ to $[0,\pi/2]$, then recognize that you need all eight combinations of plus and minus signs for $x ,y$ and $z$. This cuts the trig function calls by a factor eight.