Archive: sign(var) function


5th December 2002 16:04 UTC

sign(var) function
Is there any way to define sign(var) function using only mathematical expressions like +, -, *, /, ^ ?
Something like abs(var)=sqrt(sqr(var)). Yes - I know this isnt really correct.


5th December 2002 17:52 UTC

I don't really see why a definition using +-*/^ would be better than another definition.

Logically it's defined as:
x>0: f(x) = 1
x<0: f(x) = -1
x=0: f(x) = 0

First of all, I don't see what's incorrect about abs(x) = sqrt(sqr(x)). It fits all real numbers.

You could define sign() as the derivative of abs(), in which case:

abs(x) = sqrt(sqr(x))

D(abs(x)) = (1/(2*sqrt(sqr(x)))) * (2*x)
simplifying:
D(abs(x)) = x/sqrt(sqr(x)) = x/abs(x)


If you want to know how AVS 'calculates' it, it probably just checks the sign bit of the floating point number, like this (for a 32-bit float):

return((*(int*)(float*)&number)&0x80000000)?1.0f:-1.0f;

Doing an actual division would take ages. Note that this would have 0 return + or -, because there are two internal representations of 0 in an IEEE float: +0.0 and -0.0.

5th December 2002 18:47 UTC

Thanx :)
I need to solve a recursive function for fps reasons - its faster to calculate F(n)=phi^n/sqrt(5)and round it up than to count 1500th Fibonaccy number. Only this function uses if(),below(),max() and stuff like that so I needed sign to define them.
sqrt(sqr(x)) isnt really correct becouse of the negative root.


5th December 2002 20:53 UTC

I understand how you might need it, but:

sqrt(sqr(x))

Always exists... sqr makes 'x' positive regardless of its sign, so it always has a real sqrt.