provide trigonometric functions with LUT.

Committer:
kb10uy
Date:
Mon Mar 23 11:20:09 2015 +0000
Revision:
1:825f5c2e80b4
Parent:
0:5472db659233
double/float????;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
kb10uy 1:825f5c2e80b4 1 #pragma once
kb10uy 1:825f5c2e80b4 2
kb10uy 1:825f5c2e80b4 3 #include "math.h"
kb10uy 1:825f5c2e80b4 4
kb10uy 1:825f5c2e80b4 5 //double precision version
kb10uy 1:825f5c2e80b4 6
kb10uy 1:825f5c2e80b4 7 class LUTTrigonometric
kb10uy 1:825f5c2e80b4 8 {
kb10uy 1:825f5c2e80b4 9 public:
kb10uy 1:825f5c2e80b4 10 LUTTrigonometric(int division = 256);
kb10uy 1:825f5c2e80b4 11
kb10uy 1:825f5c2e80b4 12 inline double sin(double x);
kb10uy 1:825f5c2e80b4 13 inline double cos(double x);
kb10uy 1:825f5c2e80b4 14 inline double tan(double x);
kb10uy 1:825f5c2e80b4 15
kb10uy 1:825f5c2e80b4 16 private:
kb10uy 1:825f5c2e80b4 17 double *table;
kb10uy 1:825f5c2e80b4 18 int div;
kb10uy 1:825f5c2e80b4 19 const double pi;
kb10uy 1:825f5c2e80b4 20 double rate;
kb10uy 1:825f5c2e80b4 21 };
kb10uy 1:825f5c2e80b4 22
kb10uy 1:825f5c2e80b4 23 double LUTTrigonometric::sin(double x) {
kb10uy 1:825f5c2e80b4 24 return table[(int)(x * rate) % div];
kb10uy 1:825f5c2e80b4 25 }
kb10uy 1:825f5c2e80b4 26
kb10uy 1:825f5c2e80b4 27
kb10uy 1:825f5c2e80b4 28 double LUTTrigonometric::cos(double x) {
kb10uy 1:825f5c2e80b4 29 return table[(int)((x + pi / 2.0) * rate) % div];
kb10uy 1:825f5c2e80b4 30 }
kb10uy 1:825f5c2e80b4 31
kb10uy 1:825f5c2e80b4 32 double LUTTrigonometric::tan(double x) {
kb10uy 1:825f5c2e80b4 33 return sin(x) / cos(x);
kb10uy 1:825f5c2e80b4 34 }
kb10uy 1:825f5c2e80b4 35
kb10uy 1:825f5c2e80b4 36 //single precision version
kb10uy 1:825f5c2e80b4 37
kb10uy 1:825f5c2e80b4 38 class LUTSingleTrigonometric
kb10uy 1:825f5c2e80b4 39 {
kb10uy 1:825f5c2e80b4 40 public:
kb10uy 1:825f5c2e80b4 41 LUTSingleTrigonometric(int division = 256);
kb10uy 1:825f5c2e80b4 42
kb10uy 1:825f5c2e80b4 43 inline float sin(float x);
kb10uy 1:825f5c2e80b4 44 inline float cos(float x);
kb10uy 1:825f5c2e80b4 45 inline float tan(float x);
kb10uy 1:825f5c2e80b4 46
kb10uy 1:825f5c2e80b4 47 private:
kb10uy 1:825f5c2e80b4 48 float *table;
kb10uy 1:825f5c2e80b4 49 int div;
kb10uy 1:825f5c2e80b4 50 const float pi;
kb10uy 1:825f5c2e80b4 51 float rate;
kb10uy 1:825f5c2e80b4 52 };
kb10uy 1:825f5c2e80b4 53
kb10uy 1:825f5c2e80b4 54 float LUTSingleTrigonometric::sin(float x) {
kb10uy 1:825f5c2e80b4 55 return table[(int)(x * rate) % div];
kb10uy 1:825f5c2e80b4 56 }
kb10uy 1:825f5c2e80b4 57
kb10uy 1:825f5c2e80b4 58
kb10uy 1:825f5c2e80b4 59 float LUTSingleTrigonometric::cos(float x) {
kb10uy 1:825f5c2e80b4 60 return table[(int)((x + pi / 2.0) * rate) % div];
kb10uy 1:825f5c2e80b4 61 }
kb10uy 1:825f5c2e80b4 62
kb10uy 1:825f5c2e80b4 63 float LUTSingleTrigonometric::tan(float x) {
kb10uy 1:825f5c2e80b4 64 return sin(x) / cos(x);
kb10uy 1:825f5c2e80b4 65 }