versor  3.0
C++11 library for Geometric algebra
vsr_math.h
1 /*
2  * vsr_math.h
3  * CONGA_07
4  *
5  * Created by x on 8/25/10.
6  * Copyright 2010 x. All rights reserved.
7  *
8  */
9 
10 #ifndef VSR_MATH_INCLUDED
11 #define VSR_MATH_INCLUDED
12 
13 #include <math.h>
14 
15 namespace vsr {
16 
17  struct Math{
18  //public:
19  static double sinc(double x);
20  static double sinhc(double x);
21  static double clamp(double x, double min, double max);
22  static double map(double x, double min, double max, double rmin, double rmax);
23 
24  static int bitcount(int x);
25 
26  template<typename T>
27  inline static bool Range(T v, T low, T high){
28  return ( v >= low && v <= high) ? true : false;
29  }
30 
31  template<typename T>
32  inline static bool Error(T v, T goal, T error){
33  return Range(v, goal - error, goal + error);
34  }
35 
36 // template<typename T>
37 // inline static T log(T v) { return log(v); }
38 
39  };
40 
41  inline double Math::sinc(double x) {
42  if (FERROR(x)) return 1; else return sin(x)/(x);
43  }
44 
45  inline double Math::sinhc(double x) {
46  if (FERROR(x)) return 1; else return sinh(x)/x;
47  }
48 
49  inline double Math::clamp(double x, double min, double max){
50  return (x < min) ? min : (x > max ) ? max : x;
51  }
52 
53  inline double Math::map(double x, double min, double max, double rmin, double rmax){
54  double orange = max - min;
55  double drange = rmax - rmin;
56 
57  double offset = x - min;
58  double ratio = offset / orange;
59 
60  double doffset = ratio * drange;
61 
62  return rmin + doffset;
63  }
64 
65  inline int Math::bitcount(int x){
66  int n = 0;
67  do {
68  if (x&1) n += 1;
69  x = x >> 1;
70  } while(x);
71  return n;
72  }
73 }
74 
75 #endif
Definition: vsr_math.h:17
the versor library namespace
Definition: vsr_algebra.h:29