Overview
The rosemath_defs.h header defines library link symbols, plus several common mathematical constants and functions for comparing two floating point values for equality within an epsilon factor. The vector and transformation code also define some constant arrays.
Math Constants
#define ROSE_EPSILON 1e-15 #define ROSE_PI 3.14159265358979323846264338327950288419716939937511 #define ROSE_PISQ 9.8690440108935861883449099987615113531369940724079 #define ROSE_1_OVER_PI 0.31830988618379067153776752674502872406891929148091 #define ROSE_PI_OVER_2 1.570796326794897 #define ROSE_PI_OVER_180 (ROSE_PI/180.0) /* 0.01745329252 */ #define ROSE_180_OVER_PI (180.0/ROSE_PI) /* 57.29577951 */ #define ROSE_E 2.71828182845904523536028747135266249775724709369996 #define ROSE_ESQ 7.38905609893065022723042746057500781318031557055185 #define ROSE_1_OVER_E 0.36787944117144232159552377016146086744581113103177 #define ROSE_SQRT_2 1.41421356237309504880168872420969807856967187537695 #define ROSE_CUBERT_2 1.25992104989487316476721060727822835057025146470151 #define ROSE_SQRT_3 1.73205080756887729352744634150587236694280525381039 #define ROSE_CUBERT_3 1.44224957030740838232163831078010958839186925349935
These macros define mathematical constants for easier reuse. The constants for π, e, and roots are taken from CRC Standard Mathematical Tables, 27th edition. Constants for 180/π and π/180 are from Dwight, Integrals and other Math Data, 4th ed. The precision of the constants is determined by the native floating point representation of the machine, typically around 15 digits.
ROSE_EPSILON
gives the default uncertainty for floating point comparisons.ROSE_PI
macro is πROSE_PISQ
macro is π2ROSE_1_OVER_PI
macro is 1/πROSE_PI_OVER_2
macro is π/2ROSE_PI_OVER_180
macro is π/180ROSE_180_OVER_PI
macro is 180/πROSE_E
macro is e, the natural log base.ROSE_ESQ
macro is e2ROSE_1_OVER_E
macro is 1/eROSE_SQRT_2
macro is 2-2, the square root of two.ROSE_CUBERT_2
macro is 2-3, the cube root of two.ROSE_SQRT_3
macro is 3-2, the square root of three.ROSE_CUBERT_3
macro is 3-3, the cube root of three.
Some examples are below:
double perimeter = 2.0 * ROSE_PI * radius; double area = ROSE_PI * radius * radius; // convert from radians to degrees double degrees = rads * ROSE_180_OVER_PI; // convert from degrees to radians double rads = degrees * ROSE_PI_OVER_180; // more general unit conversion function does the same thing rads = rose_get_measure_as_unit(degrees, roseunit_deg, roseunit_rad); // some unit vectors at 45 degrees in both 2D and 3D RoseDirection vec_3d ( 1.0/ROSE_SQRT_3, 1.0/ROSE_SQRT_3, 1.0/ROSE_SQRT_3 ); RoseDirection2D vec_2d (1.0/ROSE_SQRT_2, 1.0/ROSE_SQRT_2);
Comparison Macros
#define ROSE_FLOAT_IS_EQUAL(a,b) (rose_is_epsilon_equal((a),(b),ROSE_EPSILON)) #define ROSE_FLOAT_NOT_EQUAL(a,b) (!rose_is_epsilon_equal((a),(b),ROSE_EPSILON)) #define ROSE_FLOAT_IS_ZERO(a) (rose_is_epsilon_zero((a),ROSE_EPSILON)) #define ROSE_FLOAT_NOT_ZERO(a) (rose_is_epsilon_nonzero((a),ROSE_EPSILON)) #define ROSE_FLOAT_IS_NULL(a) ((a) == ROSE_NULL_REAL) #define ROSE_FLOAT_NOT_NULL(a) ((a) != ROSE_NULL_REAL)
These macros compare floating point values with a reasonable
default epsilon factor. You can do customized comparisons by calling
the underlying functions directly. The ROSE_NULL_REAL
null
value is only equal to itself when testing for equality, and returns
false when passed to both is_zero and is_non_zero.
ROSE_FLOAT_IS_EQUAL
() returns true if two floating point values are withinROSE_EPSILON
of each other. The rose_is_epsilon_equal() function is a more general version. Note thatROSE_NULL_REAL
is only equal to itself.ROSE_FLOAT_NOT_EQUAL
() is just the opposite ofROSE_FLOAT_IS_EQUAL
()ROSE_FLOAT_IS_ZERO
() returns true if a floating point value is withinROSE_EPSILON
of zero. AROSE_NULL_REAL
is never zero. See rose_is_epsilon_zero() for a more general versionROSE_FLOAT_NOT_ZERO
() is the opposite ofROSE_FLOAT_ZERO
. See rose_is_epsilon_nonzero() for a more general version.ROSE_FLOAT_IS_NULL
() returns true if a floating point value is equal toROSE_NULL_REAL
.ROSE_FLOAT_NOT_NULL
() is the opposite ofROSE_FLOAT_IS_NULL
().
Some examples are below:
double a, b; if (ROSE_FLOAT_IS_EQUAL(a, 5.0)) { printf ("I am five!\n"); } if (ROSE_FLOAT_NOT_EQUAL(a, b)) { some_important_function(b); } if (ROSE_FLOAT_IS_NULL(a)) a = 250.99;
rose_is_epsilon_equal()
int rose_is_epsilon_equal( double a, double b, double epsilon );
The rose_is_epsilon_equal() function compares two floating
point numbers and returns true if they are within an epsilon value of
each other. This function always returns false when
testing ROSE_NULL_REAL
against anything other than null.
rose_is_epsilon_nonzero()
int rose_is_epsilon_nonzero( double val, double epsilon );
The rose_is_epsilon_nonzero() function returns true if a
floating point number is an epsilon value or more from zero. This
function always returns false when passed ROSE_NULL_REAL
.
rose_is_epsilon_zero()
int rose_is_epsilon_zero( double val, double epsilon );
The rose_is_epsilon_zero() function returns true if a
floating point number is less than an epsilon value from zero. This
function always returns false when passed ROSE_NULL_REAL
.