Is there an easy way to keep the sign after squaring the value. I currently have:
float signed_square(float x) { if (x > 0) { return x * x; } else { return -(x * x); } }
As I said in the comments:
float signed_square(float x){ return x * fabs(x); }
C99 supplies double copysign(double x, double y)
double copysign(double x, double y)
Functions copysign produce a x signed and magnified value y .
copysign
x
y
float signed_square(float x) { return copysignf(x*x, x); }