testfloat
compare expressions involving floating-point numbers
Calling Sequence
Parameters
Description
Examples
testfloat(computed, correct, bound, ...)
computed
-
test value
correct
reference value
bound
non-negative real number
testfloat verifies that the floating-point values appearing in a computed expression are within the given bound of the reference "correct" values. For example, if the expected result of a computation is the polynomial 3⁢x+2, and the actual computed result is a⁢x+b, then testfloat(a*x+b, 3*x+2, 5) will check to see that a is within 5 ulps (units in the last place) of 3 and b is within 5 ulps of 2.
The error bound is defined in terms of "units in the last place", or "ulps". Given a number x, 1 ulp relative to x is the value Float(1, -Digits+1+ilog10(x)). For more information, see ilog10.
The test value, computed, can be a single number (real or complex), a range whose endpoints are single numbers, or a list, series, univariate polynomial or univariate rational function over the complex or real numbers, or an equation whose right-hand side is one of these.
The reference value, correct, can be any of the types of expressions or values as the test value. The reference value need not be of the same type as the computed value, but it should either be 0 or, after the conversion processing described below, it should have the same number of numerics for comparison as the test value has.
Any parameters to testfloat after the third are assumed to be options controlling the type of test to be performed, and must be given in the form option = value. The recognized options are:
model
Determines whether complex numerics are treated as
single numbers (model=1; the default) or as pairs
of real numbers (model=2).
test
Specifies the type of test to perform: test=1 means
use a relative error test (this is the default,
unless correct=0); test=2 means use an absolute
error test (this is implied if correct=0).
digits
Specifies the value of Digits to be used when
calculating the size of a ulp; must be a positive
integer.
For a comparison of a single real number with a reference value (or a single complex number with a reference value and the option model=2 not given), the output will be either the single value true or the list [false, <size of error>, ulps], where <size of error> is rounded to 3 digits.
For tests involving more than one floating-point value in the parameter computed, the output will be a list, each of whose elements is in the above form.
For series comparisons, the point of expansion of the series is checked, as well as each of the coefficients.
For polynomial comparisons, if there is a common subexpression of the form b⁢x+c (that is, the polynomial is expressed as a sum of powers of b⁢x+c), then this subexpression is first replaced by a single name. In any case, the polynomial is then converted to a series for comparison, and the values of b and c are also checked.
Except in the most elementary case, all expressions are converted to lists before the actual comparison is done.
If computed is not just a single float (or a single complex float with model=2 not given), then, after conversion to lists, the test value and the reference value lists should have the same length, or the reference value should be 0. If the reference value is not 0 and the lists do not have the same length, the shorter one will be extended by infinity, producing the corresponding output value [false, infinity, ulps].
If correct=0, an absolute error check is performed on each of the numerics appearing in the expression computed.
Digits≔3:
testfloat⁡1.23,1.25,2
true
testfloat⁡1.23,1.25,1
false,2.,ulps
testfloat⁡1.23,3.45+6.78⁢I,1.22+0.0001⁢I,3.46+6.76⁢I,2
true,true
testfloat⁡1.23,3.45+6.78⁢I,1.22+0.0001⁢I,3.46+6.76⁢I,2,model=2
true,false,100.,ulps ,true,true
series⁡exp⁡x,x=0.123,3
1.13+1.13⁢x−0.123+0.565⁢x−0.1232+O⁡x−0.1233
testfloat⁡,series⁡1.23+1.13⁢x−0.123+0.567⁢x−0.1232+O⁡x−0.1233,x=0.123,3,1
false,10.,ulps ,true,true,true,false,2.,ulps ,true,true,true,true
testfloat⁡x=0.00001,y=0.025,x=0.00099,y=0.010,1,digits=2,test=2
true,false,1.5,ulps
testfloat⁡12.3..45.6,12.6..45.5,5
See Also
comparray
Digits
fnormal
series
Download Help Document