Euphoria
numeric.h
Go to the documentation of this file.
1 #pragma once
2 
4 #include "base/ints.h"
5 
6 namespace eu
7 {
8  constexpr float abs(float r)
9  {
10  return r >= 0.0f
11  ? r
12  : -r
13  ;
14  }
15 
16  // int overloads provided for template overload reasons
17  constexpr bool is_zero(float r)
18  {
19  constexpr float epsilon = 0.0001f;
20  return abs(r) < epsilon;
21  }
22 
23  constexpr bool
24  is_equal(float lhs, float rhs)
25  {
26  return is_zero(lhs - rhs);
27  }
28 
29  bool
30  is_equal(int lhs, int rhs);
31 
32  bool
33  is_zero(int r);
34 
35  float
36  clamp_zero(float r);
37 
38  float
39  floor(float v);
40 
41  float
42  ceil(float v);
43 
44  int
45  floor_to_int(float v);
46 
47  int
48  ceil_to_int(float v);
49 
50 
55  int
56  get_sign(float r);
57 
58 
59  int
60  get_sign(int r);
61 
62 
64  float
65  get_sign(bool b);
66 
67 
68  float
69  lerp_float(float f, float scale, float t);
70 
72 
73  float
74  square(float r);
75 
76  float
77  sqrt(float r);
78 
79 #define EU_MAKE_MINMAX(T) \
80  constexpr T min(T lhs, T rhs) \
81  { \
82  if (lhs < rhs) \
83  { \
84  return lhs; \
85  } \
86  \
87  return rhs; \
88  } \
89  constexpr T max(T lhs, T rhs) \
90  { \
91  if (lhs > rhs) \
92  { \
93  return lhs; \
94  } \
95  \
96  return rhs; \
97  }
98  EU_MAKE_MINMAX(float)
99  // EU_MAKE_MINMAX(int)
100  // EU_MAKE_MINMAX(std::size_t)
109 #undef EU_MAKE_MINMAX
110 
111 
112  float
113  mod(float numer, float denumer);
114 
115  bool
116  is_within_inclusive_as_int(int min, int c, int max);
117 
124  float
125  round(float num, float gran);
126 
127  constexpr float pi = 3.1415926535897932384626433832795f;
128  // constexpr float half_pi = pi / 2.0f;
129 
130  template <typename T>
131  T
132  get_default_if_close_to_zero(T value, T def, T epsilon)
133  {
134  if(abs(value) < epsilon) { return def; }
135  else { return value; }
136  }
137 
140  template <typename I>
141  constexpr bool
142  is_each_nth(I i, I each)
143  {
144  return each && (i % each) == each - 1;
145  }
146 
147 }
148 
Definition: assert.h:90
float sqrt(float r)
Definition: numeric.cc:101
float clamp_zero(float r)
Definition: numeric.cc:27
constexpr float pi
Definition: numeric.h:127
float lerp_float(float f, float scale, float t)
Definition: numeric.cc:87
std::int32_t I32
Definition: ints.h:8
std::uint16_t U16
Definition: ints.h:14
std::int8_t I8
Definition: ints.h:10
std::uint32_t U32
Definition: ints.h:13
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
std::int64_t I64
Definition: ints.h:7
constexpr bool is_each_nth(I i, I each)
returns true for a index each Nth loop.
Definition: numeric.h:142
constexpr float abs(float r)
Definition: numeric.h:8
float square(float r)
Definition: numeric.cc:94
std::uint8_t U8
Definition: ints.h:15
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
DEFAULT_INTERPOLATE(Angle, lerp_angle)
T get_default_if_close_to_zero(T value, T def, T epsilon)
Definition: numeric.h:132
std::int16_t I16
Definition: ints.h:9
size2f min(const size2f lhs, const size2f rhs)
Definition: size2.cc:140
std::uint64_t U64
Definition: ints.h:12
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
#define EU_MAKE_MINMAX(T)
Definition: numeric.h:79