Tes3Mod:Math Functions

Overview
Morrowind comes with a very limited math library. By limited, we mean square root (GetSquareRoot) and distance (GetDistance) from one object to another (with some substantial limitations). Occasionally, there's a need for something more. 

For anyone writing a script in a mod using MWSE, the basic trig functions are built in. So xArcTan is a simple extended function call. Degree/Radian conversions, Distance calculations, and the 6 basic trig functions. No power function though. 

Ghan Buri Ghan's Scripting for Dummies mentions Soralis' Math Mod, available at Planet Elder Scrolls: Math Mod. This contains the following functions:
SquareRoot
Sine
Cosine
Tangent
NaturalLog
Log
intPower
intRoot
Modulus
Antiln
Antilog
AbsoluteValue
Power

The math mod can either be included with your mod or you can use the scripts as a basis for your own.

LN by Taylor Expansion
LN can be expressed in a series similar to the SIN example, though it is slightly more complex:

 

You'd likely have to use more values than in the SIN case to get a similar accuracy (though I haven't checked). There are similar expansions for any trig/math function.

Modulo
Modulo gives you the remainder of division operation. 

 

Sin/Cos Approximation
Here's a very simple example script which assumes the existence of 4 global floats: SinIN, SinOUT, CosIN, CosOUT. All the script does is take the sin of what's in SinIN and set SinOUT to the result, then take cos of what's in CosIN and set CosOUT to the result of that. It also prints out the results in a message box

 begin SinScript

;Global float SinIN
;Global float SinOUT
;Global float CosIN
;Global float CosOUT

float IN
float OUT

short DEBUG

set IN to SinIN
;==================================================
;                                     Simple Sin
;                  By: Matt (Simpleton) Edlefsen
;               Remember to declare IN and OUT!!
;==================================================
while ( IN > 270 )
    set IN to IN - 360
endwhile
while ( IN   90 )
    set IN to 180 - IN
endif
set IN to IN * 3.14159265 / 180
set OUT to IN  * IN
set OUT to IN *  ( ( -0.16605 * OUT) + ( 0.00761 * OUT * OUT) + 1 )
;==================================================
if ( DEBUG == 1 )
    if ( OUT != SinOUT)
         MessageBox, "Sin(%.4f) = %.4f", SinIN, OUT
    endif
endif
set SinOUT to OUT

set IN to CosIN
; subtracting IN from 90 right before the sin will make it do cos instead
set IN to 90 - IN
;==================================================
;                                    Simple Sin
;                  By: Matt (Simpleton) Edlefsen
;              Remember to declare IN and OUT!!
;==================================================
while ( IN > 270 )
    set IN to IN - 360
endwhile
while ( IN  

If you want to test it out, just make sure to have the 4 globals declared and add the script to the start scripts

Sin by Taylor Expansion
Sin/Cosine can be easily computed to a reasonable degree of accuracy using Taylor series (I did this really fast so ignore any obvious errors):

 

This will give you a sin of any angle 0-360deg to at least 10 digits of accuracy. You can remove some of the end terms if you need less (really 3-4 digits is fine for most things you'd do in Morrowind anyways which means you can remove 3-4 terms). 

Cos, Tan, ArcCin, SinH, exp, log etc... can be computed in a similar manner.