clibs
Tiny but handy utility libraries for C
Loading...
Searching...
No Matches
clibs_math.h File Reference

Common math operations. More...

Go to the source code of this file.

Macros

#define MAX(a, b)
 Get the maximal value between a and b.
#define MIN(a, b)
 Get the minimal value between a and b.
#define ABS(a)
 Get the absolute value of a.
#define IS_EQUAL(a, b, epsilon)
 Check the equality between two floating-point numbers.
#define COMPARE(a, b)
 Compare two numeric data.
#define IS_EVEN(n)
 Check whether n is even.
#define IS_ODD(n)
 Check whether n is odd.
#define SWAP(a, b)
 Swap between a and b.
#define RANDINT(min, max)
 Get a random integer bewteen min and max.
#define INF   (1.0 / 0.0)
 An infinity number.
#define IS_INF(n)
 Check whether n is an infinity number.
#define NEG_INF   (-1.0 / 0.0)
 A negative infinity number.
#define IS_NEG_INF(n)
 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)

Detailed Description

Common math operations.

Author
ByteBard

Macro Definition Documentation

◆ ABS

#define ABS ( a)
Value:
((a) > 0.0 ? (a) : -(a))

Get the absolute value of a.

◆ COMPARE

#define COMPARE ( a,
b )
Value:
((a) > (b) ? 1 : (a) == (b) ? 0 : -1)

Compare two numeric data.

◆ IS_EQUAL

#define IS_EQUAL ( a,
b,
epsilon )
Value:
(ABS((a) - (b)) <= (epsilon))
#define ABS(a)
Get the absolute value of a.
Definition clibs_math.h:27

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_EVEN

#define IS_EVEN ( n)
Value:
(!((n) & 1))

Check whether n is even.

◆ IS_INF

#define IS_INF ( n)
Value:
((n) > FLT_MAX)

Check whether n is an infinity number.

◆ 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)

◆ IS_NEG_INF

#define IS_NEG_INF ( n)
Value:
(-(n) > FLT_MAX)

Check whether n is a negative infinity number.

◆ IS_ODD

#define IS_ODD ( n)
Value:
((n) & 1)

Check whether n is odd.

◆ MAX

#define MAX ( a,
b )
Value:
((a) > (b) ? (a) : (b))

Get the maximal value between a and b.

◆ MIN

#define MIN ( a,
b )
Value:
((a) < (b) ? (a) : (b))

Get the minimal value between a and b.

◆ RANDINT

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

Get a random integer bewteen min and max.

Set a random seed by yourself.

◆ SWAP

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

Swap between a and b.

a and b are integers