clibs
Tiny but handy utility libraries for C
Macros
clibs_math.h File Reference

Common math operations. More...

Go to the source code of this file.

Macros

#define MAX(a, b)   ((a) > (b) ? (a) : (b))
 Get the maximal value between a and b.
 
#define MIN(a, b)   ((a) < (b) ? (a) : (b))
 Get the minimal value between a and b.
 
#define ABS(a)   ((a) > 0.0 ? (a) : -(a))
 Get the absolute value of a.
 
#define IS_EQUAL(a, b, epsilon)   (ABS((a) - (b)) <= (epsilon))
 Check the equality between two floating-point numbers. More...
 
#define COMPARE(a, b)   ((a) > (b) ? 1 : (a) == (b) ? 0 : -1)
 Compare two numeric data.
 
#define IS_EVEN(n)   (!((n) & 1))
 Check whether n is even.
 
#define IS_ODD(n)   ((n) & 1)
 Check whether n is odd.
 
#define SWAP(a, b)   (((a) ^= (b)), ((b) ^= (a)), ((a) ^= (b)))
 Swap between a and b. More...
 
#define RANDINT(min, max)   ((min) + rand() % ((max) - (min) + 1))
 Get a random integer bewteen min and max. More...
 
#define INF   (1.0 / 0.0)
 An infinity number.
 
#define IS_INF(n)   ((n) > FLT_MAX)
 Check whether n is an infinity number.
 
#define NEG_INF   (-1.0 / 0.0)
 A negative infinity number.
 
#define IS_NEG_INF(n)   (-(n) > FLT_MAX)
 Check whether n is a negative infinity number.
 
#define NaN   (0.0 / 0.0)
 A NaN (not a number)
 
#define IS_NaN(n)
 Check whether n is NaN (not a number) More...
 

Detailed Description

Common math operations.

Author
ByteBard

Macro Definition Documentation

◆ IS_EQUAL

#define IS_EQUAL (   a,
  b,
  epsilon 
)    (ABS((a) - (b)) <= (epsilon))

Check the equality between two floating-point numbers.

IS_EQUAL(a, b, epsilon) compares two floating-point numbers by the absolute value of the difference between a and b. The value should be smaller than epsilon

◆ IS_NaN

#define IS_NaN (   n)
Value:
((n) > (n) ? 0 : \
(n) == (n) ? 0 : \
(n) < (n) ? 0 : 1)

Check whether n is NaN (not a number)

◆ RANDINT

#define RANDINT (   min,
  max 
)    ((min) + rand() % ((max) - (min) + 1))

Get a random integer bewteen min and max.

Set a random seed by yourself.

◆ SWAP

#define SWAP (   a,
 
)    (((a) ^= (b)), ((b) ^= (a)), ((a) ^= (b)))

Swap between a and b.

a and b are integers