defines.h File Reference

Introduction

Definitions of constants and macros used in several parts of the cuik library.

See also
geom.h

Definition in file defines.h.

Macros

#define ZERO   1e-11
 Floating point operations giving a value below this constant (in absolute value) are considered 0. More...
 
#define RANDOMNESS   (_DEBUG<2)
 If 0 we avoid any randomness in the cuiksuite behavior. More...
 
#define INF   DBL_MAX
 Infinite. More...
 
#define M_PI   3.14159265358979323846
 Pi. More...
 
#define M_PI_2   1.57079632679489661923
 Pi/2. More...
 
#define M_2PI   (2.0*M_PI)
 2*Pi. More...
 
#define M_E   2.718281828459045
 e. More...
 
#define M_C   299792458
 Speed of light in meter per second.
 
#define M_G   9.80665
 Gravity acceleration.
 
#define TOPOLOGY_R   1
 One of the possible topologies. More...
 
#define TOPOLOGY_S   2
 One of the possible topologies. More...
 
#define MAX_INCLUDES   100
 Maximum number of nested included files. More...
 
#define SWAP(a, b, c)   {c=a;a=b;b=c;}
 Swaps two values. More...
 
#define DEG2RAD   (M_PI/180.0)
 Constant to transform from degrees to radiants. More...
 
#define RAD2DEG   (180.0/M_PI)
 Constant to transform from radiants to degrees. More...
 
#define SGN(a)   (a>=0?1:-1)
 Returns the sign of a floating point. More...
 
#define ADJUST_REAL(a)   (fabs(a)<ZERO?0.0:(fabs(a-1.0)<ZERO?+1.0:(fabs(a+1.0)<ZERO?-1.0:a)))
 Adjust a real to 0,1,-1, if possible. More...
 
#define PI2PI(a)   {if ((a<-M_PI-ZERO)||(a>M_PI+ZERO)) {a=fmod(a,M_2PI); while(a>M_PI) a-=M_2PI; while (a<-M_PI) a+=M_2PI;}}
 Forces an angle go be in [-pi,pi]. More...
 
#define ROUNDUP   fesetround(FE_UPWARD)
 Sets the floating point operations in rounding up mode. More...
 
#define ROUNDDOWN   fesetround(FE_DOWNWARD)
 Sets the floating point operations in rounding down mode. More...
 
#define ROUNDNEAR   fesetround(FE_TONEAREST)
 Sets the floating point operations in round near mode. More...
 
#define INF_CUT(a)   ((a<=-INF?-INF:(a>=INF)?INF:a))
 Sets a number, without going beyond +/-inf. More...
 
#define IS_INF(a)   ((a==-INF)||(a==INF))
 Identifies +/-inf. More...
 
#define IS_NOT_INF(a)   ((a!=-INF)&&(a!=INF))
 Identifies not +/-inf. More...
 
#define INF_SCALE(s, a)   (IS_INF(a)?(s<0?-a:a):(s*a))
 Scales a number, possibly inf or -inf. More...
 
#define INF_EXP(a)   ((a==-INF)?0:((a==INF)?INF:exp(a)))
 Exponentional of a number, possibly inf or -inf. More...
 
#define INF_PROD(a, b, d)   (IS_NOT_INF(a)&&IS_NOT_INF(b)?(a*b):(IS_INF(a)&&IS_INF(b)?(a==b?a:(d?-INF:INF)):(IS_INF(a)?(b>ZERO?a:(b<-ZERO?-a:0.0)):(a>ZERO?b:(a<-ZERO?-b:0.0)))))
 Product of two numbers, possibly inf or -inf. More...
 
#define INF_ADD(a, b, d)   (IS_NOT_INF(a)&&IS_NOT_INF(b)?(a+b):(IS_INF(a)&&IS_INF(b)?(a==b?a:(d?-INF:INF)):(IS_INF(a)?a:b)))
 Adds two numbers, possibly inf or -inf. More...
 
#define INF_SUBS(a, b, d)   (IS_NOT_INF(a)&&IS_NOT_INF(b)?(a-b):(IS_INF(a)&&IS_INF(b)?(a==b?(d?-INF:INF):a):(IS_INF(a)?a:-b)))
 Substract two numbers, possibly inf or -inf. More...
 
#define INF_POW(a, p)   (IS_NOT_INF(a)?pow(a,(double)p):((a==-INF)?(((p%2)==0)?INF:-INF):INF))
 Power of a number, possibly +/-inf. More...
 
#define INF_SQRT(a)   ((a==INF)?INF:(a<0.0?0.0:sqrt(a)));
 Sqrt of a number, possibly +/-inf. More...
 
#define INF_PRINT(f, a)   (a==INF?fprintf(f,"inf"):(a==-INF?fprintf(f,"-inf"):fprintf(f,"%.12g",a)))
 Prints a number (possibly +/-inf) to a file. More...
 
#define ANGLE_ACCURACY   1e-4
 Accuracy for the rounding to +/-PI, +/-PI/2. More...
 
#define ROUND2SYMBOL(a)   (fabs((a)+M_PI)<ANGLE_ACCURACY?-M_PI:(fabs((a)-M_PI)<ANGLE_ACCURACY?M_PI:(fabs((a)+M_PI_2)<ANGLE_ACCURACY?-M_PI_2:(fabs((a)-M_PI_2)<ANGLE_ACCURACY?M_PI_2:a))))
 Round to +/-PI, +/-PI/2. More...
 
#define SYMBOL_PRINT(f, a)   (fabs((a)+M_PI)<ANGLE_ACCURACY?fprintf(f,"-PI"):(fabs((a)-M_PI)<ANGLE_ACCURACY?fprintf(f,"PI"):(fabs((a)+M_PI_2)<ANGLE_ACCURACY?fprintf(f,"-PI/2"):(fabs((a)-M_PI_2)<ANGLE_ACCURACY?fprintf(f,"PI/2"):INF_PRINT(f,a)))))
 Prints a number (possibly +/-PI, +/-PI/2, or +/-inf) to a file. More...
 
#define NEW(_var, _n, _type)
 Allocates memory space. More...
 
#define NEWZ(_var, _n, _type)
 Allocates and cleans memory space. More...
 
#define MEM_EXPAND(_var, _n, _type)
 Expands a previously allocated memory space. More...
 
#define MEM_DUP(_var, _n, _type)   {(_n)*=2;MEM_EXPAND(_var,_n,_type);}
 Duplicates a previously allocated memory space. More...
 
#define PRINT_VARIABLE_NAME(f, name)   if (name[0]=='_') fprintf(f,"%s",&(name[1])); else fprintf(f,"%s",name)
 Prints a variable name into a file. More...
 
#define NO_UINT   ((unsigned int)(-1))
 Used to denote an identifier that has not been initialized. More...
 
#define MOV_AVG_UP   0.1
 Weight of new data when computing moving averages. More...
 
#define MOV_AVG_DOWN   0.1
 Weight of new data when computing moving averages. More...
 

Macro Definition Documentation

◆ ZERO

#define ZERO   1e-11

Floating point operations giving a value below this constant (in absolute value) are considered 0.

We use 1e-11 because trigonometric operations (sin/cos/...) give an error typically around 1e-12. Moreover, we print doubles with 12 decimals and, thus the last one typically varies when performing operations. Note that 1e-11 is far below the typical precission used inside the simplex algorithms (1e-6).

No to be confused with parameter EPSILON. ZERO is used as a general threshold to round towards 0 and Epsilon is used only when passing parameters to the simplex. For a super-numerically-stable simplex EPSILON and ZERO would be the same.

Definition at line 38 of file defines.h.

◆ RANDOMNESS

#define RANDOMNESS   (_DEBUG<2)

If set to 0 we avoid any randomness in the cuiksuite behavior. This is basically used for debug. In norma mode, the applications in the cuiksuite rely on some randomness (to select the variable to split a box, to select the split point,...). This is usefull to improve the performance in degenerate situations and it does not hinder the convergence in the usual cases. For debug purposes, though, this randomness can be quite annoying and can be deactivated here.

Right now we only use random behavior when in optimized mode.

Todo:
Remove this in the final version. This is for debug only

Definition at line 56 of file defines.h.

◆ INF

#define INF   DBL_MAX

Infinite.

Definition at line 71 of file defines.h.

◆ M_PI

#define M_PI   3.14159265358979323846

Pi

Definition at line 84 of file defines.h.

◆ M_PI_2

#define M_PI_2   1.57079632679489661923

Pi/2

Definition at line 93 of file defines.h.

◆ M_2PI

#define M_2PI   (2.0*M_PI)

2*Pi

Definition at line 101 of file defines.h.

◆ M_E

#define M_E   2.718281828459045

Base of the natural log.

Definition at line 109 of file defines.h.

◆ TOPOLOGY_R

#define TOPOLOGY_R   1

The range of the variables is defined over the reals.

Definition at line 133 of file defines.h.

◆ TOPOLOGY_S

#define TOPOLOGY_S   2

The range of the variables is defined over a circle.

IMPORTANT: Only variables with range equal (or larger) than 2*pi are actually considered to have TOPOLOGY_S. Variables defined with TOPOLOGY_S but with small ranges are actually treated as varaibles with TOPOLOGY_R.

If a variable x has TOPOLOGY_S it means that we can replace this variable by x+k*2*pi (k any integer) and the system of equations remains the same. If a variable is defined with TOPOLOGY_S but it does not fulfills the above condition an error is likely to occur.

Definition at line 150 of file defines.h.

◆ MAX_INCLUDES

#define MAX_INCLUDES   100

A maximum of recursively included files to avoid infinite loops.

Definition at line 159 of file defines.h.

◆ SWAP

#define SWAP (   a,
  b,
 
)    {c=a;a=b;b=c;}

Swaps two values using an intermediate storage.

Parameters
aFirst value to swap.
bSecond value to swap.
cIntermediate storage for the swap.

Definition at line 172 of file defines.h.

◆ DEG2RAD

#define DEG2RAD   (M_PI/180.0)

Constant to transform from degrees to radiants.

Definition at line 180 of file defines.h.

◆ RAD2DEG

#define RAD2DEG   (180.0/M_PI)

Constant to transform from radiants to degrees.

Definition at line 186 of file defines.h.

◆ SGN

#define SGN (   a)    (a>=0?1:-1)

Returns the sign of a floating point: -1 for negative and 1 for possitive or zero.

Parameters
aThe number to operate.

Definition at line 196 of file defines.h.

◆ ADJUST_REAL

#define ADJUST_REAL (   a)    (fabs(a)<ZERO?0.0:(fabs(a-1.0)<ZERO?+1.0:(fabs(a+1.0)<ZERO?-1.0:a)))

Adjust a real to 0,1,-1, if possible.

Parameters
aThe number to adjust.

Definition at line 206 of file defines.h.

◆ PI2PI

#define PI2PI (   a)    {if ((a<-M_PI-ZERO)||(a>M_PI+ZERO)) {a=fmod(a,M_2PI); while(a>M_PI) a-=M_2PI; while (a<-M_PI) a+=M_2PI;}}

Forces an angle go be in [-pi,pi]. This is intended to be used with a variable as a parameter (using an expression would be ineficient).

Definition at line 216 of file defines.h.

◆ ROUNDUP

#define ROUNDUP   fesetround(FE_UPWARD)

Sets the floating point operations in rounding up mode.

Definition at line 224 of file defines.h.

◆ ROUNDDOWN

#define ROUNDDOWN   fesetround(FE_DOWNWARD)

Sets the floating point operations in rounding down mode.

Definition at line 230 of file defines.h.

◆ ROUNDNEAR

#define ROUNDNEAR   fesetround(FE_TONEAREST)

Sets the floating point operations in round near mode. This is the default mode.

Definition at line 236 of file defines.h.

◆ INF_CUT

#define INF_CUT (   a)    ((a<=-INF?-INF:(a>=INF)?INF:a))

Sets a number, without going beyond +/-inf..

Parameters
aThe number to check.

Definition at line 248 of file defines.h.

◆ IS_INF

#define IS_INF (   a)    ((a==-INF)||(a==INF))

TRUE if the number is either inf or -inf.

Parameters
aThe number to check.

Definition at line 257 of file defines.h.

◆ IS_NOT_INF

#define IS_NOT_INF (   a)    ((a!=-INF)&&(a!=INF))

TRUE if the number is not inf or -inf.

Parameters
aThe number to check.

Definition at line 266 of file defines.h.

◆ INF_SCALE

#define INF_SCALE (   s,
 
)    (IS_INF(a)?(s<0?-a:a):(s*a))

Scales a number, possibly inf or -inf.

See also
INF_PROD if 's' can be inf also.
Parameters
sThe scale factor (this is not +/-inf).
aThe number to scale.

Definition at line 278 of file defines.h.

◆ INF_EXP

#define INF_EXP (   a)    ((a==-INF)?0:((a==INF)?INF:exp(a)))

Exponentional of a number, possibly inf or -inf.

Parameters
aThe number to operate.

Definition at line 287 of file defines.h.

◆ INF_PROD

#define INF_PROD (   a,
  b,
 
)    (IS_NOT_INF(a)&&IS_NOT_INF(b)?(a*b):(IS_INF(a)&&IS_INF(b)?(a==b?a:(d?-INF:INF)):(IS_INF(a)?(b>ZERO?a:(b<-ZERO?-a:0.0)):(a>ZERO?b:(a<-ZERO?-b:0.0)))))

Product of two numbers, possibly inf or -inf.

When rounding down, INF*(-INF) is set to -INF, otherwise to INF.

Parameters
aThe first number to operate.
bThe second number to operate.
dTRUE if rounding down.

Definition at line 300 of file defines.h.

◆ INF_ADD

#define INF_ADD (   a,
  b,
 
)    (IS_NOT_INF(a)&&IS_NOT_INF(b)?(a+b):(IS_INF(a)&&IS_INF(b)?(a==b?a:(d?-INF:INF)):(IS_INF(a)?a:b)))

Adds two numbers, possibly inf or -inf.

When rounding down, INF+(-INF) is set to -INF, otherwise to INF.

Parameters
aThe first number to operate.
bThe second number to operate.
dTRUE if rounding down.

Definition at line 313 of file defines.h.

◆ INF_SUBS

#define INF_SUBS (   a,
  b,
 
)    (IS_NOT_INF(a)&&IS_NOT_INF(b)?(a-b):(IS_INF(a)&&IS_INF(b)?(a==b?(d?-INF:INF):a):(IS_INF(a)?a:-b)))

Substract two numbers, possibly inf or -inf.

When rounding down, INF-INF is set to -INF, otherwise to INF.

Parameters
aThe first number to operate.
bThe second number to operate.
dTRUE if rounding down.

Definition at line 326 of file defines.h.

◆ INF_POW

#define INF_POW (   a,
 
)    (IS_NOT_INF(a)?pow(a,(double)p):((a==-INF)?(((p%2)==0)?INF:-INF):INF))

Power of a number, possibly +/-inf.

Parameters
aThe number to operate.
pThe power (integer possitive).

Definition at line 336 of file defines.h.

◆ INF_SQRT

#define INF_SQRT (   a)    ((a==INF)?INF:(a<0.0?0.0:sqrt(a)));

Sqrt of a number, possibly +/-inf.

Parameters
aThe number to operate.

Definition at line 345 of file defines.h.

◆ INF_PRINT

#define INF_PRINT (   f,
 
)    (a==INF?fprintf(f,"inf"):(a==-INF?fprintf(f,"-inf"):fprintf(f,"%.12g",a)))

Prints a number (possibly +/-inf) to a file.

Parameters
fThe file.
aThe number.

Definition at line 355 of file defines.h.

◆ ANGLE_ACCURACY

#define ANGLE_ACCURACY   1e-4

Accuracy used when rounding values to +/-PI and +/-PI/2

Definition at line 362 of file defines.h.

◆ ROUND2SYMBOL

#define ROUND2SYMBOL (   a)    (fabs((a)+M_PI)<ANGLE_ACCURACY?-M_PI:(fabs((a)-M_PI)<ANGLE_ACCURACY?M_PI:(fabs((a)+M_PI_2)<ANGLE_ACCURACY?-M_PI_2:(fabs((a)-M_PI_2)<ANGLE_ACCURACY?M_PI_2:a))))

When reading values from equation files, we round them to +/-PI or to +/-PI/2 to gain accuracy. The rounding is only applied for variables that are known to be angles.

Parameters
aThe angular value to round.

Definition at line 372 of file defines.h.

◆ SYMBOL_PRINT

#define SYMBOL_PRINT (   f,
 
)    (fabs((a)+M_PI)<ANGLE_ACCURACY?fprintf(f,"-PI"):(fabs((a)-M_PI)<ANGLE_ACCURACY?fprintf(f,"PI"):(fabs((a)+M_PI_2)<ANGLE_ACCURACY?fprintf(f,"-PI/2"):(fabs((a)-M_PI_2)<ANGLE_ACCURACY?fprintf(f,"PI/2"):INF_PRINT(f,a)))))

Prints a number (possibly +/-PI, +/-PI/2, or +/-inf) to a file. Numbers close (up to 1e-4 to +/-PI or +/-PI/2 are printed symbolically). This increases accuracy when reading these values.

This type of printing is only used for variables that are known to be angles.

Parameters
fThe file.
aThe angular value to print.

Definition at line 386 of file defines.h.

◆ NEW

#define NEW (   _var,
  _n,
  _type 
)
Value:
{(_var)=(_type *)malloc(sizeof(_type)*(_n)); \
if (!(_var)) Error("Memory allocation error");}

Allocates memory space for _n elements of type _type and assigns the pointer to _var. If the memory can not be allocated, triggers an error.

Definition at line 395 of file defines.h.

◆ NEWZ

#define NEWZ (   _var,
  _n,
  _type 
)
Value:
{(_var)=(_type *)calloc(_n,sizeof(_type)); \
if (!(_var)) Error("Memory allocation error");}

Allocates memory space for _n elements of type _type and assigns the pointer to _var and initializes the space to zero. If the memory can not be allocated, triggers an error.

Definition at line 404 of file defines.h.

◆ MEM_EXPAND

#define MEM_EXPAND (   _var,
  _n,
  _type 
)
Value:
{(_var)=(_type *)realloc((void *)(_var),sizeof(_type)*(_n)); \
if (!(_var)) Error("Memory reallocation error");}

Expands a previously reserved memory space so that it can accomodate _n elements of type _type assigns the pointer to _var. The previously allocated space is pointed by _var. If the memory can not be expanded, triggers an error.

Definition at line 414 of file defines.h.

◆ MEM_DUP

#define MEM_DUP (   _var,
  _n,
  _type 
)    {(_n)*=2;MEM_EXPAND(_var,_n,_type);}

Dublicates a previously reserved memory space that accomodates _n elements of type _type. After the duplication, the pointer _var takes to a memory space that can accomodate 2*_n elements and the value of variable _n is also doubled.

Definition at line 424 of file defines.h.

◆ PRINT_VARIABLE_NAME

#define PRINT_VARIABLE_NAME (   f,
  name 
)    if (name[0]=='_') fprintf(f,"%s",&(name[1])); else fprintf(f,"%s",name)

Automatically generated variables have a name that starts with a '_'. This is done to avoid the generation of a variable with the same name of a variable defined by the user (the syntax does not allow for variables starting with a '_').
When printing the name of the variable into a file we remove the starting '_' to allow the input parser to read the output file. If this results in a duplicated variable the parser will detect it.

Definition at line 437 of file defines.h.

◆ NO_UINT

#define NO_UINT   ((unsigned int)(-1))

Used to denote an identifier that has not been initialized.

Definition at line 445 of file defines.h.

◆ MOV_AVG_UP

#define MOV_AVG_UP   0.1

Weight used for the new data when computing moving averages. In particular this constant gives the weight when increasing the average.

This is typically used to self-tune parameters.

This value must be between 0 and 1. If 0 the new data is not taken into account. If 1 only the new data is used (forgetting the previous one). A standard value for this parameter is in the range [0.01, 0.1].

Definition at line 461 of file defines.h.

◆ MOV_AVG_DOWN

#define MOV_AVG_DOWN   0.1

Weight used for the new data when computing moving averages. In particular this constant gives the weight when decreasing the average.

This is typically used to self-tune parameters.

This value must be between 0 and 1. If 0 the new data is not taken into account. If 1 only the new data is used (forgetting the previous one). A standard value for this parameter is in the range [0.01, 0.1].

Definition at line 477 of file defines.h.