Using abs and sign in code

Hi guys

Im gonna try a bit of multipication and division on samples so Im gonna need to use the abs/sign trick. Ie only done that in JSFX and ahve no idea how do implement it in iPlug2-

So is a there any bit of code I can check to see how its supposed to look? Tips appreciated :slight_smile:

Sincerely /Bo

what is the abs sign trick?

Hi Oli

Its the process of making all sample values positive and then do the math. Then revert back the negative values via the sig/sign. In JSFX it would look like this:

s0 = abs(spl0);
//Do math
spl0 = s0 * sign(spl0);

Also. How does iPlug2 handle divide by zero? Any best practice?

Sincerely /Bo

These are more C++ questions than iPlug2. the C++ standard library has std::abs() and std::copysign() which I think are the equivalents

Hi

I see. Thanks for clarifying :slight_smile:

Sincerely /Bo

My practice is simple:

int sign;
if (inputs[c][s] < 0.) sign = -1;
else sign = 1;
outputs[c][s] = inputs[c][s] * sign;
// Do the Math …
outputs[c][s] *= sign;

and slightly faster than:

outputs[c][s] = std::abs(inputs[c][s]);
// Do the Math …
outputs[c][s] = std::copysign(outputs[c][s] , inputs[c][s]);

:sunglasses:

1 Like

Thanks John

Thats exactly what I what looking for :slight_smile:

Sincerely /Bo

1 Like