TTS v4.1.0
The Tiny Test System
Loading...
Searching...
No Matches
Precision Tests Macros

Macros for performing tests taking floating-point precision into account. More...

Macros

#define TTS_ABSOLUTE_EQUAL(L, R, N, ...)
 Checks that two values are at most N apart.
#define TTS_IEEE_EQUAL(L, R, ...)
 Checks that two values are 0 ULP apart, that is equal or both NaN.
#define TTS_RELATIVE_EQUAL(L, R, N, ...)
 Checks that two values are within a combined relative and absolute tolerance N.
#define TTS_ULP_EQUAL(L, R, N, ...)
 Checks that two values are at most N ULP apart.
#define TTS_ULP_RANGE_CHECK(Producer, RefType, NewType, RefFunc, NewFunc, ...)
 Checks a function against a reference over a range of inputs, within a ULP distance.

Macro Definition Documentation

◆ TTS_ABSOLUTE_EQUAL

#define TTS_ABSOLUTE_EQUAL ( L,
R,
N,
... )

#include <tts/test/precision.hpp>

Checks that two values are at most N apart.

Measures the distance abs(L - R) with tts::absolute_check.

L and R must be of the same type, or the check does not compile.

Parameters
L,RValues to compare.
NLargest distance accepted.
...REQUIRED ends the run when the check fails.

Example

#define TTS_MAIN // No need for main()
#include <tts/tts.hpp>
TTS_CASE("Absolute distance")
{
TTS_ABSOLUTE_EQUAL(-2., 2., 4.);
TTS_ABSOLUTE_EQUAL(static_cast<long long>('A'), 80LL, 15, REQUIRED);
};

◆ TTS_IEEE_EQUAL

#define TTS_IEEE_EQUAL ( L,
R,
... )

#include <tts/test/precision.hpp>

Checks that two values are 0 ULP apart, that is equal or both NaN.

Compares with tts::ieee_check, which uses operator== and counts two NaNs as equal, the 0 ULP case of TTS_ULP_EQUAL without measuring a distance.

Parameters
L,RValues to compare.
...REQUIRED ends the run when the check fails.

Example

#define TTS_MAIN // No need for main()
#include <tts/tts.hpp>
TTS_CASE("ULP distance")
{
float x = std::numeric_limits<float>::quiet_NaN();
TTS_IEEE_EQUAL(1.f, 1.f);
TTS_IEEE_EQUAL(2., 2.);
TTS_IEEE_EQUAL(65, 'A');
};

◆ TTS_RELATIVE_EQUAL

#define TTS_RELATIVE_EQUAL ( L,
R,
N,
... )

#include <tts/test/precision.hpp>

Checks that two values are within a combined relative and absolute tolerance N.

Passes if abs(L - R) <= N * max(1, abs(L), abs(R)), the combined tolerance test of tts::relative_check. N acts as an absolute tolerance where both values are at most 1 in magnitude, and as a relative one elsewhere. Two NaNs or two equal infinities pass, and any other NaN or infinity fails.

N is a ratio, so 0.005 accepts half a percent. On an arithmetic type, a finite N of 1 or more fails the check with a message asking for N divided by a hundred, since TTS 3 took a percentage.

L and R must be of the same type, or the check does not compile.

Parameters
L,RValues to compare.
NTolerance, as a ratio.
...REQUIRED ends the run when the check fails.

Example

#define TTS_MAIN // No need for main()
#include <tts/tts.hpp>
TTS_CASE("Relative distance")
{
TTS_RELATIVE_EQUAL(42.f, 42.f, 0);
TTS_RELATIVE_EQUAL(static_cast<long long>('A'), 80LL, 0.1875);
TTS_RELATIVE_EQUAL(1., 2., 0.5);
TTS_RELATIVE_EQUAL(1, 10, 0.9, REQUIRED);
};

◆ TTS_ULP_EQUAL

#define TTS_ULP_EQUAL ( L,
R,
N,
... )

#include <tts/test/precision.hpp>

Checks that two values are at most N ULP apart.

Measures the distance with tts::ulp_check. See the ULP rationale for how the distance is measured.

L and R must be of the same type, or the check does not compile. The distance would be measured after promotion to the common type, in the ULP of that type and not of the type under test.

Parameters
L,RValues to compare.
NLargest distance accepted, in ULP.
...REQUIRED ends the run when the check fails.

Example

#define TTS_MAIN // No need for main()
#include <tts/tts.hpp>
TTS_CASE("ULP distance")
{
float a {1};
float eps = std::numeric_limits<float>::epsilon();
float qnan = std::numeric_limits<float>::quiet_NaN();
float inf = std::numeric_limits<float>::infinity();
float minf = -inf;
TTS_ULP_EQUAL(a, a, 0.);
TTS_ULP_EQUAL(a, qnan, inf);
TTS_ULP_EQUAL(qnan, qnan, 0.);
TTS_ULP_EQUAL(a, inf, inf);
TTS_ULP_EQUAL(inf, inf, inf);
TTS_ULP_EQUAL(minf, minf, inf);
TTS_ULP_EQUAL(a, a - eps, 1);
TTS_ULP_EQUAL(a, a + eps, 0.5);
TTS_ULP_EQUAL(a, a + 3 * eps, 1.5, REQUIRED);
};

◆ TTS_ULP_RANGE_CHECK

#define TTS_ULP_RANGE_CHECK ( Producer,
RefType,
NewType,
RefFunc,
NewFunc,
... )

#include <tts/test/ranges.hpp>

Checks a function against a reference over a range of inputs, within a ULP distance.

Generates inputs with Producer, calls RefFunc and NewFunc on each of them, and records the ULP distance that tts::ulp_check measures between the two results. The distances are gathered in a histogram, and the check passes if none exceeds the bound given after NewFunc.

Arguments

For a unary function, RefType and NewType are its argument type.

For a function with several arguments, RefType and NewType are tts::types lists between parentheses, in call order. Producer is either a list between parentheses of a generator per argument, or a single generator that each argument uses on its own. A single tts::sample(4096) for two arguments thus makes 16777216 calls.

Number of calls

The check runs every combination of the values its arguments take. tts::sweep gives every value in its range, and tts::sample its n values, or every value of a range that holds fewer.

The other generators together give the smallest number of values per argument whose combinations reach 4096: 4096 values for a single argument, and 64 for each of two. A generator receives the index of each value and the number of values of its argument.

The number of calls is the product of these counts, multiplied by N under --loop=N. A check whose product does not fit in std::size_t fails without running. Memory does not grow with the number of calls, so tts::sweep checks each float but NaN, once by default.

Range options

--valmin and --valmax narrow the values of tts::sweep, tts::sample, and tts::randoms in every check of the run to the ones between them, for an argument of an integer or floating-point type. A check prints a note when they narrow an argument, and passes without a call when they leave an argument no value.

Threads

Compiled with OpenMP, a check whose generators are all tts::sweep or tts::sample splits its calls between the threads of OpenMP, whose number OMP_NUM_THREADS sets. RefFunc and NewFunc are then called from several threads at once, and neither the report nor what they draw through tts::random_value depends on the number of threads.

Result types

RefFunc returns the element type that the result of NewFunc holds, or the check does not compile. A reference computed in double for a float function thus returns its result cast to float.

NewType may hold several values of RefType, as a specialization of tts::adapter describes.

A result is compared member by member, each member with its own histogram, when tts::adapter has no specialization for its type and that type is:

  • std::pair or std::tuple,
  • or a type that specializes std::tuple_size and provides a get found by argument-dependent lookup.

The check then takes a bound for all members or a bound for each member.

Report

A check that fails prints its histogram, and --verbose prints it for every check. Each bucket shows the first input whose distance falls in it. When NewType holds several values, that input is marked between stars within its group. --ulpmax replaces the bounds of every check of the run.

Parameters
ProducerGenerator of the inputs, or a generator per argument between parentheses.
RefTypeArgument type of RefFunc, or a tts::types list of them.
NewTypeArgument type of NewFunc, or a tts::types list of them.
RefFuncFunction whose results are the reference.
NewFuncFunction compared to the reference.
...Largest ULP distance accepted, or a distance per member of a tuple result.

Example

#define TTS_MAIN // No need for main()
#include <tts/tts.hpp>
#include <cmath>
#include <utility>
float my_ldexp(float x, int n)
{
return x * std::exp2(static_cast<float>(n));
}
TTS_CASE("Check my_ldexp against std::ldexp")
{
auto std_ldexp = [](float x, int n) { return std::ldexp(x, n); };
// One generator per argument: every x is checked with every n from -32 to 31
std_ldexp,
my_ldexp,
0.);
};
std::pair<float, int> my_frexp(float x)
{
int e = std::ilogb(x) + 1;
return {std::scalbn(x, -e), e};
}
TTS_CASE("Check my_frexp against std::frexp")
{
auto std_frexp = [](float x)
{
int e = 0;
float m = std::frexp(x, &e);
return std::pair {m, e};
};
// A tuple result is compared member by member, with one bound per member
TTS_ULP_RANGE_CHECK(tts::between(1.f, 1000.f), float, float, std_frexp, my_frexp, 0., 0.);
};
See also
tts::adapter