BGC Tools
Static Public Member Functions | Private Attributes
BGC.Mathematics.GeneralMath Class Reference
Collaboration diagram for BGC.Mathematics.GeneralMath:
Collaboration graph
[legend]

Static Public Member Functions

static int Mod (int x, int m)
 x mod m More...
 
static float Mod (float x, float m)
 x mod m More...
 
static double Tanh (double angle)
 Hyperbolic Tangent in radian More...
 
static double Sinh (double angle)
 Hyperbolic Sine More...
 
static double Cosh (double angle)
 Hyperbolic Cosine More...
 
static float Tanh (float angle)
 Hyperbolic Tangent in radian More...
 
static float Sinh (float angle)
 Hyperbolic Sine More...
 
static float Cosh (float angle)
 Hyperbolic Cosine More...
 
static bool IsPowerOfTwo (this int number)
 Find out whether the provided 32 bit integer is a perfect power of two. More...
 
static int ToNextExponentOf2 (this int x)
 
static int CeilingToPowerOfTwo (this int v)
 
static decimal Clamp (decimal value, decimal min, decimal max)
 
static double Clamp (double value, double min, double max)
 
static float Clamp (float value, float min, float max)
 
static long Clamp (long value, long min, long max)
 
static ulong Clamp (ulong value, ulong min, ulong max)
 
static int Clamp (int value, int min, int max)
 
static uint Clamp (uint value, uint min, uint max)
 
static short Clamp (short value, short min, short max)
 
static ushort Clamp (ushort value, ushort min, ushort max)
 
static byte Clamp (byte value, byte min, byte max)
 
static sbyte Clamp (sbyte value, sbyte min, sbyte max)
 
static decimal Clamp01 (decimal value)
 Clamps value to range [0, 1] More...
 
static double Clamp01 (double value)
 Clamps value to range [0, 1] More...
 
static float Clamp01 (float value)
 Clamps value to range [0, 1] More...
 
static decimal Lerp (decimal initial, decimal final, decimal t)
 Linearly interpolates between initial and final by t , clamps t to range [0,1] More...
 
static decimal LerpUnclamped (decimal initial, decimal final, decimal t)
 Linearly interpolates between initial and final by t without clamping t between 0 and 1 More...
 
static double Lerp (double initial, double final, double t)
 Linearly interpolates between initial and final by t , clamps t to range [0,1] More...
 
static double LerpUnclamped (double initial, double final, double t)
 Linearly interpolates between initial and final by t without clamping t between 0 and 1 More...
 
static float Lerp (float initial, float final, float t)
 Linearly interpolates between initial and final by t , clamps t to range [0,1] More...
 
static float LerpUnclamped (float initial, float final, float t)
 Linearly interpolates between initial and final by t without clamping t between 0 and 1 More...
 
static decimal Repeat (decimal value, decimal min, decimal max)
 
static double Repeat (double value, double min, double max)
 
static float Repeat (float value, float min, float max)
 
static long Repeat (long value, long min, long max)
 
static int Repeat (int value, int min, int max)
 
static bool Approximately (float a, float b)
 
static bool Approximately (double a, double b)
 

Private Attributes

const float FLOAT_MANTISSA_LOWER_BOUND = 1.1920929E-7f
 
const float FLOAT_SMALLEST_NORMAL = 1.1754943508E-38f
 
const float FLOAT_COMPARISON_MAX_FACTOR = 8 * FLOAT_MANTISSA_LOWER_BOUND
 
const float FLOAT_COMPARISON_LOWER_BOUND = 8 * FLOAT_SMALLEST_NORMAL
 
const double DOUBLE_MANTISSA_LOWER_BOUND = 2.220446E-16
 
const double DOUBLE_SMALLEST_NORMAL = 2.2250738585072014E10-308
 
const double DOUBLE_COMPARISON_MAX_FACTOR = 8 * DOUBLE_MANTISSA_LOWER_BOUND
 
const double DOUBLE_COMPARISON_LOWER_BOUND = 8 * DOUBLE_SMALLEST_NORMAL
 

Detailed Description

Definition at line 6 of file GeneralMath.cs.

Member Function Documentation

◆ Approximately() [1/2]

static bool BGC.Mathematics.GeneralMath.Approximately ( float  a,
float  b 
)
inlinestatic

Definition at line 326 of file GeneralMath.cs.

Referenced by BGC.Mathematics.Complex64.Equals(), and BGC.Mathematics.Complex32.Equals().

327  {
328  return Math.Abs(b - a) <= Math.Max(
329  FLOAT_COMPARISON_MAX_FACTOR * Math.Max(Math.Abs(a), Math.Abs(b)),
331  }
const float FLOAT_COMPARISON_MAX_FACTOR
Definition: GeneralMath.cs:323
const float FLOAT_COMPARISON_LOWER_BOUND
Definition: GeneralMath.cs:324
Here is the caller graph for this function:

◆ Approximately() [2/2]

static bool BGC.Mathematics.GeneralMath.Approximately ( double  a,
double  b 
)
inlinestatic

Definition at line 338 of file GeneralMath.cs.

339  {
340  return Math.Abs(b - a) <= Math.Max(
341  DOUBLE_COMPARISON_MAX_FACTOR * Math.Max(Math.Abs(a), Math.Abs(b)),
343  }
const double DOUBLE_COMPARISON_MAX_FACTOR
Definition: GeneralMath.cs:335
const double DOUBLE_COMPARISON_LOWER_BOUND
Definition: GeneralMath.cs:336

◆ CeilingToPowerOfTwo()

static int BGC.Mathematics.GeneralMath.CeilingToPowerOfTwo ( this int  v)
inlinestatic

Definition at line 124 of file GeneralMath.cs.

125  {
126  v--;
127  v |= v >> 1;
128  v |= v >> 2;
129  v |= v >> 4;
130  v |= v >> 8;
131  v |= v >> 16;
132  v++;
133  return v;
134  }

◆ Clamp() [1/11]

static decimal BGC.Mathematics.GeneralMath.Clamp ( decimal  value,
decimal  min,
decimal  max 
)
static

◆ Clamp() [2/11]

static double BGC.Mathematics.GeneralMath.Clamp ( double  value,
double  min,
double  max 
)
static

◆ Clamp() [3/11]

static float BGC.Mathematics.GeneralMath.Clamp ( float  value,
float  min,
float  max 
)
static

◆ Clamp() [4/11]

static long BGC.Mathematics.GeneralMath.Clamp ( long  value,
long  min,
long  max 
)
static

◆ Clamp() [5/11]

static ulong BGC.Mathematics.GeneralMath.Clamp ( ulong  value,
ulong  min,
ulong  max 
)
static

◆ Clamp() [6/11]

static int BGC.Mathematics.GeneralMath.Clamp ( int  value,
int  min,
int  max 
)
static

◆ Clamp() [7/11]

static uint BGC.Mathematics.GeneralMath.Clamp ( uint  value,
uint  min,
uint  max 
)
static

◆ Clamp() [8/11]

static short BGC.Mathematics.GeneralMath.Clamp ( short  value,
short  min,
short  max 
)
static

◆ Clamp() [9/11]

static ushort BGC.Mathematics.GeneralMath.Clamp ( ushort  value,
ushort  min,
ushort  max 
)
static

◆ Clamp() [10/11]

static byte BGC.Mathematics.GeneralMath.Clamp ( byte  value,
byte  min,
byte  max 
)
static

◆ Clamp() [11/11]

static sbyte BGC.Mathematics.GeneralMath.Clamp ( sbyte  value,
sbyte  min,
sbyte  max 
)
static

◆ Clamp01() [1/3]

static decimal BGC.Mathematics.GeneralMath.Clamp01 ( decimal  value)
inlinestatic

Clamps value to range [0, 1]

Definition at line 177 of file GeneralMath.cs.

178  {
179  if (value < 0)
180  {
181  return 0;
182  }
183  else if (value > 1)
184  {
185  return 1;
186  }
187 
188  return value;
189  }

◆ Clamp01() [2/3]

static double BGC.Mathematics.GeneralMath.Clamp01 ( double  value)
inlinestatic

Clamps value to range [0, 1]

Definition at line 194 of file GeneralMath.cs.

195  {
196  if (value < 0)
197  {
198  return 0;
199  }
200  else if (value > 1)
201  {
202  return 1;
203  }
204 
205  return value;
206  }

◆ Clamp01() [3/3]

static float BGC.Mathematics.GeneralMath.Clamp01 ( float  value)
inlinestatic

Clamps value to range [0, 1]

Definition at line 211 of file GeneralMath.cs.

212  {
213  if (value < 0)
214  {
215  return 0;
216  }
217  else if (value > 1)
218  {
219  return 1;
220  }
221 
222  return value;
223  }

◆ Cosh() [1/2]

static double BGC.Mathematics.GeneralMath.Cosh ( double  angle)
static

Hyperbolic Cosine

Parameters
angleThe hyperbolic angle, i.e. the area of the hyperbolic sector.
Returns
The hyperbolic Cosine of the angle.

Referenced by BGC.Mathematics.Complex64.Cos(), BGC.Mathematics.Complex32.Cos(), BGC.Mathematics.Complex64.Cosh(), BGC.Mathematics.Complex32.Cosh(), BGC.Mathematics.Complex64.Sin(), BGC.Mathematics.Complex32.Sin(), BGC.Mathematics.Complex64.Sinh(), BGC.Mathematics.Complex32.Sinh(), BGC.Mathematics.Complex64.Tanh(), and BGC.Mathematics.Complex32.Tanh().

Here is the caller graph for this function:

◆ Cosh() [2/2]

static float BGC.Mathematics.GeneralMath.Cosh ( float  angle)
static

Hyperbolic Cosine

Parameters
angleThe hyperbolic angle, i.e. the area of the hyperbolic sector.
Returns
The hyperbolic Cosine of the angle.

◆ IsPowerOfTwo()

static bool BGC.Mathematics.GeneralMath.IsPowerOfTwo ( this int  number)
static

Find out whether the provided 32 bit integer is a perfect power of two.

Parameters
numberThe number to very whether it's a power of two.
Returns
True if and only if it is a power of two.

◆ Lerp() [1/3]

static decimal BGC.Mathematics.GeneralMath.Lerp ( decimal  initial,
decimal  final,
decimal  t 
)
inlinestatic

Linearly interpolates between initial and final by t , clamps t to range [0,1]

Definition at line 232 of file GeneralMath.cs.

Referenced by BGC.MonoUtility.Interpolation.FadeImageAlpha.CallAction(), BGC.Audio.Calibration.GetLevelOffset(), and BGC.Audio.Visualization.SpectralDecomp.Linterp().

233  {
234  t = Clamp01(t);
235  return initial * (1 - t) + final * t;
236  }
static decimal Clamp01(decimal value)
Clamps value to range [0, 1]
Definition: GeneralMath.cs:177
Here is the caller graph for this function:

◆ Lerp() [2/3]

static double BGC.Mathematics.GeneralMath.Lerp ( double  initial,
double  final,
double  t 
)
inlinestatic

Linearly interpolates between initial and final by t , clamps t to range [0,1]

Definition at line 251 of file GeneralMath.cs.

252  {
253  t = Clamp01(t);
254  return initial * (1 - t) + final * t;
255  }
static decimal Clamp01(decimal value)
Clamps value to range [0, 1]
Definition: GeneralMath.cs:177

◆ Lerp() [3/3]

static float BGC.Mathematics.GeneralMath.Lerp ( float  initial,
float  final,
float  t 
)
inlinestatic

Linearly interpolates between initial and final by t , clamps t to range [0,1]

Definition at line 270 of file GeneralMath.cs.

271  {
272  t = Clamp01(t);
273  return initial * (1 - t) + final * t;
274  }
static decimal Clamp01(decimal value)
Clamps value to range [0, 1]
Definition: GeneralMath.cs:177

◆ LerpUnclamped() [1/3]

static decimal BGC.Mathematics.GeneralMath.LerpUnclamped ( decimal  initial,
decimal  final,
decimal  t 
)
inlinestatic

Linearly interpolates between initial and final by t without clamping t between 0 and 1

Definition at line 242 of file GeneralMath.cs.

243  {
244  return initial * (1 - t) + final * t;
245  }

◆ LerpUnclamped() [2/3]

static double BGC.Mathematics.GeneralMath.LerpUnclamped ( double  initial,
double  final,
double  t 
)
inlinestatic

Linearly interpolates between initial and final by t without clamping t between 0 and 1

Definition at line 261 of file GeneralMath.cs.

262  {
263  return initial * (1 - t) + final * t;
264  }

◆ LerpUnclamped() [3/3]

static float BGC.Mathematics.GeneralMath.LerpUnclamped ( float  initial,
float  final,
float  t 
)
inlinestatic

Linearly interpolates between initial and final by t without clamping t between 0 and 1

Definition at line 280 of file GeneralMath.cs.

281  {
282  return initial * (1 - t) + final * t;
283  }

◆ Mod() [1/2]

static int BGC.Mathematics.GeneralMath.Mod ( int  x,
int  m 
)
inlinestatic

x mod m

Parameters
x
m
Returns

Definition at line 14 of file GeneralMath.cs.

15  {
16  // https://stackoverflow.com/questions/1082917/mod-of-negative-number-is-melting-my-brain
17  return ((x % m) + m) % m;
18  }

◆ Mod() [2/2]

static float BGC.Mathematics.GeneralMath.Mod ( float  x,
float  m 
)
inlinestatic

x mod m

Parameters
x
m
Returns

Definition at line 26 of file GeneralMath.cs.

27  {
28  return ((x % m) + m) % m;
29  }

◆ Repeat() [1/5]

static decimal BGC.Mathematics.GeneralMath.Repeat ( decimal  value,
decimal  min,
decimal  max 
)
inlinestatic

Definition at line 288 of file GeneralMath.cs.

Referenced by BGC.Audio.CarrierTone.CarrierTone().

289  {
290  decimal scanningValue = (value - min) % (max - min);
291  return scanningValue >= 0 ? scanningValue + min : scanningValue + max;
292  }
Here is the caller graph for this function:

◆ Repeat() [2/5]

static double BGC.Mathematics.GeneralMath.Repeat ( double  value,
double  min,
double  max 
)
inlinestatic

Definition at line 294 of file GeneralMath.cs.

295  {
296  double scanningValue = (value - min) % (max - min);
297  return scanningValue >= 0 ? scanningValue + min : scanningValue + max;
298  }

◆ Repeat() [3/5]

static float BGC.Mathematics.GeneralMath.Repeat ( float  value,
float  min,
float  max 
)
inlinestatic

Definition at line 300 of file GeneralMath.cs.

301  {
302  float scanningValue = (value - min) % (max - min);
303  return scanningValue >= 0 ? scanningValue + min : scanningValue + max;
304  }

◆ Repeat() [4/5]

static long BGC.Mathematics.GeneralMath.Repeat ( long  value,
long  min,
long  max 
)
inlinestatic

Definition at line 306 of file GeneralMath.cs.

307  {
308  long scanningValue = (value - min) % (max - min);
309  return scanningValue >= 0 ? scanningValue + min : scanningValue + max;
310  }

◆ Repeat() [5/5]

static int BGC.Mathematics.GeneralMath.Repeat ( int  value,
int  min,
int  max 
)
inlinestatic

Definition at line 312 of file GeneralMath.cs.

313  {
314  int scanningValue = (value - min) % (max - min);
315  return scanningValue >= 0 ? scanningValue + min : scanningValue + max;
316  }

◆ Sinh() [1/2]

static double BGC.Mathematics.GeneralMath.Sinh ( double  angle)
static

Hyperbolic Sine

Parameters
angleThe hyperbolic angle, i.e. the area of the hyperbolic sector.
Returns
The hyperbolic sine of the angle.

Referenced by BGC.Mathematics.Complex64.Cos(), BGC.Mathematics.Complex32.Cos(), BGC.Mathematics.Complex64.Cosh(), BGC.Mathematics.Complex32.Cosh(), BGC.Mathematics.Complex64.Sin(), BGC.Mathematics.Complex32.Sin(), BGC.Mathematics.Complex64.Sinh(), BGC.Mathematics.Complex32.Sinh(), BGC.Mathematics.Complex64.Tanh(), and BGC.Mathematics.Complex32.Tanh().

Here is the caller graph for this function:

◆ Sinh() [2/2]

static float BGC.Mathematics.GeneralMath.Sinh ( float  angle)
static

Hyperbolic Sine

Parameters
angleThe hyperbolic angle, i.e. the area of the hyperbolic sector.
Returns
The hyperbolic sine of the angle.

◆ Tanh() [1/2]

static double BGC.Mathematics.GeneralMath.Tanh ( double  angle)
inlinestatic

Hyperbolic Tangent in radian

Parameters
angleThe hyperbolic angle, i.e. the area of the hyperbolic sector.
Returns
The hyperbolic tangent of the angle.

Definition at line 38 of file GeneralMath.cs.

Referenced by BGC.Audio.Envelopes.SigmoidEnvelope.Read(), BGC.Audio.Envelopes.SigmoidEnvelope.ReadNextSample(), BGC.Mathematics.Complex64.Tanh(), and BGC.Mathematics.Complex32.Tanh().

39  {
40  if (angle > 19.1)
41  {
42  return 1.0;
43  }
44 
45  if (angle < -19.1)
46  {
47  return -1.0;
48  }
49 
50  double e1 = Math.Exp(angle);
51  double e2 = Math.Exp(-angle);
52  return (e1 - e2) / (e1 + e2);
53  }
Here is the caller graph for this function:

◆ Tanh() [2/2]

static float BGC.Mathematics.GeneralMath.Tanh ( float  angle)
inlinestatic

Hyperbolic Tangent in radian

Parameters
angleThe hyperbolic angle, i.e. the area of the hyperbolic sector.
Returns
The hyperbolic tangent of the angle.

Definition at line 79 of file GeneralMath.cs.

80  {
81  if (angle > 19.1f)
82  {
83  return 1.0f;
84  }
85 
86  if (angle < -19.1f)
87  {
88  return -1f;
89  }
90 
91  float e1 = Mathf.Exp(angle);
92  float e2 = Mathf.Exp(-angle);
93  return (e1 - e2) / (e1 + e2);
94  }

◆ ToNextExponentOf2()

static int BGC.Mathematics.GeneralMath.ToNextExponentOf2 ( this int  x)
static

Field Documentation

◆ DOUBLE_COMPARISON_LOWER_BOUND

const double BGC.Mathematics.GeneralMath.DOUBLE_COMPARISON_LOWER_BOUND = 8 * DOUBLE_SMALLEST_NORMAL
private

Definition at line 336 of file GeneralMath.cs.

◆ DOUBLE_COMPARISON_MAX_FACTOR

const double BGC.Mathematics.GeneralMath.DOUBLE_COMPARISON_MAX_FACTOR = 8 * DOUBLE_MANTISSA_LOWER_BOUND
private

Definition at line 335 of file GeneralMath.cs.

◆ DOUBLE_MANTISSA_LOWER_BOUND

const double BGC.Mathematics.GeneralMath.DOUBLE_MANTISSA_LOWER_BOUND = 2.220446E-16
private

Definition at line 333 of file GeneralMath.cs.

◆ DOUBLE_SMALLEST_NORMAL

const double BGC.Mathematics.GeneralMath.DOUBLE_SMALLEST_NORMAL = 2.2250738585072014E10-308
private

Definition at line 334 of file GeneralMath.cs.

◆ FLOAT_COMPARISON_LOWER_BOUND

const float BGC.Mathematics.GeneralMath.FLOAT_COMPARISON_LOWER_BOUND = 8 * FLOAT_SMALLEST_NORMAL
private

Definition at line 324 of file GeneralMath.cs.

◆ FLOAT_COMPARISON_MAX_FACTOR

const float BGC.Mathematics.GeneralMath.FLOAT_COMPARISON_MAX_FACTOR = 8 * FLOAT_MANTISSA_LOWER_BOUND
private

Definition at line 323 of file GeneralMath.cs.

◆ FLOAT_MANTISSA_LOWER_BOUND

const float BGC.Mathematics.GeneralMath.FLOAT_MANTISSA_LOWER_BOUND = 1.1920929E-7f
private

Definition at line 321 of file GeneralMath.cs.

◆ FLOAT_SMALLEST_NORMAL

const float BGC.Mathematics.GeneralMath.FLOAT_SMALLEST_NORMAL = 1.1754943508E-38f
private

Definition at line 322 of file GeneralMath.cs.


The documentation for this class was generated from the following file: