0

I created a programming language for fun and now I want to write some basic math functions. The limitation I have is that the language only supports the integer data type. So I want to calculate 100 * sin(x). I've already managed to do the same thing with sqrt but I am stuck here. I've tried using the method described here: https://stackoverflow.com/a/2284929/6291515

And also a fast algorithm as described here: http://lab.polygonal.de/2007/07/18/fast-and-accurate-sinecosine-approximation/

I first tried modelling the algorithm in java (since it's easier to handle and debug) but it doesn't quite work. Maybe you have some idea on how to make it work.

int mult = 100;
int pi = (int)(Math.PI * (double) mult);
int x = (int)( 5 * (double) mult );
x = x % (2*pi);
int res=0, pow=x, fact=1, buf1 = pow;
for(int i=0; i<10; i++){
  buf1 = pow;
  buf1 /= fact;
  res+=buf1;
  pow*=x;
  pow /= mult;
  pow*=x;
  pow /= mult;
  pow*=-1;
  fact*=(2*(i+1))*(2*(i+1)+1);
}
BBotMerlin
  • 230
  • 1
  • 3
  • 12
  • 1
    *...but it doesn't quite work*. What do you mean by that? – lurker Feb 25 '18 at 11:43
  • The results are seemingly arbitrary numbers – BBotMerlin Feb 25 '18 at 12:54
  • No time to write a good answer right now (maybe later), but have a look at https://en.wikipedia.org/wiki/CORDIC . Also, your scaling of **x** looks very strange -- maybe you want to use degrees? At least there are an even number of degrees in a circle. – Matt Timmermans Feb 25 '18 at 12:55
  • I tried to wrap the radian value to -pi; +pi but I think I'll go for the easier 0; 2pi because it doesn't really work – BBotMerlin Feb 25 '18 at 13:03
  • Perhaps by the time you round Pi to only a couple of decimal places and keep rounding other intermediate results to 2 and rescaling, some numeric entropy may be setting in? An `int` isn't a very big number to store these power series terms when you're scaled by `mult`, and constantly trying to "unscale" each pass is going to cause inaccuracies. What's the purpose of the casts in `int x = (int)( 5 * (double) mult );`? – lurker Feb 25 '18 at 18:55

0 Answers0