assertApprox

Assert that two floating point values are approximately equal using an epsilon value.

  1. void assertApprox(A value, B target, long ulps, string message, string file, size_t line)
  2. void assertApprox(A value, B target, double epsilon, string message, string file, size_t line)
    void
    assertApprox
    (
    A
    B
    )
    (,,
    double epsilon
    ,
    string message = "Failed asserting approximately equal"
    ,
    string file = __FILE__
    ,
    size_t line = __LINE__
    )
    if (
    isFloatingPoint!(CommonType!(A, B))
    )

Parameters

value A

The value used during the assertion.

target B

The target value.

epsilon double

An epsilon value to be used as the maximum absolute and relative error in the comparison.

message string

The error message to display.

file string

The file name where the error occurred. The value is added automatically at the call site.

line size_t

The line where the error occurred. The value is added automatically at the call site.

Throws

DUnitAssertError if the assertation fails.

Examples

float smallestFloatSubnormal = float.min_normal * float.epsilon;
smallestFloatSubnormal.assertApprox(-smallestFloatSubnormal, 0.00001);

double smallestDoubleSubnormal = double.min_normal * double.epsilon;
smallestDoubleSubnormal.assertApprox(-smallestDoubleSubnormal, 0.00001);

0.0f.assertApprox(-0.0f, 0.00001);
(-0.0f).assertApprox(0.0f, 0.00001);
0.0.assertApprox(-0.0, 0.00001);
(-0.0).assertApprox(0.0, 0.00001);
2.0f.assertApprox(1.99f, 0.01);
1.99f.assertApprox(2.0f, 0.01);
2.0.assertApprox(1.99, 0.01);
1.99.assertApprox(2.0, 0.01);

// The following tests pass but are open for debate whether or not they should.
float.max.assertApprox(float.infinity, 0.00001);
float.infinity.assertApprox(float.max, 0.00001);
double.infinity.assertApprox(double.max, 0.00001);
double.max.assertApprox(double.infinity, 0.00001);
float.nan.assertApprox(float.nan, 0.00001);
double.nan.assertApprox(double.nan, 0.00001);

// Assert a DUnitAssertError is thrown if assertApprox fails.
10f.assertApprox(0f, 0.00001).assertThrow!(DUnitAssertError)("Failed asserting approximately equal");

See Also

Meta