Java-Gaming.org Hi ! Featured games (91) games approved by the League of Dukes Games in Showcase (804) Games in Android Showcase (237) games submitted by our members Games in WIP (867) games currently in development
 News: Read the Java Gaming Resources, or peek at the official Java tutorials JavaGaming.org - Pastebin

Author: Roquen (posted 2016-10-17 12:07:20, viewed 378 times)

 1   2   3   4   5   6   7   8   9   10   11   12   13   14   15   16   17   18   19   20   21   22   23   24   25   26   27   28   29   30   31   32   33   34   35   36   37   38   39   40   41   42   43   44   45   46   47   48   49   50   51   52   53   54   55   56   57   58   59   60   61   62   63   64   65   66   67   68   69   70   71   72   73   74   75   76   77   78   79   80   81   82   83   84   85   86   87   88   89   90   91   92   93   94   95   96   97   98   99   100   101   102   103   104   105   106 ```// file must have unix line endings, otherwise will get: " cannot be recognized. Will ignore it. suppressmessage(346); // truncated power series: the coefficients get small fast. This is good way to guess how well a pure polynomial approximation will work out. As counter examples 1/x and sqrt(x) don't move much in magnitude power15 = x - (x^3/6) + (x^5/120) - (x^7/5040) + (x^9/362880) - (x^11/39916800) + (x^13/6227020800) - (x^15/1307674368000); // the truncated power series with coefficents rounded to single precision. [|24...|] is an infinite list: 24,24,24,24... (24 is the precision of singles including the implied bit) power15t = roundcoefficients(power15,[|24...|]); // polynomials are automatically in horner's form write("rounded power series: "); power15t; print(""); // the poly from: http://www.gamedev.net/topic/681723-faster-sin-and-cos/#entry5308906 // doesn't matter it's not in Horner's form. junk = 1*x -1.66666671633720397949e-01*x^3 + 8.33333376795053482056e-03*x^5 -1.98412497411482036114e-04*x^7 + 2.75565571428160183132e-06*x^9 -2.50368472620721149724e-08*x^11 + 1.58849267073435385100e-10*x^13-6.58925550841432672300e-13*x^15; // sanity check..insure constants are proper single precision junkt = roundcoefficients(junk, [|24...|]); write("unrounded gamedev poly: "); junk; print(""); write("rounded gamedev poly: "); junkt; // where the error function is zero, the approximation is correct. Get an approximate value write(" ~correct at: "); dirtyfindzeros(junkt-sin(x),[-pi/2;pi/2]); // estimate the maximum error on the range write(" ~max abs error: "); dirtyinfnorm(junkt-sin(x),[-pi/2;pi/2]); print(""); // make a 9-degree approximation. parameters: // 1) the function to approximate // 2) list of degrees. We have an odd function so: ?x + ?x^3 + ?x^5 + ?x^7 + ?x^9 + ... // 3) precision of each coefficient. [|24...|] says all 24 bits (double would be 53) // 4) input domain. Since odd [-pi/2,pi/2] is same as [0,pi/2]. I've biased away from zero since the method will // explode if f(x)=0 (and sin(0)=0). This slightly hurts the output quality but doing it one of the "right" // ways is more work and I can't be bothered about it. // 5) we're working with floating-point (not fixed point) // 6) minimize absolute error (other option is relative) sina = fpminimax(sin(x), [|1,3,5,7,9|], [|24...|], [2^-48;pi/2], floating, absolute); err = sina -sin(x); write("9th degree: "); sina; // if we have 'n' non-zero cofficients, we should be correct at (n-1) points on the domain write(" ~correct at: "); dirtyfindzeros(err, [0; pi/2]); // only show half the range since it's symmetric write(" ~max abs error: "); single(dirtyinfnorm(err, [-pi/2; pi/2])); write(" @x: "); dirtyfindzeros(diff(err), [0; pi/2]); // only show half the range since it's symmetric print(""); // RINSE-AND-REPEAT for higher degrees sina = fpminimax(sin(x), [|1,3,5,7,9,11|], [|24...|], [2^-48;pi/2], floating, absolute); err = sina -sin(x); write("11th degree: "); sina; write(" ~correct at: "); dirtyfindzeros(err, [0; pi/2]); write(" ~max abs error: "); single(dirtyinfnorm(err, [-pi/2; pi/2])); write(" @x: "); dirtyfindzeros(diff(err), [0; pi/2]); print(""); sina = fpminimax(sin(x), [|1,3,5,7,9,11,13|], [|24...|], [2^-48;pi/2], floating, absolute); err = sina -sin(x); write("13th degree: "); sina; write(" ~correct at: "); dirtyfindzeros(err, [0; pi/2]); write(" ~max abs error: "); single(dirtyinfnorm(err, [-pi/2; pi/2])); write(" @x: "); dirtyfindzeros(diff(err), [0; pi/2]); print(""); sina = fpminimax(sin(x), [|1,3,5,7,9,11,13,15|], [|24...|], [2^-48;pi/2], floating, absolute); err = sina -sin(x); write("15th degree: "); sina; write(" ~correct at: "); dirtyfindzeros(err, [0; pi/2]); write(" ~max abs error: "); single(dirtyinfnorm(err, [-pi/2; pi/2])); write(" @x: "); dirtyfindzeros(diff(err), [0; pi/2]); print(""); // *** cosine is pretty much the same // make an 8th degree approximation print("COSINE\n"); cosa = fpminimax(cos(x), [|0,2,4,6,8|], [|24...|], [0;pi/2], floating, absolute); err = cosa-cos(x); write("8th degree: "); cosa; write(" ~correct at: "); dirtyfindzeros(err, [0; pi/2]); write(" ~max abs error: "); single(dirtyinfnorm(err, [-pi/2; pi/2])); write(" @x: "); dirtyfindzeros(diff(err), [0; pi/2]); print(""); cosa = fpminimax(cos(x), [|0,2,4,6,8,10|], [|24...|], [0;pi/2], floating, absolute); err = cosa-cos(x); write("10th degree: "); cosa; write(" ~correct at: "); dirtyfindzeros(err, [0; pi/2]); write(" ~max abs error: "); single(dirtyinfnorm(err, [-pi/2; pi/2])); write(" @x: "); dirtyfindzeros(diff(err), [0; pi/2]); print(""); cosa = fpminimax(cos(x), [|0,2,4,6,8,10,12|], [|24...|], [0;pi/2], floating, absolute); err = cosa-cos(x); write("12th degree: "); cosa; write(" ~correct at: "); dirtyfindzeros(err, [0; pi/2]); write(" ~max abs error: "); single(dirtyinfnorm(err, [-pi/2; pi/2])); write(" @x: "); dirtyfindzeros(diff(err), [0; pi/2]); print(""); cosa = fpminimax(cos(x), [|0,2,4,6,8,10,12,14|], [|24...|], [0;pi/2], floating, absolute); err = cosa-cos(x); write("14th degree: "); cosa; write(" ~correct at: "); dirtyfindzeros(err, [0; pi/2]); write(" ~max abs error: "); single(dirtyinfnorm(err, [-pi/2; pi/2])); write(" @x: "); dirtyfindzeros(diff(err), [0; pi/2]); print("");```

Special syntax:
• To highlight a line (yellow background), prefix it with '@@'
• To indicate that a line should be removed (red background), prefix it with '-'
• To indicate that a line should be added (green background), prefix it with '+'
• To post multiple snippets, seperate them by '~~~~'
EOF
 LeoOno 10x princec 8x VaTTeRGeR 8x KaiHH 7x neoexpert 7x Guerra2442 7x NuclearPixels 6x SugarBlood 5x NahogaraDS 4x SteveSmith 4x CommanderKeith 2x Mr. Gol 2x Ranger 2x saocrinn 2x homac 2x ral0r2 2x 