Posted: Sat Mar 21, 2009 8:35 pm Post subject: Some math improvements for ACS

Hello there, how are you doing?

This may sound useless for many, but if you are smart and like to invent creative stuff with ACS, it may be very useful IMO.
As ACS doesn't like math very much by default, I am developing my own formulaes. I'll be posting 'em here even for organization reasons (I am afraid of loosing/corrupting them somehow)
Thanks to bagheadspidey for helping me optimizing some codes.

Exponential:

Code:

function int FExp(int base, int exp)
{
int result = 1.0;
int i;
int abs_exp = exp;
if (exp < 0) abs_exp *= -1;

Usage: FExp(Your floating value,Your INTEGER exponent) (e.g.: FExp(3.0,5))
Works with negative exponents.

Square Root:

Code:

function int FixedSqrt(int number)
{
int samples=15; //Samples for accuracy

if (number==1.0) return 1.0;
if (number<=0) return 0;

int val=FixedMul(samples<<16,10.0);

for (int i=0; i<samples; i++)
val=FixedDiv(val+FixedDiv(number,val),2.0);

return val;
}

Usage: use it as a function writing FixedSqrt(your floating number).
Negative values results 0.

Keep in mind that it's not a perfect solution. The smaller the number is, the higher the percentage of error is.
Feel free to change the number of samples by editing the samples string. 15 samples are great in most cases.

Sine, cosine and tangent:
ACS has a built-in sine and cosine system. But tangent is missing, and the values that you put on them must be in a different scale than radian values. 1.0 value means a full circle instead of 2*pi. If you want to make interactive math equations based on sine, cosine or tangent values in radian - which is vastly used - try this conversion:

Code:

int pi=3.14159;
function int FCos(int fcosnum)
{
int fcosresult=cos(FixedMul(fcosnum,FixedDiv(1.0,FixedMul(2.0,pi))));
return fcosresult;
}

Usage: FCos(your floating number in radians).

Now, it's Sine turn:

Code:

int pi=3.14159;
function int FSin(int fsinnum)
{
int fsinresult=sin(FixedMul(fsinnum,FixedDiv(1.0,FixedMul(2.0,pi))));
return fsinresult;
}

Usage: FSin(your floating number in radians).

I recommend not to change the pi value.

What about tangent?
That's quite simple. tangent(x) = sin(x)/cos(x). Then:

Code:

int pi=3.14159;
function int FTan(int ftannum)
{
int ftanresult=FixedDiv(sin(FixedMul(ftannum,FixedDiv(1.0,FixedMul(2.0,pi)))),
cos(FixedMul(ftannum,FixedDiv(1.0,FixedMul(2.0,pi)))));
return ftanresult;
}

Usage: FTan(your floating number in radians)

Arc sine, arc cosine and arc tangent (NOT DONE YET):
This one has been a pain in my butt since nobody teached inverse trigonometry to me yet at school. But I think I got some average results.

Here's the arc sine formulae:

Code:

function int FASin(int number)
{
int result=0;
int samples=5; //Samples for accuracy (over 5 samples usually results wrongly)
if(number!=0 && number<1.0 && number>-1.0 && samples>0)
{
int div_on=1.0;
int div_un=1.0;
int div_val=1.0;
int loopresult;
for(int i=0; i<samples; i++)
{
div_on=FixedMul(div_on,div_val);
div_un=FixedMul(div_un,div_val+1.0);
div_val=div_val+2.0;

Usage: FASin(your floating number)
Although there are 5 samples in this algorithm, the margin of error is considerably big if the input value is close to 1.0.
This problem would be solved if the samples were increased, but unfortunely the values would become completly wrong when processed in ACS because of the depth of the numbers.

Now the acos. As arc cosine of x represents pi/2-asin(x) then:

also looking at the 'other useful functions' -page on zdoom wiki, I can find a lot better examples of square root: http://zdoom.org/wiki/Sqrt <- there's one that uses just 3 lines, compared to your long formula

also I'm not conviced of the use of various constant values in your formulas, that can only result in hacky and inaccurate results To be honest, now looking at some of the formulas..

also looking at the 'other useful functions' -page on zdoom wiki, I can find a lot better examples of square root: http://zdoom.org/wiki/Sqrt <- there's one that uses just 3 lines, compared to your long formula

also I'm not conviced of the use of various constant values in your formulas, that can only result in hacky and inaccurate results To be honest, now looking at some of the formulas..

Code:

YOURINVERSECOSVALUE

how the hell did you come up with this?

I took reference from wikipedia mainly. The big deal is that the number can be only 8 chars long in ACS (if I'm not mistaking), so the results can't be more accurate

Anyway, thanks for the links from zdoom wiki. I'll take a look.

Ronald wrote:

Oh my God, you remind me of my math teacher! Looks like you'll be given an A at your next test.

About the usefulness of this, I'm not really convinced. What kind of creative stuff do you think you can invent with these formulaes?

I don't know really. All I can say is that using some math improvements makes the doom gameplay closer to the latest games.
Let me take an example: GTA-VC. It's pretty old, but it has daylight system, some physics and so on. I'm not sure how it works, but I guess they didn't use only additions and subtractions for this.
It depends on creactivity either.

@BestOfTheWorst
Thank you pal! I didn't know about these custom functions. Now it's way more simple to use these formulas.
Fixed the square root written in the first post. I'll be updating the rest later.

BTW, I couldn't get what's wrong with this square root solution at zdoom wiki page. When I tried the sqrt of 8, it returned to me the 2 value. Looks like it doesn't work with floating numbers.

BTW, I couldn't get what's wrong with this square root solution at zdoom wiki page. When I tried the sqrt of 8, it returned to me the 2 value. Looks like it doesn't work with floating numbers.

I see.. ..maybe it would be better off for you to post yours in the zdoom wiki instead then

You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum