Empeld
Empeld plugin documentation.
pluginbase.Helpers.Computative.MExt Class Reference

Math extensions More...

Static Public Member Functions

static int mmod (int i, int m)
 Mod the way every other language does with negative numbers (Result should always be positive) eg. 5 % 10 = 5; -5 % 10 = 5 (In csharp -5 % 10 = -5) More...
 
static float mmodNorm (int i, int m)
 Mod where the result is normalized between 0-1 More...
 
static int bounce (int i, int m)
 A modulo that bounces back and forth More...
 
static float WrapUnit (float val)
 Wraps a number between 0-1 (Effectively truncating it) Negative numbers will be wrapped up eg -0.14 -> 0.86 More...
 
static float FloatRemainder (float val, float div)
 Get the remainder of two floats divided by each other i.e. 11/4, has a remainder of 3; or 4 / 1.5 has a remainder of 1; or -3 / 10 = 7 More...
 
static double FloatRemainder (double val, double div)
 Get the remainder of two floats divided by each other i.e. 11/4, has a remainder of 3; or 4 / 1.5 has a remainder of 1; or -3 / 10 = 7 More...
 
static float NormalizedFloatRemainder (float val, float div)
 Gets the normalized (0.0-1.0) value of the divided remainer of two numbers More...
 
static float FloorNearest (float val, float by)
 Floors a number to the nearest unit of by (rather than removing the remainder) eg. FloorNearest(5.9, 0.2) = 0.8 More...
 
static double FloorNearest (double val, double by)
 Floors a number to the nearest unit of by (rather than removing the remainder) eg. FloorNearest(5.9, 0.2) = 0.8 More...
 
static int NextPow2 (int v)
 Gets the next power-of-2. i.e. if 513 was passed in, it'd return 1024 More...
 
static int FloorInt (float v)
 Make a floor of the float and then converts to an int (i.e. 0.5 -> 0, -1.5 -> -2) More...
 
static int FloorInt (double v)
 Fast floor int for double More...
 
static int CeilInt (float v)
 Ceils the int. More...
 
static int CeilInt (double v)
 Ceils the int. More...
 
static float Fraction (float v)
 Gets the fractional part of the value More...
 
static double Fraction (double v)
 Gets the fractional part of the value More...
 
static float RadianToDegree (float radian)
 Radians to degree. More...
 
static float DegreeToRadian (float degree)
 Degrees to radian. More...
 
static double RadianToDegree (double radian)
 Radians to degree. More...
 
static double DegreeToRadian (double degree)
 Degrees to radian. More...
 
static float Clamp (float v, float min, float max)
 Clamp the specified v, min and max. More...
 
static int Clamp (int v, int min, int max)
 Clamp a number between min and max More...
 
static long Clamp (long v, long min, long max)
 Clamp a number between min and max More...
 
static double Clamp (double v, double min, double max)
 Clamp a number between min and max More...
 
static float ClampUnit (float v)
 Clamp a float between 0 and 1, inclusive More...
 
static double ClampUnit (double v)
 Clamp a double between 0 and 1, inclusive More...
 
static int FloorDiv (int a, int b)
 Returns the floor value of the division of two numbers C# Standard specifies that the div op moves the remainder closer to 0, rather than the normal standard (a higher value) More...
 
static int CeilDiv (int a, int b)
 Gets the ceiling of a number that moves towards the higher value, rather than away from 0 More...
 
static float Normalize (float v, float min, float max)
 Scales the values between the max and min to a 0-1 scale eg 0.6 between 0.5 and 1 = 0.2 More...
 
static double Normalize (double v, double min, double max)
 Scales the values between the max and min to a 0-1 scale eg 0.6 between 0.5 and 1 = 0.2 More...
 
static float NormalizeClamp (float v, float min, float max)
 Normalize the value, and clamp between 0 and 1 More...
 
static double NormalizeClamp (double v, double min, double max)
 Normalize the value and clamp between 0 and 1 More...
 
static T Max< T > (params T[] args)
 Get the max value in all the arguments More...
 
static T Min< T > (params T[] args)
 Get the min value in all the arguments More...
 

Public Attributes

const float Pi = (float)Math.PI
 Pi in float form More...
 

Detailed Description

Math extensions

Member Function Documentation

◆ bounce()

static int pluginbase.Helpers.Computative.MExt.bounce ( int  i,
int  m 
)
static

A modulo that bounces back and forth

Parameters
iA System.Int32
mA System.Int32
Returns
A System.Int32
73  {
74  return Math.Abs((m-1) - MExt.mmod(i, m*2-1 ));
75  }

◆ CeilDiv()

static int pluginbase.Helpers.Computative.MExt.CeilDiv ( int  a,
int  b 
)
static

Gets the ceiling of a number that moves towards the higher value, rather than away from 0

Returns
The div.
Parameters
aThe alpha component.
bThe blue component.
414  {
415  return (a ^ b) > 0 && a % b != 0 ? a / b + 1 : a / b;
416  }

◆ CeilInt() [1/2]

static int pluginbase.Helpers.Computative.MExt.CeilInt ( float  v)
static

Ceils the int.

Returns
The int.
Parameters
vV.
223  {
224  return v < 0f ? (int)v : (int)(v + NEAR_ONE_FLOAT);
225  }

◆ CeilInt() [2/2]

static int pluginbase.Helpers.Computative.MExt.CeilInt ( double  v)
static

Ceils the int.

Returns
The int.
Parameters
vV.
237  {
238  return v < 0.0 ? (int)v : (int)(v + NEAR_ONE_DOUBLE);
239  }

◆ Clamp() [1/4]

static float pluginbase.Helpers.Computative.MExt.Clamp ( float  v,
float  min,
float  max 
)
static

Clamp the specified v, min and max.

Parameters
vV.
minMinimum.
maxMax.
332  {
333  return v < min ? min : (v > max ? max : v);
334  }

◆ Clamp() [2/4]

static int pluginbase.Helpers.Computative.MExt.Clamp ( int  v,
int  min,
int  max 
)
static

Clamp a number between min and max

Parameters
vV.
minMinimum.
maxMax.
343  {
344  return v < min ? min : (v > max ? max : v);
345  }

◆ Clamp() [3/4]

static long pluginbase.Helpers.Computative.MExt.Clamp ( long  v,
long  min,
long  max 
)
static

Clamp a number between min and max

Parameters
vV.
minMinimum.
maxMax.
354  {
355  return v < min ? min : (v > max ? max : v);
356  }

◆ Clamp() [4/4]

static double pluginbase.Helpers.Computative.MExt.Clamp ( double  v,
double  min,
double  max 
)
static

Clamp a number between min and max

Parameters
vV.
minMinimum.
maxMax.
365  {
366  return v < min ? min : (v > max ? max : v);
367  }

◆ ClampUnit() [1/2]

static float pluginbase.Helpers.Computative.MExt.ClampUnit ( float  v)
static

Clamp a float between 0 and 1, inclusive

Returns
Clamped value.
Parameters
vValue
375  {
376  return v < 0f ? 0f : (v > 1f ? 1f : v);
377  }

◆ ClampUnit() [2/2]

static double pluginbase.Helpers.Computative.MExt.ClampUnit ( double  v)
static

Clamp a double between 0 and 1, inclusive

Returns
Clamped value
Parameters
vValue
385  {
386  return v < 0.0 ? 0.0 : (v > 1.0 ? 1.0 : v);
387  }

◆ DegreeToRadian() [1/2]

static float pluginbase.Helpers.Computative.MExt.DegreeToRadian ( float  degree)
static

Degrees to radian.

Returns
The to radian.
Parameters
degreeDegree.
287  {
288  return degree * FLOAT_PI_OVER_180;
289  }

◆ DegreeToRadian() [2/2]

static double pluginbase.Helpers.Computative.MExt.DegreeToRadian ( double  degree)
static

Degrees to radian.

Returns
The to radian.
Parameters
degreeDegree.
315  {
316  return degree * DBL_PI_OVER_180;
317  }

◆ FloatRemainder() [1/2]

static float pluginbase.Helpers.Computative.MExt.FloatRemainder ( float  val,
float  div 
)
static

Get the remainder of two floats divided by each other i.e. 11/4, has a remainder of 3; or 4 / 1.5 has a remainder of 1; or -3 / 10 = 7

Returns
The remainder.
Parameters
valValue.
divDiv.
102  {
103  return val - (float)Math.Floor(val/div)*div;
104  }

◆ FloatRemainder() [2/2]

static double pluginbase.Helpers.Computative.MExt.FloatRemainder ( double  val,
double  div 
)
static

Get the remainder of two floats divided by each other i.e. 11/4, has a remainder of 3; or 4 / 1.5 has a remainder of 1; or -3 / 10 = 7

Returns
The remainder.
Parameters
valValue.
divDiv.
120  {
121  return val - Math.Floor(val / div) * div;
122  }

◆ FloorDiv()

static int pluginbase.Helpers.Computative.MExt.FloorDiv ( int  a,
int  b 
)
static

Returns the floor value of the division of two numbers C# Standard specifies that the div op moves the remainder closer to 0, rather than the normal standard (a higher value)

Returns
The div.
Parameters
aA.
bB.
403  {
404  return (a ^ b) < 0 && a % b != 0 ? a / b -1 : a / b;
405  }

◆ FloorInt() [1/2]

static int pluginbase.Helpers.Computative.MExt.FloorInt ( float  v)
static

Make a floor of the float and then converts to an int (i.e. 0.5 -> 0, -1.5 -> -2)

Returns
The int.
Parameters
vV.
195  {
196  return v < 0f ? (int)(v - NEAR_ONE_FLOAT) : (int)v;
197  }

◆ FloorInt() [2/2]

static int pluginbase.Helpers.Computative.MExt.FloorInt ( double  v)
static

Fast floor int for double

Returns
The int.
Parameters
vV.
209  {
210  return v < 0.0 ? (int)(v - NEAR_ONE_DOUBLE) : (int)v;
211  }

◆ FloorNearest() [1/2]

static float pluginbase.Helpers.Computative.MExt.FloorNearest ( float  val,
float  by 
)
static

Floors a number to the nearest unit of by (rather than removing the remainder) eg. FloorNearest(5.9, 0.2) = 0.8

Returns
The nearest.
Parameters
valValue.
byBy.
149  {
150  return (float)Math.Floor(val / by) * by;
151  }

◆ FloorNearest() [2/2]

static double pluginbase.Helpers.Computative.MExt.FloorNearest ( double  val,
double  by 
)
static

Floors a number to the nearest unit of by (rather than removing the remainder) eg. FloorNearest(5.9, 0.2) = 0.8

Returns
The nearest.
Parameters
valValue.
byBy.
161  {
162  return Math.Floor(val / by) * by;
163  }

◆ Fraction() [1/2]

static float pluginbase.Helpers.Computative.MExt.Fraction ( float  v)
static

Gets the fractional part of the value

Parameters
vV.
248  {
249  return v - (int)v;
250  }

◆ Fraction() [2/2]

static double pluginbase.Helpers.Computative.MExt.Fraction ( double  v)
static

Gets the fractional part of the value

Parameters
vV.
259  {
260  return v - (int)v;
261  }

◆ Max< T >()

static T pluginbase.Helpers.Computative.MExt.Max< T > ( params T []  args)
static

Get the max value in all the arguments

Parameters
argsArguments.
Template Parameters
TThe 1st type parameter.

Attribute: 0

;

Attribute: i

.CompareTo(ret) > 0)

Attribute: i

;

Type Constraints
T :struct 
T :IComparable<T> 
474  : struct, IComparable<T>
475  {
476  T ret = args[0];
477  for (int i=1; i<args.Length; ++i)
478  {
479  if (args[i].CompareTo(ret) > 0)
480  ret = args[i];
481  }
482  return ret;
483  }

◆ Min< T >()

static T pluginbase.Helpers.Computative.MExt.Min< T > ( params T []  args)
static

Get the min value in all the arguments

Parameters
argsArguments.
Template Parameters
TThe 1st type parameter.

Attribute: 0

;

Attribute: i

.CompareTo(ret) < 0)

Attribute: i

;

Type Constraints
T :struct 
T :IComparable<T> 
491  : struct, IComparable<T>
492  {
493  T ret = args[0];
494  for (int i=1; i<args.Length; ++i)
495  {
496  if (args[i].CompareTo(ret) < 0)
497  ret = args[i];
498  }
499  return ret;
500  }

◆ mmod()

static int pluginbase.Helpers.Computative.MExt.mmod ( int  i,
int  m 
)
static

Mod the way every other language does with negative numbers (Result should always be positive) eg. 5 % 10 = 5; -5 % 10 = 5 (In csharp -5 % 10 = -5)

Parameters
iA System.Int32
mA System.Int32
Returns
A System.Int32
37  {
38  //This method is slightly slower in debug mode, but if it's allowed to JIT
39  //it's absurdly fast.
40  return (i%m+m)%m;
41  }

◆ mmodNorm()

static float pluginbase.Helpers.Computative.MExt.mmodNorm ( int  i,
int  m 
)
static

Mod where the result is normalized between 0-1

Returns
The norm.
Parameters
iI.
mM.
56  {
57  return ((i%m+m)%m) / (float)m;
58  }

◆ NextPow2()

static int pluginbase.Helpers.Computative.MExt.NextPow2 ( int  v)
static

Gets the next power-of-2. i.e. if 513 was passed in, it'd return 1024

Returns
The next pow2.
Parameters
vV.
175  {
176  v--;
177  v |= v >> 1;
178  v |= v >> 2;
179  v |= v >> 4;
180  v |= v >> 8;
181  v |= v >> 16;
182  return v + 1;
183  }

◆ Normalize() [1/2]

static float pluginbase.Helpers.Computative.MExt.Normalize ( float  v,
float  min,
float  max 
)
static

Scales the values between the max and min to a 0-1 scale eg 0.6 between 0.5 and 1 = 0.2

Parameters
vV.
minMinimum.
maxMax.
426  {
427  return (v - min) / (max - min);
428  }

◆ Normalize() [2/2]

static double pluginbase.Helpers.Computative.MExt.Normalize ( double  v,
double  min,
double  max 
)
static

Scales the values between the max and min to a 0-1 scale eg 0.6 between 0.5 and 1 = 0.2

Parameters
vV.
minMinimum.
maxMax.
438  {
439  return (v - min) / (max - min);
440  }

◆ NormalizeClamp() [1/2]

static float pluginbase.Helpers.Computative.MExt.NormalizeClamp ( float  v,
float  min,
float  max 
)
static

Normalize the value, and clamp between 0 and 1

Returns
The clamp.
Parameters
vV.
minMinimum.
maxMax.
450  {
451  float x = (v - min) / (max - min);
452  return x < 0f ? 0f : (x > 1f ? 1f : x);
453  }

◆ NormalizeClamp() [2/2]

static double pluginbase.Helpers.Computative.MExt.NormalizeClamp ( double  v,
double  min,
double  max 
)
static

Normalize the value and clamp between 0 and 1

Returns
The clamp.
Parameters
vV.
minMinimum.
maxMax.
463  {
464  double x = (v - min) / (max - min);
465  return x < 0f ? 0f : (x > 1f ? 1f : x);
466  }

◆ NormalizedFloatRemainder()

static float pluginbase.Helpers.Computative.MExt.NormalizedFloatRemainder ( float  val,
float  div 
)
static

Gets the normalized (0.0-1.0) value of the divided remainer of two numbers

Returns
The float remainder.
Parameters
valValue.
divDiv.
137  {
138  return (val - (float)Math.Floor(val/div)*div) / div;
139  }

◆ RadianToDegree() [1/2]

static float pluginbase.Helpers.Computative.MExt.RadianToDegree ( float  radian)
static

Radians to degree.

Returns
The to degree.
Parameters
radianRadian.
273  {
274  return radian * FLOAT_180_OVER_PI;
275  }

◆ RadianToDegree() [2/2]

static double pluginbase.Helpers.Computative.MExt.RadianToDegree ( double  radian)
static

Radians to degree.

Returns
The to degree.
Parameters
radianRadian.
301  {
302  return radian * DBL_180_OVER_PI;
303  }

◆ WrapUnit()

static float pluginbase.Helpers.Computative.MExt.WrapUnit ( float  val)
static

Wraps a number between 0-1 (Effectively truncating it) Negative numbers will be wrapped up eg -0.14 -> 0.86

Returns
The unit.
Parameters
valValue.
84  {
85  return val - (float)Math.Floor(val);
86  }

Member Data Documentation

◆ Pi

const float pluginbase.Helpers.Computative.MExt.Pi = (float)Math.PI

Pi in float form


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