Systemnahe Programmierung in C: Funktionszeiger in Daten |
1#include <math.h>
2
3typedef double (*RealFct) (double x);
4
5static double sineTable[3141];
6
7static double lookupSineTable (double x);
8static double initSineTable (double x);
9
10RealFct sine = initSineTable;
11
12static double
13lookupSineTable (double x)
14{
15 return sineTable[(unsigned) (x * 1000)];
16}
17
18static double
19initSineTable (double x)
20{
21 unsigned i;
22
23 for (i = 0; i < 3141; ++i)
24 {
25 sineTable[i] = sin ((double) i / 1000.0);
26 }
27
28 sine = lookupSineTable;
29 return sine (x);
30}
31
32int
33main (void)
34{
35 double
36 r1 = sine (3.141), /* --> initSineTab */
37 r2 = sine (0.0), /* --> lookupSineTab */
38 r3 = sine (3.141 / 2.0);
39
40 return 0;
41}
|
Letzte Änderung: 11.01.2007 | © Prof. Dr. Uwe Schmidt |