On Thu, 18 Jan 2001, Tancredi wrote:

> I'm looking for a math library for a simple apllication.
> How do you make sin and cos (calculator application?).
> Thanks!
> 
> 

I wrote an app which needed sin and cos values, and ended up getting bogged
down in FloatMgr calls and such, eventually deciding to chuck it in and use a
table. 

Obviously it takes more memory doing this than a calculation, but it's
certainly faster. Anyway, I wrote a little program to generate the tables,
which because it's only small is attached below. Right now it only generates
lists for sine & cosine -- tangential values are left as an exercise :)

Simon.

Note this program has very little in the way of error checking and is released
under the GPL. Change values of PI and number of decimal places to suit.


/* File : sohcahtoa.c


   $Id$

   Simon Drabble        12/20/00
   [EMAIL PROTECTED]

   (C) 1999,2000  Simon Drabble
         
         Use: sohcahtoa <min> <max> 
         e.g. sohcahtoa 0 359
*/


#include <stdlib.h>
#include <stdio.h>
#include <math.h>

#define PI 3.14159
#define EPSILON (10e-3)
  

#define rad2deg(x) (double) ((180.0 * (double)(x) / PI))
#define deg2rad(x) (double) (((double) (x) * PI / 180.0))

int main(int argc, char *argv[])
{
        int i, min, max;
        FILE *fd;

        if (argc == 1) {
                min = 0;
                max = 359;
        } else if (argc == 2) {
                min = (int) strtol(argv[1], NULL, 10);
                max = 359;
        } else if (argc > 2) {
                min = (int) strtol(argv[1], NULL, 10);
                max = (int) strtol(argv[2], NULL, 10);
        }

        fd = fopen("sine_tables.h", "w");
        fprintf(fd, "float sin_tab[] = {\n");

        for (i = min; i <= max; ++i) {
                fprintf(fd, "%6.5f,  ", sin(deg2rad(i)));
                if ((i % 7) == 6 || (i % 7) == -6) fprintf(fd, "\n");
        }
        fprintf(fd, "\n};\n\n");
        fprintf(fd, "
#ifndef PI
#define PI 3.14159
#endif

#define rad2deg(x) (float) ((180.0 * (float)(x) / PI))
#define deg2rad(x) (float) (((float) (x) * PI / 180.0))

");

        fprintf(fd, "#define SIND(x) (sin_tab[(x) % 360])\n");
        fprintf(fd, "#define COSD(x) (sin_tab[(90 + (x)) % 360])\n");
        fprintf(fd, "\n");
        fclose(fd);

  return EXIT_SUCCESS;
}


-- 
For information on using the Palm Developer Forums, or to unsubscribe, please see 
http://www.palmos.com/dev/tech/support/forums/

Reply via email to