Difference between revisions of "ESM Math Library"
Jump to navigation
Jump to search
Added stage 40 and 50 for natural logarithm and exponentiation
imported>Wrye m (Stage function repository moved to ESM Math Library: More accurate. This is really info on a specific package of stage functions.) |
imported>Diarrhoe (Added stage 40 and 50 for natural logarithm and exponentiation) |
||
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: NaturalLogarithm === | |||
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 or worse accuracy (makes the script faster) | |||
;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)</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]] |