Difference between revisions of "ESM Math Library"

3,319 bytes added ,  14:02, 17 April 2013
no edit summary
imported>JustTim
m
imported>JustTim
 
(3 intermediate revisions by 2 users not shown)
Line 6: Line 6:


=== The easy way: Math Library ESM ===
=== The easy way: Math Library ESM ===
The easiest way to use the functions shown in this article is by using [http://www.megamods.de/_data/parts/dl.php?file=68 this ESM Math Library]. To use it copy the ESM file to your oblivion data folder and select it as an additional master file when loading your mod with the Construction Set. By doing this all the necessary setup steps are already done for you. All you need to do to use a function is to call it. Examples of how to call each function are shown below in the "Usage" blocks of each stage function.
The easiest way to use the functions shown in this article is by using [http://www.hazardx.com/details.php?file=68 this ESM Math Library]. To use it copy the ESM file to your oblivion data folder and select it as an additional master file when loading your mod with the Construction Set. By doing this all the necessary setup steps are already done for you. All you need to do to use a function is to call it. Examples of how to call each function are shown below in the "Usage" blocks of each stage function.
Be aware that other users of your mod will need the Library too to run it.
Be aware that other users of your mod will need the Library too to run it.


Download: [http://www.megamods.de/_data/parts/dl.php?file=68 ESM Math Library v1.0].  
Download: [http://www.hazardx.com/details.php?file=68 ESM Math Library v1.0].  


=== Do it yourself: Step by Step ===
=== Do it yourself: Step by Step ===
Line 106: Line 106:
float tan
float tan
;float ang
;float ang
;S40 FUNCTION NaturalLogarithm
short LN_n
;float t1
;float t2
;float t3
;float t5
;float t7
;float t9
;float t11
float t13
;S50 FUNCTION Exponentiation
float base
float exponent
;float t1
;float t2
;float t3
float t4
;float t5
;float t6
;float t7
float t8
;float t9
float t10
;float t11


; Set constants first
; Set constants first
Line 548: Line 574:
set myResultAngle to f.fout</pre>
set myResultAngle to f.fout</pre>


=== Stage 40: Natural Logarithm ===
Takes an antilogarithm a (float) and returns the exponent b (float) to the base e(=2.71828...):<br>
a=e^b --> b=LN(a)<br>
<br>
Serial approximation is described [http://www.convertit.com/Go/EducationPlanet/Reference/AMS55.ASP?Res=150&Page=68 here].<br>
Code to include in FunctionQuestScript:
<pre>;S40 FUNCTION NaturalLogarithm
short LN_n
float t1
float t2
float t3
float t5
float t7
float t9
float t11
float t13</pre>
Code for the Stage Result Script:
<pre>;FUNCTION float LN(float input)
if f.LN_n == 0
set f.LN_n to 1
endif
if f.fin1 <= 0
set f.fout to 0
set f.LN_n to 0
elseif f.fin1 > 4 || f.fin1 < 0.25
;change values for better (slower processing) or worse accuracy (faster processing)
;this configuration should provide an accuracy of at least 99.99%
;LN(x) = n * LN( nthRoot(x) )
set f.LN_n to (f.LN_n * 2)
setStage f 5
set f.fin1 to f.fout
setStage f 40
else
set f.t1 to ((f.fin1 - 1) / (f.fin1 + 1))
set f.t2 to (f.t1*f.t1)
set f.t3 to (f.t1*f.t2)
set f.t5 to (f.t3*f.t2)
set f.t7 to (f.t5*f.t2)
set f.t9 to (f.t7*f.t2)
set f.t11 to (f.t9*f.t2)
set f.t13 to (f.t11*f.t2)
set f.fout to (2 * f.LN_n * (f.t1 + f.t3/3 + f.t5/5 + f.t7/7 + f.t9/9 + f.t11/11 + f.t13/13))
set f.LN_n to 0
endif</pre>
Actually, there are two ways to increase accuracy. You can either use more terms or change the values above (nearer 1 increases accuracy, farther 1 decreases it).<br>
Keep in mind, that this is only useful in the relation: firstValue = 1 / secondValue
Usage in another script:
<pre>;CALL float NaturalLogarithm(float x)
set f.fin1 to xVar
setStage f 40
set exponent to f.fout</pre>
=== Stage 50: Exponentiation ===
Takes a base x (positive) and an exponent y and returns a=x^y (all floats).<br>
Script uses Taylor-Series for exponential functions from [http://www.convertit.com/Go/EducationPlanet/Reference/AMS55.ASP?Res=150&Page=69 here].
Code to include in FunctionQuestScript:
<pre>;S50 FUNCTION Exponentiation
float base
float exponent
float t1
float t2
float t3
float t4
float t5
float t6
float t7
float t8
float t9
float t10
float t11</pre>
Code for the Stage Result Script:
<pre>;FUNCTION float Exponentiation(float base, float exponent)
set f.base to f.fin1
set f.exponent to f.fin2
setStage f 40
;x^y = e^(LN(x) * y) , e=2.71828...
set f.t1 to (f.fout*f.exponent)
set f.t2 to (f.t1*f.t1)
set f.t3 to (f.t2*f.t1)
set f.t4 to (f.t3*f.t1)
set f.t5 to (f.t4*f.t1)
set f.t6 to (f.t5*f.t1)
set f.t7 to (f.t6*f.t1)
set f.t8 to (f.t7*f.t1)
set f.t9 to (f.t8*f.t1)
set f.t10 to (f.t9*f.t1)
set f.t11 to (f.t10*f.t1)
set f.fout to (1 + f.t1 + f.t2/2 + f.t3/6 + f.t4/24 + f.t5/120 + f.t6/720 + f.t7/5040 + f.t8/40320 + f.t9/362880 + f.t10/3628800 + f.t11/39916800) ;attend to write this in one line</pre>
Actually, more terms won't increase accuracy any more, because Oblivion can't handle higher values.
Usage in another script:
<pre>;CALL float Exponentiation(float base, float exponent)
set f.fin1 to base
set f.fin2 to exponent
setStage f 50
set result to f.fout</pre>
[[Category: Useful Code]]
[[Category: Useful Code]]
Anonymous user