Euphoria
numeric.cc
Go to the documentation of this file.
1 #include "base/numeric.h"
2 
3 #include "assert/assert.h"
4 #include "base/angle.h"
5 #include "base/range.h"
6 
7 #include <cmath>
8 
9 
10 namespace eu
11 {
12  bool
13  is_equal(int lhs, int rhs)
14  {
15  return lhs == rhs;
16  }
17 
18 
19  bool
20  is_zero(int r)
21  {
22  return r == 0;
23  }
24 
25 
26  float
27  clamp_zero(float r)
28  {
29  if(is_zero(r)) { return 0; }
30  else { return r; }
31  }
32 
33 
34  float
35  floor(float v)
36  {
37  return std::floor(v);
38  }
39 
40 
41  float
42  ceil(float v)
43  {
44  return std::ceil(v);
45  }
46 
47 
48  int
49  floor_to_int(float v)
50  {
51  return static_cast<int>(std::floor(v));
52  }
53 
54 
55  int
56  ceil_to_int(float v)
57  {
58  return static_cast<int>(std::ceil(v));
59  }
60 
61 
62  int
63  get_sign(float r)
64  {
65  if(r >= 0.0f) { return 1; }
66  else { return -1; }
67  }
68 
69 
70  int
71  get_sign(int r)
72  {
73  if(r >= 0) { return 1; }
74  else { return -1; }
75  }
76 
77 
78  float
79  get_sign(bool b)
80  {
81  if(b) { return 1.0f; }
82  else { return -1.0f; }
83  }
84 
85 
86  float
87  lerp_float(float f, float scale, float t)
88  {
89  return f + (t - f) * scale;
90  }
91 
92 
93  float
94  square(float r)
95  {
96  return r * r;
97  }
98 
99 
100  float
101  sqrt(float r)
102  {
103  ASSERTX(r >= 0 && "input must be bigger than 0", r);
104  return std::sqrt(r);
105  }
106 
107 
108  float
109  mod(float numer, float denumer)
110  {
111  return ::fmodf(numer, denumer);
112  }
113 
114 
115  bool
117  {
118  return c >= min && c <= max;
119  }
120 
121 
122  namespace // internal
123  {
124  float
125  get_lower_bound(float num, float gran)
126  {
127  return std::floor(num / gran) * gran;
128  }
129 
130 
131  float
132  get_upper_bound(float num, float gran)
133  {
134  return std::ceil(num / gran) * gran;
135  }
136  }
137 
138 
139  float
140  round(float num, float gran)
141  {
142  const float lower = get_lower_bound(num, gran);
143  const float upper = get_upper_bound(num, gran);
144  const float percent = (num - lower) / gran;
145  // 1.0 is upper, 0.0 is lower, 0.25 is 25% between
146  // lower and upper, moving from lower to upper
147 
148  if(percent >= 0.5f)
149  {
150  return upper;
151  }
152 
153  return lower;
154  }
155 }
#define ASSERTX(x,...)
Definition: assert.h:48
Definition: assert.h:90
float sqrt(float r)
Definition: numeric.cc:101
float clamp_zero(float r)
Definition: numeric.cc:27
float lerp_float(float f, float scale, float t)
Definition: numeric.cc:87
bool is_zero(int r)
Definition: numeric.cc:20
bool is_within_inclusive_as_int(int min, int c, int max)
Definition: numeric.cc:116
float square(float r)
Definition: numeric.cc:94
float mod(float numer, float denumer)
Definition: numeric.cc:109
float round(float num, float gran)
Rounds a value to the nearest nice value.
Definition: numeric.cc:140
float ceil(float v)
Definition: numeric.cc:42
bool is_equal(int lhs, int rhs)
Definition: numeric.cc:13
int get_sign(float r)
Calculates the sign as a positive or a negative int.
Definition: numeric.cc:63
size2f min(const size2f lhs, const size2f rhs)
Definition: size2.cc:140
float floor(float v)
Definition: numeric.cc:35
size2f max(const size2f lhs, const size2f rhs)
Definition: size2.cc:149
int ceil_to_int(float v)
Definition: numeric.cc:56
int floor_to_int(float v)
Definition: numeric.cc:49