uLib  User mode C/C++ extended API library for Win32 programmers.
Arithmetic Supplements

Module Description

Some additional useful arithmetic functions, f.ex. map().

Note
Due to many "modern" compiler's lack of support for FPU 80 bit "long double",
the "real number" functions (e.g. _root()) may suffer truncation errors due to the
lack of precision in "double", and You need to account for that in Your programming.

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 >
map_ (T x, T xMin, T xMax, T toMin, T toMax)
 
template<typename T >
constrain_ (T x, T lo, T hi)
 
template<typename T >
min_ (T a, T b)
 
template<typename T >
max_ (T a, T b)
 
template<typename T >
runningAvg_ (T Avg, T X, unsigned int Period)
 
template<typename T >
mulDiv_ (const T X, const T Mu, const T Dv)
 

Macro Definition Documentation

◆ min

#define min (   a,
 
)    ((a)<(b)?(a):(b))

Definition at line 933 of file Common.h.

◆ max

#define max (   a,
 
)    ((a)>(b)?(a):(b))

Definition at line 936 of file Common.h.

Function Documentation

◆ map()

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.

◆ constrain()

long constrain ( long  x,
long  lo,
long  hi 
)

Limit a value to an inclusive range.

Definition at line 151 of file Common.cpp.

◆ random()

short random ( short  iMin,
short  iMax 
)

Return a range inclusive random number. Negatives also possible.

Definition at line 156 of file Common.cpp.

◆ randomize()

void randomize ( )

Randomize the internal random seed.

Definition at line 167 of file Common.cpp.

◆ runningAvg()

long runningAvg ( long  Avg,
long  X,
unsigned int  Period 
)

Return the running average of Avg and X over Period.
Example:

Avg = runningAvg( Avg, In, 4 );

Definition at line 176 of file Common.cpp.

◆ ipow2()

ulong ipow2 ( ushort  expo)

Compute the integer 2^expo (by left shift).

Definition at line 183 of file Common.cpp.

◆ ipow2x()

uint64 ipow2x ( ushort  expo)

Compute the 64bit integer 2^expo (by left shift).

Definition at line 189 of file Common.cpp.

◆ ilog2()

ushort ilog2 ( ulong  X)

Compute the integer log2 of X (by right shift).

Definition at line 208 of file Common.cpp.

◆ ilog2x()

ushort ilog2x ( uint64  X)

See ilog2( ulong ).

Definition at line 241 of file Common.cpp.

◆ ilog10()

ushort ilog10 ( ulong  X)

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.

◆ ilog10x()

ushort ilog10x ( uint64  X)

See ilog10( ulong ).

Definition at line 271 of file Common.cpp.

◆ _root()

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.

◆ _log()

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.

◆ _round()

double _round ( double  x,
unsigned char  ndecimals 
)

Round a floating point number to arbitrary precision.

Note
Due to the limited internal math precision of many "modern" compilers,
the the result of _round() is itself subject to rounding errors [sic],
which may produce an aberration in the last decimal.
Hence, Your programming may need to account for this.

Definition at line 343 of file Common.cpp.

◆ _isprime()

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.

◆ EngUnits()

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.

◆ map_()

template<typename T >
T map_ ( x,
xMin,
xMax,
toMin,
toMax 
)

See map()

float Y = map_<float>( X, 0, 255, -1.0f, 1.0f );

Note: The templated map_ might overflow in the intermediate product!

Definition at line 891 of file Common.h.

◆ constrain_()

template<typename T >
T constrain_ ( x,
lo,
hi 
)

See constrain()

Definition at line 901 of file Common.h.

◆ min_()

template<typename T >
T min_ ( a,
b 
)

Definition at line 906 of file Common.h.

◆ max_()

template<typename T >
T max_ ( a,
b 
)

Definition at line 907 of file Common.h.

◆ runningAvg_()

template<typename T >
T runningAvg_ ( Avg,
X,
unsigned int  Period 
)

See runningAvg()

Definition at line 912 of file Common.h.

◆ mulDiv_()

template<typename T >
T mulDiv_ ( const T  X,
const T  Mu,
const T  Dv 
)

The muldiv ratio concept is sometimes useful even for non-integers.

Definition at line 920 of file Common.h.