assertApprox

Assert that two floating point values are approximately equal.

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

Parameters

value
Type: A

The value used during the assertion.

target
Type: B

The target value.

ulps
Type: long

The maximum space between two approximately equal floating point numbers measured in units of least precision. A higher number means more approximation.

message
Type: string

The error message to display.

file
Type: string

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

line
Type: size_t

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

Throws

DUnitAssertError if the assertation fails.

Examples

1 float smallestFloatSubnormal = float.min_normal * float.epsilon;
2 smallestFloatSubnormal.assertApprox(-smallestFloatSubnormal);
3 
4 double smallestDoubleSubnormal = double.min_normal * double.epsilon;
5 smallestDoubleSubnormal.assertApprox(-smallestDoubleSubnormal);
6 
7 0.0f.assertApprox(-0.0f);
8 (-0.0f).assertApprox(0.0f);
9 0.0.assertApprox(-0.0);
10 (-0.0).assertApprox(0.0);
11 2.0f.assertApprox(1.999999f);
12 1.999999f.assertApprox(2.0f);
13 2.0.assertApprox(1.999999999999999);
14 1.999999999999999.assertApprox(2.0);
15 
16 // The following tests pass but are open for debate whether or not they should.
17 float.max.assertApprox(float.infinity);
18 float.infinity.assertApprox(float.max);
19 double.infinity.assertApprox(double.max);
20 double.max.assertApprox(double.infinity);
21 float.nan.assertApprox(float.nan);
22 double.nan.assertApprox(double.nan);
23 
24 // Assert a DUnitAssertError is thrown if assertApprox fails.
25 10f.assertApprox(0f).assertThrow!(DUnitAssertError)("Failed asserting approximately equal");

See Also

Meta