VCV Rack API v2
rack::math Namespace Reference

Extends <cmath> with extra functions and types. More...

Classes

struct  Rect
 2-dimensional rectangle for graphics. More...
 
struct  Vec
 2-dimensional vector of floats, representing a point on the plane for graphics. More...
 

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=0.f, float b=1.f)
 Limits x between a and b. More...
 
float clampSafe (float x, float a=0.f, float b=1.f)
 Limits x between a and b. More...
 
 __attribute__ ((optimize("signed-zeros"))) inline 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)
 Rescales x from the range [xMin, xMax] to [yMin, yMax]. More...
 
float crossfade (float a, float b, float p)
 Linearly interpolates between a and b, from p = 0 to p = 1. More...
 
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...
 
Vec operator+ (const Vec &a)
 
Vec operator- (const Vec &a)
 
Vec operator+ (const Vec &a, const Vec &b)
 
Vec operator- (const Vec &a, const Vec &b)
 
Vec operator* (const Vec &a, const Vec &b)
 
Vec operator* (const Vec &a, const float &b)
 
Vec operator* (const float &a, const Vec &b)
 
Vec operator/ (const Vec &a, const Vec &b)
 
Vec operator/ (const Vec &a, const float &b)
 
Vec operator+= (Vec &a, const Vec &b)
 
Vec operator-= (Vec &a, const Vec &b)
 
Vec operator*= (Vec &a, const Vec &b)
 
Vec operator*= (Vec &a, const float &b)
 
Vec operator/= (Vec &a, const Vec &b)
 
Vec operator/= (Vec &a, const float &b)
 
bool operator== (const Vec &a, const Vec &b)
 
bool operator!= (const Vec &a, const Vec &b)
 
bool operator== (const Rect &a, const Rect &b)
 
bool operator!= (const Rect &a, const Rect &b)
 

Detailed Description

Extends <cmath> with extra 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 = 0.f,
float  b = 1.f 
)
inline

Limits x between a and b.

If b < a, returns a.

◆ clampSafe() [2/2]

float rack::math::clampSafe ( float  x,
float  a = 0.f,
float  b = 1.f 
)
inline

Limits x between a and b.

If b < a, switches the two values.

◆ __attribute__()

rack::math::__attribute__ ( (optimize("signed-zeros"))  )

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

Rescales x from the range [xMin, xMax] to [yMin, yMax].

◆ crossfade()

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

Linearly interpolates between a and b, from p = 0 to p = 1.

◆ 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);

◆ operator+() [1/2]

Vec rack::math::operator+ ( const Vec a)
inline

◆ operator-() [1/2]

Vec rack::math::operator- ( const Vec a)
inline

◆ operator+() [2/2]

Vec rack::math::operator+ ( const Vec a,
const Vec b 
)
inline

◆ operator-() [2/2]

Vec rack::math::operator- ( const Vec a,
const Vec b 
)
inline

◆ operator*() [1/3]

Vec rack::math::operator* ( const Vec a,
const Vec b 
)
inline

◆ operator*() [2/3]

Vec rack::math::operator* ( const Vec a,
const float &  b 
)
inline

◆ operator*() [3/3]

Vec rack::math::operator* ( const float &  a,
const Vec b 
)
inline

◆ operator/() [1/2]

Vec rack::math::operator/ ( const Vec a,
const Vec b 
)
inline

◆ operator/() [2/2]

Vec rack::math::operator/ ( const Vec a,
const float &  b 
)
inline

◆ operator+=()

Vec rack::math::operator+= ( Vec a,
const Vec b 
)
inline

◆ operator-=()

Vec rack::math::operator-= ( Vec a,
const Vec b 
)
inline

◆ operator*=() [1/2]

Vec rack::math::operator*= ( Vec a,
const Vec b 
)
inline

◆ operator*=() [2/2]

Vec rack::math::operator*= ( Vec a,
const float &  b 
)
inline

◆ operator/=() [1/2]

Vec rack::math::operator/= ( Vec a,
const Vec b 
)
inline

◆ operator/=() [2/2]

Vec rack::math::operator/= ( Vec a,
const float &  b 
)
inline

◆ operator==() [1/2]

bool rack::math::operator== ( const Vec a,
const Vec b 
)
inline

◆ operator!=() [1/2]

bool rack::math::operator!= ( const Vec a,
const Vec b 
)
inline

◆ operator==() [2/2]

bool rack::math::operator== ( const Rect a,
const Rect b 
)
inline

◆ operator!=() [2/2]

bool rack::math::operator!= ( const Rect a,
const Rect b 
)
inline