AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Snippets and Tutorials (https://forums.alliedmods.net/forumdisplay.php?f=112)
-   -   [SNIPPET][TRASH]Calculating sine. (https://forums.alliedmods.net/showthread.php?t=226291)

Bimbo1 09-16-2013 01:11

[SNIPPET][TRASH]Calculating sine.
 
well, this is probably the worst snippet you will ever read. it serves for almost nothing...
the function i made calculates sine for any angle at the interval [0;Pi/2] with a precision of 10^(-3)[i guess...]. i know, it's a horrible precision and it's very limited, however it calculates sine 7% to 10% faster than the native function for calculating sine.
anyway, it's trash.
well, if you need to calculate 100 million sines in a row of angles in [0;pi/2] with low precision, this is the right snippet for you:
Code:

#include <sourcemod>

#pragma semicolon 1

#define C0 0.00023301105055311040507405363895867744838057969783202801
#define C1 0.99544724482913058418216562176926738156431820708931287424
#define C2 0.02093370138860544581496859818026477588188983853689570814
#define C3 0.20386398831094746691085039624037738708809526005357954191
#define C4 0.02864641864877972104358109297731732858321869940620898045

stock Float:Mat_Sine(Float:ang){

        return C0+C1*ang+C2*(ang*ang)-C3*(ang*ang*ang)+C4*(ang*ang*ang*ang);

}

i learnt that taylor series are used to calculate sine. but, then, i wondered, what if i used linear algebra to do it, would it be faster? yes, it would, since you do a function that only works at a very limited interval.
actually, if i had used the same subspace that the taylor series for calculating sine uses, i would for sure have got a polynom that approximates the sine function better than the series in the whole interval of the series, which i guess it's [0, 2pi] or [-pi, pi], although it would take much more time to run than the series. however, i got tired at P4 subspace, i would never be able to get to P7(i can't handle 8 dimensions, that's too much! maybe i could make the computer handle it for me... well, it's meaningless) subspace, which i think it's the one used by the series...
this is the first draft that i made:

the other one keeps the same logic, except by the fact that it has integrals with sine.

Zephyrus 09-16-2013 02:48

Re: [SNIPPET][TRASH]Calculating sine.
 
you could have just found the taylor numbers for sine on the internet, also to extend it to [0,Pi] you could have just added an if(pi>ang>pi/2), then decrease the angle by pi then multiply the result by -1, you could have just as well extended it to -inf;+inf easily

Bimbo1 09-16-2013 08:03

Re: [SNIPPET][TRASH]Calculating sine.
 
yep. the taylor numbers are easy to calculate, and that wouldn't be as fun as trying something new. and, actually, if pi/2<ang<pi, i could just calculate sin(pi-ang). i, actually, did it and expanded it to -infinite ane +infinite, but when i compared it to the native sine function, it was slower, thus there would be no point in using a function that is slower and less accurate...

Zephyrus 09-16-2013 09:34

Re: [SNIPPET][TRASH]Calculating sine.
 
btw im pretty sure that the built-in sine function uses taylor numbers too just higher precision.

Bimbo1 09-16-2013 10:22

Re: [SNIPPET][TRASH]Calculating sine.
 
no, i didn't use taylor numbers. the built in sine has higher precision because it probably goes to the seventh derivative, creating a polynom of p7(a+bx+cx^2+...+hx^7). i just reached p4(a+bx+cx^2+dx^3+ex^4).


All times are GMT -4. The time now is 18:30.

Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.