v2.0.2
Macros | Functions
CC3Math.h File Reference
#import <Foundation/Foundation.h>
#import <math.h>

Macros

#define CC3Cyclic(value, period)   (fmodf((value), (period)))
 
#define CC3IntIsEven(INT)   (!CC3IntIsOdd(INT))
 
#define CC3IntIsOdd(INT)   ((INT) & 1)
 
#define CC3WeightedAverage(val1, val2, weight)
 
#define CLAMP(val, min, max)
 
#define kCC3DegToRadFactor   0.0174532925199433f
 
#define SIGN(A)
 
#define XOR(exp1, exp2)   ((exp1) ? !(exp2) : (exp2))
 

Functions

static float CC3CyclicAngle (float angle)
 
static float CC3CyclicDifference (float minuend, float subtrahend, float period)
 
static bool CC3IsWithinTolerance (float value, float benchmarkValue, float aTolerance)
 
static float CC3PositiveCyclic (float value, float period)
 
static double CC3RandomDouble ()
 
static double CC3RandomDoubleBetween (double min, double max)
 
static float CC3RandomFloat ()
 
static float CC3RandomFloatBetween (float min, float max)
 
static NSUInteger CC3RandomUInt ()
 
static NSUInteger CC3RandomUIntBelow (NSUInteger max)
 
static float CC3SemiCyclicAngle (float angle)
 

Macro Definition Documentation

#define CC3Cyclic (   value,
  period 
)    (fmodf((value), (period)))

Returns the positive or negative modulo remainder of value divided by period.

#define CC3IntIsEven (   INT)    (!CC3IntIsOdd(INT))

Returns whether the specified integer value is even.

#define CC3IntIsOdd (   INT)    ((INT) & 1)

Returns whether the specified integer value is odd.

#define CC3WeightedAverage (   val1,
  val2,
  weight 
)
Value:
({ \
__typeof__(val1) __v1 = (val1); \
__v1 + (((val2) - __v1) * CLAMP((weight), 0.0, 1.0)); \
})

Returns a weighted average of the two values, where weight is between zero and one, inclusive.

#define CLAMP (   val,
  min,
  max 
)
Value:
({ \
__typeof__(val) __v = (val); \
__typeof__(min) __mn = (min); \
__typeof__(max) __mx = (max); \
__v < __mn ? __mn : (__v > __mx ? __mx : __v); \
})

Returns the value clamped to be between the min and max values.

#define kCC3DegToRadFactor   0.0174532925199433f

Conversion between degrees and radians.

#define SIGN (   A)
Value:
({ \
__typeof__(A) __a = (A); \
__a < 0 ? -1 : (__a > 0 ? 1 : 0); \
})

Returns -1, 0 or +1 if the arguement is negative, zero or positive respectively.

#define XOR (   exp1,
  exp2 
)    ((exp1) ? !(exp2) : (exp2))

Returns the logical exclusive-OR of the specified two expressions.

For logical expressions, this is more precise than the bitwise ^ operator, because it works correctly even if either exp1 or exp2 evaluates to a value that is not explicitly either YES (1) or NO (0). Furthermore, it is efficient, as it evaluates each expression only once.

Function Documentation

static float CC3CyclicAngle ( float  angle)
inlinestatic

Converts the specified angle, to an equivalent angle between +/-360 degrees.

The result may be positive or negative, but will always be between -360 and +360 degrees.

For example:

  • CC3CyclicAngle(350) will return -350
  • CC3CyclicAngle(750) will return +30
  • CC3CyclicAngle(-185) will return -185
  • CC3CyclicAngle(-535) will return -175
static float CC3CyclicDifference ( float  minuend,
float  subtrahend,
float  period 
)
inlinestatic

Returns the difference between the specified minuend and subtrahend, in terms of the minimum difference within the specified periodic cycle.

Therefore, the result may be positive or negative, but will always be between (+period/2) and (-period/2).

For example, for the numbers on a compass, the period is 360, and CC3CyclicDifference(350, 10, 360) will yield -20 (ie- the smallest change from 10 degrees to 350 degrees is -20 degrees) rather than +340 (from simple subtraction). Similarly, CC3CyclicDifference(10, 350, 360) will yield +20 (ie- the smallest change from 350 degrees to 10 degrees is +20 degrees) rather than -340 (from simple subtraction).

For angles in degrees, consider using CC3SemiCyclicAngle instead.

static bool CC3IsWithinTolerance ( float  value,
float  benchmarkValue,
float  aTolerance 
)
inlinestatic

Returns whether the specified value is as close or closer to the specified benchmark value than the specified tolerance.

If tolerance is zero, returns YES only if the two values are identical.

static float CC3PositiveCyclic ( float  value,
float  period 
)
inlinestatic

Returns the positive modulo remainder of value divided by period.

This function is similar to CC3Cyclic(), but converts a negative result into a positive value that is the same distance away from the end of the cycle as the result was below zero. In this sense, this function behaves like the numbers on a clock, and CC3Cyclic(-2.0, 12.0) will return 10.0 rather than -2.0.

static double CC3RandomDouble ( )
inlinestatic

Returns a random double between 0.0 inclusive and 1.0 exclusive.

static double CC3RandomDoubleBetween ( double  min,
double  max 
)
inlinestatic

Returns a random double between the specified min inclusive and the specified max exclusive.

static float CC3RandomFloat ( )
inlinestatic

Returns a random float between 0.0 inclusive and 1.0 exclusive.

static float CC3RandomFloatBetween ( float  min,
float  max 
)
inlinestatic

Returns a random float between the specified min inclusive and the specified max exclusive.

static NSUInteger CC3RandomUInt ( )
inlinestatic

Returns a random unsigned integer over the full unsigned interger range (between 0 and 0xFFFFFFFF).

static NSUInteger CC3RandomUIntBelow ( NSUInteger  max)
inlinestatic

Returns a random unsigned integer between 0 inclusive and the specified max exclusive.

static float CC3SemiCyclicAngle ( float  angle)
inlinestatic

Converts the specified angle, to an equivalent angle between +/-180 degrees.

The result may be positive or negative, but will always be between -180 and +180 degrees.

For example:

  • CC3SemiCyclicAngle(350) will return -10
  • CC3SemiCyclicAngle(750) will return +30
  • CC3SemiCyclicAngle(-185) will return +175
  • CC3SemiCyclicAngle(-535) will return -175