Skip to content
Forum Archive

Tips&Tricks in AVS

274 posts

Raz#
Incidentally, which would be quicker for calculating pi:
"acos(-1)"
or
"6167950454/1963319607" ?
dirkdeftly#
well, that depends.

are you idiot enough to be calculating it on a regular basis in any given preset?
Jaak#
Pi is calculated just once... i only hope that you do calculate it in init?
fastet way: pi=3.141592654...
shreyas_potnis#
Not necessarily, acos(-1) would return say about 6 decimal places (dont know exaclt), but you can always use a calculator and obtain that value and write pi=3.14159265.. instead of pi=acos(-1)
UnConeD#
Yeah and it wouldn't matter one bit :P. For drawing a (-1,1) circle in a 320x320 window:

r=i*pi*2;
x=cos(r);y=sin(r);

1 pixel is 2/320 units = 0.00625

So you only need a precision of 0.003125 (half due to rounding errors) on cos and sin to guarantee a correct result.

Error-propagation theory says something like:

relative error of f(x) = (error of x)*(df(x)/dx)

Because dcos(x)/dx = -sin(x), abs(df(x)/dx) is at most equal to 1. So the relative error of cos(x) will never exceed the relative error on x.
It means that you need at most a relative error of 0.003125 for 100% accuracy in AVS.

So something like 3.1416 should make no difference from anything more accurate, for drawing circles at least. If you're doing more complicated calculations of course, more accuracy is probably needed.
Raz#
Well if more accuracy is needed then would acos(-1) or 6167950454/1963319607 be the quickest? I know it doesn't matter much at all but i might as well have the quickest method of doing so.
mikm#
I don't see what speed has to do with it, if you put it into init, it will only calculate once, so speed won't matter.
sidd#
cmon.. unless avs is a big dum dum, acos(-1) would have to be recognised by the prog. Seeing as acos requires pi anyway.
But then.. after the tidbits that steve uncovered, i wouldnt be suprised if its not.
Raz#
Is there any way to mimick the "rand" function but so it can be syncronised between scopes? I've tried a few different test ways and can't seem to come up with anything.

edit: i only need it so it's the quivalent of "rand(3)" but be able to syncronise.
UnConeD#
Or use something based on getosc.

These days I use asin(sin(...)) where ... is a sum of getoscs. If you multiply the ... by a large number, you get a somewhat equal spread (if you don't need that, you can drop the asin too).
Raz#
Actually, i just need a syncronisable version of the rand(2) that would work in UnConeDs tweak of my changing tunnel. I've tried and it doesn't seem to work with it.
anubis2003#
You'll have to apply some translations to it to get it to the range 0-2, and then you'll have to round it.
Raz#
All that time messing with random number generators and the answer was right under my nose. The code had unecessary variables in it that were easily replicated. Without the rand function used.
shreyas_potnis#
synched rand(2) would be something like.

no=abs((getosc(something)+getosc(something))*bignumber)%3;
Tuggummi#
Why 2 getosc functions? 🙄

[edit]
Since it's rand(2) it's clear you want 0&1, so just rnd=above(getosc,0) will do, since getosc can be both negative or positive. It will be either 0 or 1
jheriko#
Good point tug.. and shreyas.. you method is rand(3) (prolly a typo).. rand(3) is either 0, 1 or 2 just like n%3. % is definately useful for this sort of thing in the general case though. (getosc(something)*bignumber)%n is probably the easiest way to generate a synchable rand(n). Sometimes a method like rnd=(largeprime*rnd+smallerprime)%n is more useful... like if you want to generate the same sequence of psuedorandom numbers, for example to create a starfield scope. You can use this per point and start with the same rnd every frame. Sometimes I use this method on beat except the rnd inside the statement is replaced with a var that increments every frame... this tends to give a pretty rectangular distribution.
Raz#
Amazing how my mind comes up with all this complex crap and then when it comes to one of the simplest things it just comes out a blank.
Tuggummi#
Like Jheriko didn't cover that part up already 🙄

fucking stupid... asswanker idiot... always groaning about shit...
dirkdeftly#
...well that was stupid of me...opened this up early afternoon and forgot about it...then came back...>_O

...anyway...🙄
sidd#edited
OOlrighty.. nice way of thnking about it is like this.

n%x = returns the reamainder in real terms. Or, if you prefer, its like "n minus the next lowest mulitple of x, including 0, or the current n if it is a multiple of x."

Eg.
x%1 = 0... as 1 divides perfectly into any integer.
1%2 = 1... it divides 0 times, with a remainder of 1.
45%13 = 6... 13 goes into 45, 3 times (39), with a remainder of 6.

using any form of x%a (such as x%3 in your example) will cycle between 0 and a-1, including all the integers in between (0,1,2,0,1,2,0...). The reason is easy to understand, just follow this.

0%3 = 0... it divides 0 times, and there is no remainder.
1%3 = 1... it divides 0 times, and there is a remainder of 1.
2%3 = 2... it divides 0 times, and there is a remainder of 2.
3%3 = 0... it divides 1 time, and there is no remainder.
...
40%3 = 1... it divides 13 times, and there is a remainder of 1.
41%3 = 2... it divides 13 times, and there is a remainder of 2.


make sense?
dirkdeftly#
sidd forgot to mention that % only returns...sensible results for positive numbers on both sides, and integers on the left side.
Raz#
And how might i go about making avs spew a zero, then onbeat switch to 1, then onbeat switch to 2, then onbeat switch to 1 again, then onbeat to zero, etc etc. Tried for a while again, no luck.