1# sqrt.xml - square root for numbers and fpn arrays with high precision (9 digits).
2# arcsin.xml - inverse sine
#3 arccos.xml - inverse cosine
Remark on inverse trigonometric functions:
In order to provide a moderate time consumption of the algorithm the precision is about 5 digit for input near +/-0.5. However, the precision increases exponenially towards input +/-1 to more than 9 digits.
As usual: any feedback, suggestions, bug reports etc. are welcome
Download link moved up:
New download link
You also may want to check this post by dz_duck. He wrote some nice fpn functions which, I'm sure, you will find usefull.
Version 1.0 uploaded !
Some bugs fixed and 2 main scripts and a subscript added.
As you know there are sine and cosine function available in the SE but they take integer and return integers.
The new floating point main scripts sine.xml and cosine.xml are exactly as the math functions. They are 2pi-periodic over the interval [-1,1]. Also they provider higher precision.
Here an example of placing 21 sats along the sine between 0 and 2pi.
Any feedback is more than welcome.
Hotfix uploaded (please overwrite all old files).
Revised and tested all scripts to avoid the array reference problem. As a bonus added a new script 'modulo.xml'. It's like 'mod' but takes also fpn as arguments.
Every feedback is welcome. Please, bug reps. here or via email etc. .
Bug found, see here for details. I'm on it .......
(Update - original post moved down)
Here my first attempt to code a FLOATING POINT MATH LIBRARY VER.0 (16-11-06) for the x3 Script Editor. Feel free to use them in your scripts.
link moved up.E.
As you may know already the number representation of the SE is restricted. Elementary operations like addition, multiplication and division are difficult to code because the SE can handle only integers within a certain range (for example the greates SE number is 2^(31)-1 = 2147483647) and has also problems with rounding.
The SE is reliable within a precision of 9 digits which allow numbers between 0 and 999,999,999 plus, of course, a possible minus sign. So it is possible to represent a floating point number by an array (s,m,e) of size 3, where s is the sign, m is a natural number of max 9 digits and e is the exponent.
The main scripts are add.xml, prod.xml and divide.xml and they do exactly what their names indicate.
Each has 2 parameters as input which can be either integers or the fpn (s,m,e) of a number (see below). The output is an fpn. Note that all these 3 main scripts convert integers to fpn.
In some cases one needs the inverse: integer of an fpn. It is risky because of a possible over or under flow and be cause of truncation errors. The script float2int.xml returns the number 0 for underfow and null for overflow, and in all other cases the integer of the fpn.
Below an example: a simple modification of part of my complexProductionCost script (delete all fpn script calls and uncomment each line to get the original script) to apply the new fpn computation.
Code: Select all
083 if $ware == Energy Cells 084 $price = $complex -> get price of ware Crystals 085 @ $fpn = [THIS] -> call script 'divide' : integer or floating point array=$price integer or floating point array=138 086 @ $pcost = [THIS] -> call script 'float2int' : fpn array (s,m,e)=$fpn 087 *$pcost = $price / 138 088 else 089 @ $prod = [THIS] -> call script 'ware2relval' : ware=$ware 090 $k = 0 091 $pcost = 0 092 while $k < $nsr 093 $res = $stationresources[$k] 094 @ $ares = [THIS] -> call script 'ware2relval' : ware=$res 095 $price = $complex -> get price of ware $res 098 @ $fpn = [THIS] -> call script 'prod' : number 1=$prod number 2=$price 099 @ = wait 1 ms 100 @ $fpn = [THIS] -> call script 'divide' : integer or floating point array=$fpn integer or floating point array=$ares 101 @ = wait 1 ms 102 @ $fpcost = [THIS] -> call script 'float2int' : fpn array (s,m,e)=$fpn 103 $pcost = $pcost + $fpcost 104 *$dum = $prod * $price 105 *$pcost = $pcost + $dum / $ares 106 $k = $k + 1 107 end 108 end
fpn: is an array (s,m,e) of size 3 with s=+/-1 the sign, m natural number and e the exponent.
Example: The number -1/20 = -0.04 = -4E-2 has the fps rep (-1,4,-2)
pnum: is an array(n,e) of size 2 with integer 0<n and exponent 0<e. The corresponding integer n*10^e may not exist (overflow). Hence the notion pnum (pseudo-number).
Example: (12345678,5) = 1234567800000
nstr: is a string of a number
Example: n= 12345000 and nstr='12345000'
revstr: reverse nstr
Example: n= 12345000 has revstr = '00054321'
Function: sum of two numbers
Input: 2 integer or fpn
subscripts: int2float, pnum2revstr,addstrings,revstr2pnum, normalize.
Function: Product of two numbers
Input: 2 integer or fpn
Subscripts: int2float, pnum2revstr, prodstrings, revstr2pnum, normalize
Function: Divison of first number by second
Input: 2 integer or fpn
Subscripts: int2float, invert, prod, gcd
Funtion: maps fpn to integer
Output: Integer, NULL if overflow
Subscripts: pnum2revstr, evalpnum, revstr
I'm wondering if a floating point math library for the SE does already exist, somebody working on it or whether it would be useful at all?
Every feedback is appreciated.
Edit: basic operations have been coded; what else should I include?