VCV Rack API  v1
rack::math Namespace Reference

Supplemental <cmath> functions and types. More...

Classes

struct  Rect
 
struct  Vec
 

Functions

template<typename T >
bool isEven (T x)
 Returns true if x is odd. More...
 
template<typename T >
bool isOdd (T x)
 Returns true if x is odd. More...
 
int clamp (int x, int a, int b)
 Limits x between a and b. More...
 
int clampSafe (int x, int a, int b)
 Limits x between a and b. More...
 
int eucMod (int a, int b)
 Euclidean modulus. More...
 
int eucDiv (int a, int b)
 Euclidean division. More...
 
void eucDivMod (int a, int b, int *div, int *mod)
 
int log2 (int n)
 Returns floor(log_2(n)), or 0 if n == 1. More...
 
template<typename T >
bool isPow2 (T n)
 Returns whether n is a power of 2. More...
 
template<typename T >
sgn (T x)
 Returns 1 for positive numbers, -1 for negative numbers, and 0 for zero. More...
 
float clamp (float x, float a, float b)
 Limits x between a and b. More...
 
float clampSafe (float x, float a, float b)
 Limits x between a and b. More...
 
float normalizeZero (float x)
 Converts -0.f to 0.f. More...
 
float eucMod (float a, float b)
 Euclidean modulus. More...
 
bool isNear (float a, float b, float epsilon=1e-6f)
 Returns whether a is within epsilon distance from b. More...
 
float chop (float x, float epsilon=1e-6f)
 If the magnitude of x if less than epsilon, return 0. More...
 
float rescale (float x, float xMin, float xMax, float yMin, float yMax)
 
float crossfade (float a, float b, float p)
 
float interpolateLinear (const float *p, float x)
 Linearly interpolates an array p with index x. More...
 
void complexMult (float ar, float ai, float br, float bi, float *cr, float *ci)
 Complex multiplication c = a * b. More...
 

Detailed Description

Supplemental <cmath> functions and types.

Function Documentation

◆ isEven()

template<typename T >
bool rack::math::isEven ( x)

Returns true if x is odd.

◆ isOdd()

template<typename T >
bool rack::math::isOdd ( x)

Returns true if x is odd.

◆ clamp() [1/2]

int rack::math::clamp ( int  x,
int  a,
int  b 
)
inline

Limits x between a and b.

If b < a, returns a.

◆ clampSafe() [1/2]

int rack::math::clampSafe ( int  x,
int  a,
int  b 
)
inline

Limits x between a and b.

If b < a, switches the two values.

◆ eucMod() [1/2]

int rack::math::eucMod ( int  a,
int  b 
)
inline

Euclidean modulus.

Always returns 0 <= mod < b. b must be positive. See https://en.wikipedia.org/wiki/Euclidean_division

◆ eucDiv()

int rack::math::eucDiv ( int  a,
int  b 
)
inline

Euclidean division.

b must be positive.

◆ eucDivMod()

void rack::math::eucDivMod ( int  a,
int  b,
int *  div,
int *  mod 
)
inline

◆ log2()

int rack::math::log2 ( int  n)
inline

Returns floor(log_2(n)), or 0 if n == 1.

◆ isPow2()

template<typename T >
bool rack::math::isPow2 ( n)

Returns whether n is a power of 2.

◆ sgn()

template<typename T >
T rack::math::sgn ( x)

Returns 1 for positive numbers, -1 for negative numbers, and 0 for zero.

See https://en.wikipedia.org/wiki/Sign_function.

◆ clamp() [2/2]

float rack::math::clamp ( float  x,
float  a,
float  b 
)
inline

Limits x between a and b.

If b < a, returns a.

◆ clampSafe() [2/2]

float rack::math::clampSafe ( float  x,
float  a,
float  b 
)
inline

Limits x between a and b.

If b < a, switches the two values.

◆ normalizeZero()

float rack::math::normalizeZero ( float  x)
inline

Converts -0.f to 0.f.

Leaves all other values unchanged.

◆ eucMod() [2/2]

float rack::math::eucMod ( float  a,
float  b 
)
inline

Euclidean modulus.

Always returns 0 <= mod < b. See https://en.wikipedia.org/wiki/Euclidean_division.

◆ isNear()

bool rack::math::isNear ( float  a,
float  b,
float  epsilon = 1e-6f 
)
inline

Returns whether a is within epsilon distance from b.

◆ chop()

float rack::math::chop ( float  x,
float  epsilon = 1e-6f 
)
inline

If the magnitude of x if less than epsilon, return 0.

◆ rescale()

float rack::math::rescale ( float  x,
float  xMin,
float  xMax,
float  yMin,
float  yMax 
)
inline

◆ crossfade()

float rack::math::crossfade ( float  a,
float  b,
float  p 
)
inline

◆ interpolateLinear()

float rack::math::interpolateLinear ( const float *  p,
float  x 
)
inline

Linearly interpolates an array p with index x.

The array at p must be at least length floor(x) + 2.

◆ complexMult()

void rack::math::complexMult ( float  ar,
float  ai,
float  br,
float  bi,
float *  cr,
float *  ci 
)
inline

Complex multiplication c = a * b.

Arguments may be the same pointers. Example:

cmultf(ar, ai, br, bi, &ar, &ai);