Suggested improvement for math library...

The place to discuss scripting and game modifications for X²: The Threat.

Moderators: Scripting / Modding Moderators, Moderators for English X Forum

blackard
Posts: 7
Joined: Fri, 16. Apr 04, 01:13
x3tc

Suggested improvement for math library...

Post by blackard »

I was reading the notes on the math library - http://www.the-commander.com/x2tutorial3.htm. One thing I noticed that would impact the accuracy of the sine function as described has to do with loosing information at various points. I don't quite follow the determination of the quadrant - I think there some stuff missing - but see if this works for you...

First, if all angles are values 0..65535 (adding 1 to 65535 would return you to 0), and the Maclaurin approximation works for angles 0..pi/2 radians, then the quadrant should be determined and you have an angle value 0..16383. This takes 14 bits to store.

By doing some creative factoring and elimination (for example, not starting out by immediately multiplying the angle by 1000 to give yourself four digits of precision), you end up with the conversion to radians in fixed point with four digits of accuracy:

rad = ( theta * 19635 / 1024 + 1 ) / 20

I ran some comparisons using the following three formulas on values 0..16383:

floating point : rad = floor( 62832.0 * theta / 65536.0 + 0.5 )
original : rad = ( ( theta * 1000 ) / 65536 * 62832 + 50 ) / 1000
improved : rad = ( theta * 19635 / 1024 + 1 ) / 20

The original formula calculates rad with min error 0, max error 63 and mean error a little more than 31. The new formula calculates rad with min error 0, max error 1 and mean error just under 0.5.

I haven't analyzed the formulas for caluclating sine and cosine yet, but I suspect that similar improvements, but likely not as dramatic, can bet made.
stella
Posts: 1004
Joined: Tue, 17. Feb 04, 18:41
x2

Post by stella »

I have a nosebleed.
blackard
Posts: 7
Joined: Fri, 16. Apr 04, 01:13
x3tc

Post by blackard »

You're supposed to scratch your head... not pick your nose.

I did some work on sine, but not cosine yet. The following calculation has max error at pi/2, just like any other series expansion, but it gets 1.005 rather 1.7 - the correct value is 1.0:

sine theta = ( theta * 10000 - theta * theta / 2400 * theta / 25 + theta * theta / 2048 * theta / 15000 * theta / 15626 * theta / 250 + 50000 ) / 100000

This calulation of accurate to five significant digits at pi/4, and the error is increasing as theta approaches pi/2. Symmetry of sine and cosine can be taken advantage of knowing that for theta where pi/4 < theta <= pi/2:

sine theta = cosine ( pi/2 - theta )
cosine theta = sine ( pi/2 - theta )
User avatar
Burianek
Posts: 2981
Joined: Mon, 29. Dec 03, 03:29
x3tc

Post by Burianek »

I'd make sure you throw this in the jobs section of the devnet.
(If you don't know how to do that let me know, and I'll show you or do it for you.)
If I were a coder, I'd always appreciate helpful ways to increase accuracy and efficiency.
"Nature's first green is gold" . . . stay golden.
blackard
Posts: 7
Joined: Fri, 16. Apr 04, 01:13
x3tc

Post by blackard »

I'm not sure what that section even is so I would appreciate it if you could supply this to them, though this was really targeted at the mathlib scripts rather than the innards of X2.

In any event, a coding error (I used 120000 rather than 1200000 for the last devisor when I coded the original) changed the results so that the accuracy of the new code, while improved, is nowhere near as dramatic as I originally reported.

First, I finished with cosine:

cos theta = ( 10000 * 10000 - theta * theta / 2 + theta * theta / 2048 * theta / 15625 * theta / 75 - theta * theta / 2048 * theta / 15000 * theta / 25000 * theta / 1500 * theta / 625 + 50000 ) / 100000

Error analysis

using 0..pi/2

min max mean
cos orig 4.6E-9 6.4E-3 2.0E-3
new 4.6E-9 1.3E-3 2.9E-4
sin orig 4.2E-8 6.7E-3 2.2E-3
new 9.5E-9 5.0E-3 7.1E-4

using 0..pi/4

min max mean
cos orig 4.6E-9 4.8E-3 6.4E-4
new 4.6E-9 5.5E-4 1.2E-4
sin orig 4.2E-8 6.7E-3 1.4E-4
new 2.1E-8 5.9E-4 1.3E-4
User avatar
Burianek
Posts: 2981
Joined: Mon, 29. Dec 03, 03:29
x3tc

Post by Burianek »

Didn't realize this was an improvement to a user created library and not an egosoft one. As such, probably doesn't belong on ego's devnet Sorry for misleading, I got confused.
Why don't you pm reven with your suggestions?
"Nature's first green is gold" . . . stay golden.
blackard
Posts: 7
Joined: Fri, 16. Apr 04, 01:13
x3tc

Post by blackard »

'cause I was hoping for feedback, throughts, other suggestions from others, not just looking to show the original mahtlib developer that 'I can do it better than you can'.

Also, I'm not 100% sure how deep into scripting and modding I want to get. I've been developing software (and playing games) for a long time - over 22 years. I've never tried to develop a game, but the idea of being able to actually program a ship's computer (the kinds of scripts I'm thinking about rather than outright cheats) to perform automatable tasks in a game like x2 is intriguing.

There are some very interesting things in X2 - scripts, etc. - but there's plenty of things that disturb me - the flight model being one. I'm not complaining, I just haven't decided how much time I can devote to fiddling with this stuff.

The reason I posted this stuff, as I said, was to get comments and to make it available to anyone else that would be interested.

Return to “X²: The Threat - Scripts and Modding”