From the original SynthMaker forum (2004–2014), with its members’ names and dates.
Navij11 ·
Hey, I'm not a very advanced user, but at school today I created some algorithms for filters on my graphing calculator. What I want to know, is how to get an absolute value function. I'm aware that its not native, and if I was using Java, it'd probably be something like:
if ( x > 0) {
x = 0 - x
}
However, I'm not familiar with the "if" statement method in Synthmaker. Does anyone know how to do this? Thanks!
- Nav
MyCo ·
The fastest way to get an absolut value is to remove the sign bit of a floats binary representation. Here it is:
Which Abs is faster?
Andrew J ·
Hi Nav,
In code, you need to use a boolean mask as follows:
x = x * ( 1 - (x<0)&2 )
If x<0 then (x<0)&2 evaluates to 2, otherwise it evaluates to zero.
Alternatively you could use the following (though this is slightly slower):
x = (x>0)&x - (x<0)&x
If speed is your thing, then there's a trick in Assembler which is much faster:
BTW, you got your Java example around the wrong way, should be if (x<0)!
Andrew J ·
Oops too slow - MyCo beat me to it.
Navij11 ·
Andrew J wrote: streamin in;
streamout out;
int absmask=2147483647;
movaps xmm0,in;
andps xmm0,absmask;
movaps out,xmm0;
Ah, thanks for that! This will probably need speed, so this will be very useful! 🙂
BTW, you got your Java example around the wrong way, should be if (x<0)!
Yeah, just noticed that, haha. Thanks for the correction, but its not as if I code in Java anymore. I just use TI Basic now, since that's what I know how to use.
Thanks for the help all!
Wrote something here? To have a post removed or your name hidden, write to [email protected].