SDL: testautomation_math: do relative comparison + more precise correct trigonometric values (f1f9e)

From f1f9e2712881b035d65a6703cd46b2a53d9a3d57 Mon Sep 17 00:00:00 2001
From: Anonymous Maarten <[EMAIL REDACTED]>
Date: Sat, 14 Oct 2023 03:23:01 +0200
Subject: [PATCH] testautomation_math: do relative comparison + more precise
 correct trigonometric values

If the magnitude of the expected result is small, then we can safely
assume that the actual calculated result matches it to 10 decimal
places.

However, if the magnitude is very large, as it is for some of our exp()
tests, then 10 decimal places represents an unrealistically high level
of precision, for example 24 decimal digits for the test that is
expected to return approximately 6.6e14. IEEE 754 floating point only
has a precision of about 16 decimal digits, causing test failure on
x86 compilers that use an i387 80-bit extended-precision register for
the result and therefore get a slightly different answer.

To avoid this, scale the required precision with the magnitude of the
expected result, so that we accept a maximum error of either 10 decimal
places or 1 part in 1e10, whichever is greater.

[smcv: Added longer commit message explaining why we need this]
(cherry picked from commit 880c69392ae10c726fc97f17b6e5e2173f70b62f)
---
 test/testautomation_math.c | 201 ++++++++++++++++++++-----------------
 1 file changed, 108 insertions(+), 93 deletions(-)

diff --git a/test/testautomation_math.c b/test/testautomation_math.c
index 92cd6acc7f9c..200b6fd4f4e4 100644
--- a/test/testautomation_math.c
+++ b/test/testautomation_math.c
@@ -103,8 +103,15 @@ helper_dtod_inexact(const char *func_name, d_to_d_func func,
     Uint32 i;
     for (i = 0; i < cases_size; i++) {
         const double result = func(cases[i].input);
-        SDLTest_AssertCheck(result >= cases[i].expected - EPSILON &&
-                                result <= cases[i].expected + EPSILON,
+        double diff = result - cases[i].expected;
+        double max_err = (cases[i].expected + 1.) * EPSILON;
+        if (diff < 0) {
+            diff = -diff;
+        }
+        if (max_err < 0) {
+            max_err = -max_err;
+        }
+        SDLTest_AssertCheck(diff <= max_err,
                             "%s(%f), expected [%f,%f], got %f",
                             func_name,
                             cases[i].input,
@@ -158,8 +165,16 @@ helper_ddtod_inexact(const char *func_name, dd_to_d_func func,
     Uint32 i;
     for (i = 0; i < cases_size; i++) {
         const double result = func(cases[i].x_input, cases[i].y_input);
-        SDLTest_AssertCheck(result >= cases[i].expected - EPSILON &&
-                                result <= cases[i].expected + EPSILON,
+        double diff = result - cases[i].expected;
+        double max_err = (cases[i].expected + 1.) * EPSILON;
+        if (diff < 0) {
+            diff = -diff;
+        }
+        if (max_err < 0) {
+            max_err = -max_err;
+        }
+
+        SDLTest_AssertCheck(diff <= max_err,
                             "%s(%f,%f), expected [%f,%f], got %f",
                             func_name,
                             cases[i].x_input, cases[i].y_input,
@@ -1674,7 +1689,7 @@ pow_regularCases(void *args)
         { 39.23, -1.5, 0.0040697950366865498147972424192175822099670767784118652343750 },
         { 478.972, 12.125, 315326359630449587856007411793920.0 }
     };
-    return helper_ddtod("Pow", SDL_pow, regular_cases, SDL_arraysize(regular_cases));
+    return helper_ddtod_inexact("Pow", SDL_pow, regular_cases, SDL_arraysize(regular_cases));
 }
 
 /**
@@ -2002,24 +2017,24 @@ static int
 cos_precisionTest(void *args)
 {
     const d_to_d precision_cases[] = {
-        { SDL_PI_D * 1.0 / 10.0, 0.9510565162 },
-        { SDL_PI_D * 2.0 / 10.0, 0.8090169943 },
-        { SDL_PI_D * 3.0 / 10.0, 0.5877852522 },
-        { SDL_PI_D * 4.0 / 10.0, 0.3090169943 },
+        { SDL_PI_D * 1.0 / 10.0, 0.9510565162951535 },
+        { SDL_PI_D * 2.0 / 10.0, 0.8090169943749475 },
+        { SDL_PI_D * 3.0 / 10.0, 0.5877852522924731 },
+        { SDL_PI_D * 4.0 / 10.0, 0.30901699437494745 },
         { SDL_PI_D * 5.0 / 10.0, 0.0 },
-        { SDL_PI_D * 6.0 / 10.0, -0.3090169943 },
-        { SDL_PI_D * 7.0 / 10.0, -0.5877852522 },
-        { SDL_PI_D * 8.0 / 10.0, -0.8090169943 },
-        { SDL_PI_D * 9.0 / 10.0, -0.9510565162 },
-        { SDL_PI_D * -1.0 / 10.0, 0.9510565162 },
-        { SDL_PI_D * -2.0 / 10.0, 0.8090169943 },
-        { SDL_PI_D * -3.0 / 10.0, 0.5877852522 },
-        { SDL_PI_D * -4.0 / 10.0, 0.3090169943 },
+        { SDL_PI_D * 6.0 / 10.0, -0.30901699437494734 },
+        { SDL_PI_D * 7.0 / 10.0, -0.587785252292473 },
+        { SDL_PI_D * 8.0 / 10.0, -0.8090169943749473 },
+        { SDL_PI_D * 9.0 / 10.0, -0.9510565162951535 },
+        { SDL_PI_D * -1.0 / 10.0, 0.9510565162951535 },
+        { SDL_PI_D * -2.0 / 10.0, 0.8090169943749475 },
+        { SDL_PI_D * -3.0 / 10.0, 0.5877852522924731 },
+        { SDL_PI_D * -4.0 / 10.0, 0.30901699437494745 },
         { SDL_PI_D * -5.0 / 10.0, 0.0 },
-        { SDL_PI_D * -6.0 / 10.0, -0.3090169943 },
-        { SDL_PI_D * -7.0 / 10.0, -0.5877852522 },
-        { SDL_PI_D * -8.0 / 10.0, -0.8090169943 },
-        { SDL_PI_D * -9.0 / 10.0, -0.9510565162 }
+        { SDL_PI_D * -6.0 / 10.0, -0.30901699437494734 },
+        { SDL_PI_D * -7.0 / 10.0, -0.587785252292473 },
+        { SDL_PI_D * -8.0 / 10.0, -0.8090169943749473 },
+        { SDL_PI_D * -9.0 / 10.0, -0.9510565162951535 }
     };
     return helper_dtod_inexact("Cos", SDL_cos, precision_cases, SDL_arraysize(precision_cases));
 }
@@ -2120,23 +2135,23 @@ static int
 sin_precisionTest(void *args)
 {
     const d_to_d precision_cases[] = {
-        { SDL_PI_D * 1.0 / 10.0, 0.3090169943 },
-        { SDL_PI_D * 2.0 / 10.0, 0.5877852522 },
-        { SDL_PI_D * 3.0 / 10.0, 0.8090169943 },
-        { SDL_PI_D * 4.0 / 10.0, 0.9510565162 },
-        { SDL_PI_D * 6.0 / 10.0, 0.9510565162 },
-        { SDL_PI_D * 7.0 / 10.0, 0.8090169943 },
-        { SDL_PI_D * 8.0 / 10.0, 0.5877852522 },
-        { SDL_PI_D * 9.0 / 10.0, 0.3090169943 },
+        { SDL_PI_D * 1.0 / 10.0, 0.3090169943749474 },
+        { SDL_PI_D * 2.0 / 10.0, 0.5877852522924731 },
+        { SDL_PI_D * 3.0 / 10.0, 0.8090169943749475 },
+        { SDL_PI_D * 4.0 / 10.0, 0.9510565162951535 },
+        { SDL_PI_D * 6.0 / 10.0, 0.9510565162951536 },
+        { SDL_PI_D * 7.0 / 10.0, 0.8090169943749475 },
+        { SDL_PI_D * 8.0 / 10.0, 0.5877852522924732 },
+        { SDL_PI_D * 9.0 / 10.0, 0.3090169943749475 },
         { SDL_PI_D, 0.0 },
-        { SDL_PI_D * -1.0 / 10.0, -0.3090169943 },
-        { SDL_PI_D * -2.0 / 10.0, -0.5877852522 },
-        { SDL_PI_D * -3.0 / 10.0, -0.8090169943 },
-        { SDL_PI_D * -4.0 / 10.0, -0.9510565162 },
-        { SDL_PI_D * -6.0 / 10.0, -0.9510565162 },
-        { SDL_PI_D * -7.0 / 10.0, -0.8090169943 },
-        { SDL_PI_D * -8.0 / 10.0, -0.5877852522 },
-        { SDL_PI_D * -9.0 / 10.0, -0.3090169943 },
+        { SDL_PI_D * -1.0 / 10.0, -0.3090169943749474 },
+        { SDL_PI_D * -2.0 / 10.0, -0.5877852522924731 },
+        { SDL_PI_D * -3.0 / 10.0, -0.8090169943749475 },
+        { SDL_PI_D * -4.0 / 10.0, -0.9510565162951535 },
+        { SDL_PI_D * -6.0 / 10.0, -0.9510565162951536 },
+        { SDL_PI_D * -7.0 / 10.0, -0.8090169943749475 },
+        { SDL_PI_D * -8.0 / 10.0, -0.5877852522924732 },
+        { SDL_PI_D * -9.0 / 10.0, -0.3090169943749475 },
         { -SDL_PI_D, 0.0 },
     };
     return helper_dtod_inexact("Sin", SDL_sin, precision_cases, SDL_arraysize(precision_cases));
@@ -2236,26 +2251,26 @@ static int
 tan_precisionTest(void *args)
 {
     const d_to_d precision_cases[] = {
-        { SDL_PI_D * 1.0 / 11.0, 0.2936264929 },
-        { SDL_PI_D * 2.0 / 11.0, 0.6426609771 },
-        { SDL_PI_D * 3.0 / 11.0, 1.1540615205 },
-        { SDL_PI_D * 4.0 / 11.0, 2.1896945629 },
-        { SDL_PI_D * 5.0 / 11.0, 6.9551527717 },
-        { SDL_PI_D * 6.0 / 11.0, -6.9551527717 },
-        { SDL_PI_D * 7.0 / 11.0, -2.1896945629 },
-        { SDL_PI_D * 8.0 / 11.0, -1.1540615205 },
-        { SDL_PI_D * 9.0 / 11.0, -0.6426609771 },
-        { SDL_PI_D * 10.0 / 11.0, -0.2936264929 },
-        { SDL_PI_D * -1.0 / 11.0, -0.2936264929 },
-        { SDL_PI_D * -2.0 / 11.0, -0.6426609771 },
-        { SDL_PI_D * -3.0 / 11.0, -1.1540615205 },
-        { SDL_PI_D * -4.0 / 11.0, -2.1896945629 },
-        { SDL_PI_D * -5.0 / 11.0, -6.9551527717 },
-        { SDL_PI_D * -6.0 / 11.0, 6.9551527717 },
-        { SDL_PI_D * -7.0 / 11.0, 2.1896945629 },
-        { SDL_PI_D * -8.0 / 11.0, 1.1540615205 },
-        { SDL_PI_D * -9.0 / 11.0, 0.6426609771 },
-        { SDL_PI_D * -10.0 / 11.0, 0.2936264929 }
+        { SDL_PI_D * 1.0 / 11.0, 0.29362649293836673 },
+        { SDL_PI_D * 2.0 / 11.0, 0.642660977168331 },
+        { SDL_PI_D * 3.0 / 11.0, 1.1540615205330094 },
+        { SDL_PI_D * 4.0 / 11.0, 2.189694562989681 },
+        { SDL_PI_D * 5.0 / 11.0, 6.9551527717734745 },
+        { SDL_PI_D * 6.0 / 11.0, -6.955152771773481 },
+        { SDL_PI_D * 7.0 / 11.0, -2.189694562989682 },
+        { SDL_PI_D * 8.0 / 11.0, -1.1540615205330096 },
+        { SDL_PI_D * 9.0 / 11.0, -0.6426609771683314 },
+        { SDL_PI_D * 10.0 / 11.0, -0.2936264929383667 },
+        { SDL_PI_D * -1.0 / 11.0, -0.29362649293836673 },
+        { SDL_PI_D * -2.0 / 11.0, -0.642660977168331 },
+        { SDL_PI_D * -3.0 / 11.0, -1.1540615205330094 },
+        { SDL_PI_D * -4.0 / 11.0, -2.189694562989681 },
+        { SDL_PI_D * -5.0 / 11.0, -6.9551527717734745 },
+        { SDL_PI_D * -6.0 / 11.0, 6.955152771773481 },
+        { SDL_PI_D * -7.0 / 11.0, 2.189694562989682 },
+        { SDL_PI_D * -8.0 / 11.0, 1.1540615205330096 },
+        { SDL_PI_D * -9.0 / 11.0, 0.6426609771683314 },
+        { SDL_PI_D * -10.0 / 11.0, 0.2936264929383667 }
     };
     return helper_dtod_inexact("Tan", SDL_tan, precision_cases, SDL_arraysize(precision_cases));
 }
@@ -2420,26 +2435,26 @@ static int
 asin_precisionTest(void *args)
 {
     const d_to_d precision_cases[] = {
-        { 0.9, 1.1197695149 },
-        { 0.8, 0.9272952180 },
-        { 0.7, 0.7753974966 },
-        { 0.6, 0.6435011087 },
-        { 0.5, 0.5235987755 },
-        { 0.4, 0.4115168460 },
-        { 0.3, 0.3046926540 },
-        { 0.2, 0.2013579207 },
-        { 0.1, 0.1001674211 },
+        { 0.9, 1.1197695149986342 },
+        { 0.8, 0.9272952180016123 },
+        { 0.7, 0.775397496610753 },
+        { 0.6, 0.6435011087932844 },
+        { 0.5, 0.5235987755982989 },
+        { 0.4, 0.41151684606748806 },
+        { 0.3, 0.3046926540153976 },
+        { 0.2, 0.20135792079033074 },
+        { 0.1, 0.10016742116155977 },
         { 0.0, 0.0 },
         { -0.0, -0.0 },
-        { -0.1, -0.1001674211 },
-        { -0.2, -0.2013579207 },
-        { -0.3, -0.3046926540 },
-        { -0.4, -0.4115168460 },
-        { -0.5, -0.5235987755 },
-        { -0.6, -0.6435011087 },
-        { -0.7, -0.7753974966 },
-        { -0.8, -0.9272952180 },
-        { -0.9, -1.1197695149 }
+        { -0.1, -0.10016742116155977 },
+        { -0.2, -0.20135792079033074 },
+        { -0.3, -0.3046926540153976 },
+        { -0.4, -0.41151684606748806 },
+        { -0.5, -0.5235987755982989 },
+        { -0.6, -0.6435011087932844 },
+        { -0.7, -0.775397496610753 },
+        { -0.8, -0.9272952180016123 },
+        { -0.9, -1.1197695149986342 }
     };
     return helper_dtod_inexact("Asin", SDL_asin, precision_cases, SDL_arraysize(precision_cases));
 }
@@ -2514,24 +2529,24 @@ static int
 atan_precisionTest(void *args)
 {
     const d_to_d precision_cases[] = {
-        { 6.313751514675041, 1.4137166941 },
-        { 3.0776835371752527, 1.2566370614 },
-        { 1.9626105055051504, 1.0995574287 },
-        { 1.3763819204711734, 0.9424777960 },
-        { 1.0, 0.7853981633 },
-        { 0.7265425280053609, 0.6283185307 },
-        { 0.5095254494944288, 0.4712388980 },
-        { 0.3249196962329063, 0.3141592653 },
-        { 0.15838444032453627, 0.1570796326 },
-        { -0.15838444032453627, -0.1570796326 },
-        { -0.3249196962329063, -0.3141592653 },
-        { -0.5095254494944288, -0.4712388980 },
-        { -0.7265425280053609, -0.6283185307 },
-        { -1.0, -0.7853981633 },
-        { -1.3763819204711734, -0.9424777960 },
-        { -1.9626105055051504, -1.0995574287 },
-        { -3.0776835371752527, -1.2566370614 },
-        { -6.313751514675041, -1.4137166941 },
+        { 6.313751514675041, 1.413716694115407 },
+        { 3.0776835371752527, 1.2566370614359172 },
+        { 1.9626105055051504, 1.0995574287564276 },
+        { 1.3763819204711734, 0.9424777960769379 },
+        { 1.0, 0.7853981633974483 },
+        { 0.7265425280053609, 0.6283185307179586 },
+        { 0.5095254494944288, 0.47123889803846897 },
+        { 0.3249196962329063, 0.3141592653589793 },
+        { 0.15838444032453627, 0.15707963267948966 },
+        { -0.15838444032453627, -0.15707963267948966 },
+        { -0.3249196962329063, -0.3141592653589793 },
+        { -0.5095254494944288, -0.47123889803846897 },
+        { -0.7265425280053609, -0.6283185307179586 },
+        { -1.0, -0.7853981633974483 },
+        { -1.3763819204711734, -0.9424777960769379 },
+        { -1.9626105055051504, -1.0995574287564276 },
+        { -3.0776835371752527, -1.2566370614359172 },
+        { -6.313751514675041, -1.413716694115407 },
     };
     return helper_dtod_inexact("Atan", SDL_atan, precision_cases, SDL_arraysize(precision_cases));
 }