uLib
User mode C/C++ extended API library for Win32 programmers.
|
Some additional useful arithmetic functions, f.ex. map().
Macros | |
#define | min(a, b) ((a)<(b)?(a):(b)) |
#define | max(a, b) ((a)>(b)?(a):(b)) |
Functions | |
long | map (long x, long xMin, long xMax, long toMin, long toMax) |
long | constrain (long x, long lo, long hi) |
short | random (short iMin, short iMax) |
void | randomize () |
long | runningAvg (long Avg, long X, unsigned int Period) |
ulong | ipow2 (ushort expo) |
uint64 | ipow2x (ushort expo) |
ushort | ilog2 (ulong X) |
ushort | ilog2x (uint64 X) |
ushort | ilog10 (ulong X) |
ushort | ilog10x (uint64 X) |
long double | _root (long double base, long double X) |
long double | _log (long double base, long double X) |
double | _round (double x, unsigned char ndecimals) |
bool | _isprime (unsigned long X) |
long double | EngUnits (long double Value, int *pExpo) |
template<typename T > | |
T | map_ (T x, T xMin, T xMax, T toMin, T toMax) |
template<typename T > | |
T | constrain_ (T x, T lo, T hi) |
template<typename T > | |
T | min_ (T a, T b) |
template<typename T > | |
T | max_ (T a, T b) |
template<typename T > | |
T | runningAvg_ (T Avg, T X, unsigned int Period) |
template<typename T > | |
T | mulDiv_ (const T X, const T Mu, const T Dv) |
long map | ( | long | x, |
long | xMin, | ||
long | xMax, | ||
long | toMin, | ||
long | toMax | ||
) |
Convert one integer range to another.
Inversions are also possible.
Definition at line 145 of file Common.cpp.
long constrain | ( | long | x, |
long | lo, | ||
long | hi | ||
) |
Limit a value to an inclusive range.
Definition at line 151 of file Common.cpp.
short random | ( | short | iMin, |
short | iMax | ||
) |
Return a range inclusive random number. Negatives also possible.
Definition at line 156 of file Common.cpp.
void randomize | ( | ) |
Randomize the internal random seed.
Definition at line 167 of file Common.cpp.
long runningAvg | ( | long | Avg, |
long | X, | ||
unsigned int | Period | ||
) |
Return the running average of Avg and X over Period.
Example:
Definition at line 176 of file Common.cpp.
Compute the integer 2^expo (by left shift).
Definition at line 183 of file Common.cpp.
Compute the 64bit integer 2^expo (by left shift).
Definition at line 189 of file Common.cpp.
Compute the integer log2 of X (by right shift).
Definition at line 208 of file Common.cpp.
See ilog2( ulong ).
Definition at line 241 of file Common.cpp.
Compute the integer log10 of an integer.
ilog10() is computed by the relationship log10(v) = log2(v) / log2(10).
Note: The return value is an integer approximation, not the true log10().
Definition at line 247 of file Common.cpp.
See ilog10( ulong ).
Definition at line 271 of file Common.cpp.
long double _root | ( | long double | base, |
long double | X | ||
) |
Compute the N'th root of x, e.g. root( 2,x ) == sqrt( x ).
Calculated with the inverse power method, _root( b,x ) = x^(1/b).
Definition at line 296 of file Common.cpp.
long double _log | ( | long double | base, |
long double | X | ||
) |
Compute the base n logartithm of x.
Calculated by the relationship logB(x) = log(x) / log(B).
Definition at line 303 of file Common.cpp.
double _round | ( | double | x, |
unsigned char | ndecimals | ||
) |
Round a floating point number to arbitrary precision.
Definition at line 343 of file Common.cpp.
bool _isprime | ( | unsigned long | x | ) |
_isprime() returns true if x is a prime number, using an allocation-free "Sieve of Erathostenes". Credits to an unknown Borland programmer!
_isprime() returns true if x is a prime number.
Thus is a port of an old "set-less" Borland Pascal implementation of the
"Sieve of Erathostenes". While this may be slower than using a bit array
to tick off values already checked (due to having to re-mod the cases)
it does not need any allocation, hence can be used "on the fly".
Definition at line 373 of file Common.cpp.
long double EngUnits | ( | long double | Value, |
int * | pExpo | ||
) |
Eng[ineering]Units returns a value as a multiple of 10^3, and it's exponent.
Note: MSVC silently trucates long double to a measly double.
Definition at line 180 of file UtilFunc.cpp.
T map_ | ( | T | x, |
T | xMin, | ||
T | xMax, | ||
T | toMin, | ||
T | toMax | ||
) |
T constrain_ | ( | T | x, |
T | lo, | ||
T | hi | ||
) |
See constrain()
T runningAvg_ | ( | T | Avg, |
T | X, | ||
unsigned int | Period | ||
) |
See runningAvg()