Skip to content

Floating Point Extensions

Thomas M. Hermann edited this page Oct 10, 2012 · 1 revision

The floating point extensions to lisp-unit are presented below. The original lisp-unit has been extended with predicate functions and assertions to support numerical testing. The predicates can be used with assert-equality or with the corresponding assertions. All extensions are implemented using generic functions and consequently can be specialized for user classes.

Predicates and Assertions

The internal default value of epsilon is is twice the appropriate float epsilon (i.e. 2*single-float-epsilon or 2*double-float-epsilon). Epsilon can be controlled at a lexical level using the package variable *epsilon*. If *epsilon* is set to nil, the internal epsilon values are used. This is the default value of epsilon.

(float-equal data1 data2 [epsilon])
(assert-float-equal value form [form1 form2 ...])

Return true if the relative error between data1 and data2 is less than epsilon. The assertion tallies the failure if value is not equal to the result returned from form, using float-equal.

(sigfig-equal float1 float2 [significant-figures])
(assert-sigfig-equal value form [form1 form2 ...])

Return true if float1 and float2 are equal to the specified significant-figures. The default value of significant figures is 4, set by the global variable *significant-figures*. The test tallies the failure if value is not equal to the result returned from form, using sigfig-equal.

(norm-equal data1 data2 [epsilon] [measure])
(assert-norm-equal value form [form1 form2 ...])

Return true if the relative error norm between data1 and data2 is less than epsilon.

GSLL Specific Predicates and Assertions

(number-equal number1 number2 [epsilon] [type-eq-p])
(assert-number-equal value form [form1 form2 ...])

Return true if the error between number1 and number2 is less than epsilon. The numbers must be the same type unless type-eq-p is t. For the comparison, both numbers are coerced to (complex double-float) and passed to float-equal. The test tallies the failure if value is not equal to the result returned from form, using number-equal.

(numerical-equal result1 result2 [:test])
(assert-numerical-equal value form [form1 form2 ...])

Return true if the numerical result1 is equal to result2 as defined by :test. The results can be numbers, sequences, nested sequences and arrays. The test tallies the failure if value is not equal to the result returned from form, using numerical-equal. In general, test must be a function that accepts 2 arguments and returns true or false.

Floating point functions

The floating point functions can be specialized for user data types and aid in writing user specific predicates.

(default-epsilon value)

Return the default epsilon for value.

(relative-error exact approximate)

Return the relative error.

(sumsq data)

Return the scaling parameter and the sum of the squares of the data.

(sump data p)

Return the scaling parameter and the sum of the powers of p of the data.

(norm data [measure])

Return the element-wise norm of the data.

(relative-error-norm exact approximate [measure])

Return the relative error norm.