Gray C++ Libraries  0.0.2
A set of C++ libraries for MSVC, GNU on Windows, WinCE, Linux
GrayLib::CalcI Struct Reference

#include <Calc.h>

Inheritance diagram for GrayLib::CalcI:
GrayLib::Calc

Static Public Member Functions

template<typename TYPE >
static int Sign (TYPE a)
 
template<typename TYPE >
static bool IsInRange (TYPE n, TYPE low, TYPE high) noexcept
 
template<typename TYPE >
static bool IsInBetween (TYPE n, TYPE a, TYPE b) noexcept
 
template<typename TYPE >
static TYPE Min (TYPE a, TYPE b) noexcept
 
template<typename TYPE >
static TYPE Max (TYPE a, TYPE b) noexcept
 
template<typename TYPE >
static TYPE Max3 (TYPE a, TYPE b, TYPE c) noexcept
 
template<typename TYPE >
static TYPE Clamp (TYPE value, TYPE low, TYPE high)
 
template<typename TYPE >
static void ClampRef (TYPE &rValue, TYPE low, TYPE high)
 
template<typename TYPE >
static bool IsNear (TYPE n1, TYPE n2, TYPE nDiff=(TYPE) k_FLT_MIN2) noexcept
 
template<typename TYPE >
static TYPE Sqr (TYPE a) noexcept
 
template<typename TYPE >
static TYPE Abs (TYPE a) noexcept
 similar to ABS(n) macro. Does nothing for unsigned types. More...
 
template<typename TYPE >
static TYPE Mod (TYPE a, TYPE b)
 a modulus b = remainder of a/b More...
 
template<typename TYPE >
static TYPE Wrap (TYPE nVal, TYPE nMod)
 
static bool IsSignDiff (int a, int b) noexcept
 
template<typename TYPE >
static TYPE GetGreatestCommonDivisor (TYPE x, TYPE y)
 
template<typename TYPE >
static TYPE MulDiv (TYPE a, TYPE b, TYPE c)
 
template<typename TYPE >
static TYPE DivideUp (TYPE a, TYPE b)
 
template<typename TYPE >
static TYPE AddC (TYPE a, TYPE b, bool &bCarry) noexcept
 
template<typename TYPE >
static TYPE SubC (TYPE a, TYPE b, bool &bBorrow) noexcept
 
template<typename TYPE >
static bool ToBool (TYPE nVal) noexcept
 
static int GRAYCALL Log2Est (UINT iVal)
 
static Percent10_t GRAYCALL GetSCurve (int iValDiff, int iValVarianceHalf)
 
static Percent10_t GRAYCALL GetBellCurve (int iValDiff, int iValVarianceHalf)
 
template<>
float Abs (float a) noexcept
 
template<>
float Mod (float a, float b)
 
template<>
double Abs (double a) noexcept
 
template<>
double Mod (double a, double b)
 
template<>
int Abs (int a) noexcept
 
template<>
int Mod (int a, int b)
 
template<>
UINT Abs (UINT a) noexcept
 
template<>
UINT Mod (UINT a, UINT b)
 
template<>
UINT16 MulDiv (UINT16 a, UINT16 b, UINT16 c)
 

Detailed Description

Basic (non floating point) math calculations. works with integer or float/double numbers.

Member Function Documentation

◆ Abs() [1/5]

template<>
double GrayLib::CalcI::Abs ( double  a)
inlinestaticnoexcept

◆ Abs() [2/5]

template<>
float GrayLib::CalcI::Abs ( float  a)
inlinestaticnoexcept

◆ Abs() [3/5]

template<>
int GrayLib::CalcI::Abs ( int  a)
inlinestaticnoexcept

◆ Abs() [4/5]

template<typename TYPE >
static TYPE GrayLib::CalcI::Abs ( TYPE  a)
inlinestaticnoexcept

similar to ABS(n) macro. Does nothing for unsigned types.

◆ Abs() [5/5]

template<>
UINT GrayLib::CalcI::Abs ( UINT  a)
inlinestaticnoexcept
Note
absolute value doesn't really make sense for unsigned types ? should i be checking the high bit ? M$ does, GNUC does not.

◆ AddC()

template<typename TYPE >
static TYPE GrayLib::CalcI::AddC ( TYPE  a,
TYPE  b,
bool &  bCarry 
)
inlinestaticnoexcept

add (unsigned type) + carry (in) with carry (out) return. like M$ _addcarry_u64() TODO Move to cTypeLimit ?

◆ Clamp()

template<typename TYPE >
static TYPE GrayLib::CalcI::Clamp ( TYPE  value,
TYPE  low,
TYPE  high 
)
inlinestatic

similar to MIN() MAX() effects clamp_t

◆ ClampRef()

template<typename TYPE >
static void GrayLib::CalcI::ClampRef ( TYPE rValue,
TYPE  low,
TYPE  high 
)
inlinestatic

similar to MIN() MAX() effects clamp_t

◆ DivideUp()

template<typename TYPE >
static TYPE GrayLib::CalcI::DivideUp ( TYPE  a,
TYPE  b 
)
inlinestatic

Divide and round up result.

◆ GetBellCurve()

Percent10_t GRAYCALL GrayLib::CalcI::GetBellCurve ( int  iValDiff,
int  iValVarianceHalf 
)
static

Produce a simple log curve as a percent chance that something could happen.

//! 50+
//!       |
//!       |
//!       |
//! 25|  +
//!       |
//!       |        +
//!       |               +
//!     0 --+--+--+--+------
//!    iVar                             iValDiff
//! 
@arg
 iValDiff = (-100 to +100) Given a value relative to 0
            0 = 50.0% chance.
 iValVarianceHalf = the 25.0% point of the bell curve. Similar to Standard Deviation.
@return
 (0-100.0) % chance at this iValDiff.
 Chance gets smaller as Diff gets bigger.
EXAMPLE:
 if ( iValDiff == iValVarianceHalf ) return( 250 )
 if ( iValDiff == 0 ) return( 500 ); 

◆ GetGreatestCommonDivisor()

template<typename TYPE >
static TYPE GrayLib::CalcI::GetGreatestCommonDivisor ( TYPE  x,
TYPE  y 
)
inlinestatic

This function uses the Euclidean algorithm to find the greatest common divisor of the positive integers x and y. This function will fail in undefined ways if either x or y is zero. http://en.wikipedia.org/wiki/Greatest_common_divisor

Returns
the greatest common divisor of a and b.

◆ GetSCurve()

Percent10_t GRAYCALL GrayLib::CalcI::GetSCurve ( int  iValDiff,
int  iValVarianceHalf 
)
static

a percent chance that something could happen.

  • iValDiff = (-100 to +100) Difference between our skill level and difficulty. positive = high chance, negative = lower chance 0 = 50.0% chance. iValVarianceHalf = the 25.0% difference point of the bell curve. Similar to Standard Deviation.
    Returns
    what is the (0-100.0)% chance of success = 0-1000
    Note
    Chance of skill gain is inverse to chance of success.

◆ IsInBetween()

template<typename TYPE >
static bool GrayLib::CalcI::IsInBetween ( TYPE  n,
TYPE  a,
TYPE  b 
)
inlinestaticnoexcept
Returns
is n inside inclusive arbitrary range. min/max are not assumed to be sorted.

◆ IsInRange()

template<typename TYPE >
static bool GrayLib::CalcI::IsInRange ( TYPE  n,
TYPE  low,
TYPE  high 
)
inlinestaticnoexcept
Returns
is n inside inclusive range.

◆ IsNear()

template<typename TYPE >
static bool GrayLib::CalcI::IsNear ( TYPE  n1,
TYPE  n2,
TYPE  nDiff = (TYPE)k_FLT_MIN2 
)
inlinestaticnoexcept

◆ IsSignDiff()

static bool GrayLib::CalcI::IsSignDiff ( int  a,
int  b 
)
inlinestaticnoexcept

is the sign diff ? Does nothing for unsigned types.

◆ Log2Est()

int GRAYCALL GrayLib::CalcI::Log2Est ( UINT  iVal)
static

This is really a very rough approximate/estimate of log2() + 1 "::logf(x) * M_LOG2E"

◆ Max()

template<typename TYPE >
static TYPE GrayLib::CalcI::Max ( TYPE  a,
TYPE  b 
)
inlinestaticnoexcept

similar to MAX() macro

◆ Max3()

template<typename TYPE >
static TYPE GrayLib::CalcI::Max3 ( TYPE  a,
TYPE  b,
TYPE  c 
)
inlinestaticnoexcept
Returns
Max of 3 values.

◆ Min()

template<typename TYPE >
static TYPE GrayLib::CalcI::Min ( TYPE  a,
TYPE  b 
)
inlinestaticnoexcept

similar to MIN() macro

◆ Mod() [1/5]

template<>
double GrayLib::CalcI::Mod ( double  a,
double  b 
)
inlinestatic

NOT the same as modf() but similar.

◆ Mod() [2/5]

template<>
float GrayLib::CalcI::Mod ( float  a,
float  b 
)
inlinestatic

◆ Mod() [3/5]

template<>
int GrayLib::CalcI::Mod ( int  a,
int  b 
)
inlinestatic

◆ Mod() [4/5]

template<typename TYPE >
static TYPE GrayLib::CalcI::Mod ( TYPE  a,
TYPE  b 
)
inlinestatic

a modulus b = remainder of a/b

◆ Mod() [5/5]

template<>
UINT GrayLib::CalcI::Mod ( UINT  a,
UINT  b 
)
inlinestatic

◆ MulDiv() [1/2]

template<typename TYPE >
static TYPE GrayLib::CalcI::MulDiv ( TYPE  a,
TYPE  b,
TYPE  c 
)
inlinestatic

Multiply and divide using higher precision intermediate values. Try not to lose precision.

Returns
((a*b)/c)
Note
_WIN32 MulDiv() function will round ?! should just do intermediate 64 bit math (for UINT32)? 128 bit math for UINT64? like __mulh() ?

◆ MulDiv() [2/2]

template<>
UINT16 GrayLib::CalcI::MulDiv ( UINT16  a,
UINT16  b,
UINT16  c 
)
inlinestatic

UINT16 multiply and divide with 32 bit intermediate.

Returns
((a*b)/c)

◆ Sign()

template<typename TYPE >
static int GrayLib::CalcI::Sign ( TYPE  a)
inlinestatic
Returns
the sign of the value. (1,0,-1) Does nothing for unsigned types of course.

◆ Sqr()

template<typename TYPE >
static TYPE GrayLib::CalcI::Sqr ( TYPE  a)
inlinestaticnoexcept

Square = Pow(a,2)

◆ SubC()

template<typename TYPE >
static TYPE GrayLib::CalcI::SubC ( TYPE  a,
TYPE  b,
bool &  bBorrow 
)
inlinestaticnoexcept

subtract (unsigned type) with borrow input and borrow output. (borrow AKA carry) TODO Move to cTypeLimit ?

◆ ToBool()

template<typename TYPE >
static bool GrayLib::CalcI::ToBool ( TYPE  nVal)
inlinestaticnoexcept

Convert a number to bool (0/1) with minimal instructions and no branching.

◆ Wrap()

template<typename TYPE >
static TYPE GrayLib::CalcI::Wrap ( TYPE  nVal,
TYPE  nMod 
)
inlinestatic

similar to Mod/fmod() except the value is always wrapped back to positive/unsigned


The documentation for this struct was generated from the following files: