Search code examples
testingmathfloating-pointpow

How do I test binary floating point math functions


I implemented the powf(float x, float y) math function. This function is a binary floating point operation. I need to test it for correctness,but the test can't iterate over all floating point. what should I do.


Solution

  • Consider 2 questions:

    How do I test binary floating point math functions?

    Break FP values into groups:

    • Largest set: Normal values including +/- values near 1.0 and near the extremes as well as randomly selected ones.

    • Subnormals

    • Zeros: +0.0, -0.0

    • NANs

    Use at least combinations of 100,000s+ sample values from the first set (including +/-min, +/-1.0, +/-max), 1,000s from the second set (including +/-min, +/-max) and -0.0, +0.0, -NAN, +NAN.

    Additional tests for the function's edge cases.


    How do I test powf()?

    How: Test powf() against pow() for result correctness.

    Values to test against: powf() has many concerns.

    *pow(x,y) functions are notoriously difficult to code well. The error in little sub-calculations errors propagate to large final errors.

    *pow() includes expected integral results with integral value arguments. E.g. pow(2, y) is expected to be exact for all in range results. pow(10, y) is expected to be within 0.5 unit in the last place for all y in range.

    *pow() includes expected integer results with negative x.

    There is little need to test every x, y combination. Consider every x < 0, y non-whole number value leads to a NAN.

    z = powf(x,y) readily underflows to 0.0. Testing of x, y, values near a result of z == 0 needs some attention.

    z = powf(x,y) readily overflows to ∞. Testing of x, y, values near a result of z == FMT_MAX needs more attention as a slight error result in FLT_MAX vs. INF. Since overflow is so rampant with powf(x,y), this reduces the numbers of combinations needed as it is the edge that is important and larger values need light testing.