VCV Rack API v2
Loading...
Searching...
No Matches
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.
 
template<typename T >
bool isOdd (T x)
 Returns true if x is odd.
 
int clamp (int x, int a, int b)
 Limits x between a and b.
 
int clampSafe (int x, int a, int b)
 Limits x between a and b.
 
int eucMod (int a, int b)
 Euclidean modulus.
 
int eucDiv (int a, int b)
 Euclidean division.
 
void eucDivMod (int a, int b, int *div, int *mod)
 
int log2 (int n)
 Returns floor(log_2(n)), or 0 if n == 1.
 
template<typename T >
bool isPow2 (T n)
 Returns whether n is a power of 2.
 
template<typename T >
sgn (T x)
 Returns 1 for positive numbers, -1 for negative numbers, and 0 for zero.
 
float clamp (float x, float a=0.f, float b=1.f)
 Limits x between a and b.
 
float clampSafe (float x, float a=0.f, float b=1.f)
 Limits x between a and b.
 
 __attribute__ ((optimize("signed-zeros"))) inline float normalizeZero(float x)
 Converts -0.f to 0.f.
 
float eucMod (float a, float b)
 Euclidean modulus.
 
bool isNear (float a, float b, float epsilon=1e-6f)
 Returns whether a is within epsilon distance from b.
 
float chop (float x, float epsilon=1e-6f)
 If the magnitude of x if less than epsilon, return 0.
 
float rescale (float x, float xMin, float xMax, float yMin, float yMax)
 Rescales x from the range [xMin, xMax] to [yMin, yMax].
 
float crossfade (float a, float b, float p)
 Linearly interpolates between a and b, from p = 0 to p = 1.
 
float interpolateLinear (const float *p, float x)
 Linearly interpolates an array p with index x.
 
void complexMult (float ar, float ai, float br, float bi, float *cr, float *ci)
 Complex multiplication c = a * b.
 
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 ( T x)

Returns true if x is odd.

◆ isOdd()

template<typename T >
bool rack::math::isOdd ( T 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 ( T n)

Returns whether n is a power of 2.

◆ sgn()

template<typename T >
T rack::math::sgn ( T 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