disable misc-non-private-member-variables-in-classes in .clang-tidy
[liba.git] / test / math.h
bloba76b83860a4af5690c54cb78c91082ff033f23a2
1 #define MAIN_(x) A_CAST_2(x, _math)
2 #include "test.h"
3 #include "a/math.h"
5 #if defined(__MINGW32__) && A_PREREQ_GNUC(3, 0)
6 #pragma GCC diagnostic ignored "-Wfloat-conversion"
7 #endif /* __MINGW32__ */
8 #if A_PREREQ_GNUC(4, 6) || __has_warning("-Wdouble-promotion")
9 #pragma GCC diagnostic ignored "-Wdouble-promotion"
10 #endif /* -Wdouble-promotion */
12 static void test_u32_sqrt(void)
14 TEST_BUG(a_u32_sqrt(A_U32_C(~0)) == A_U16_C(0xFFFF));
15 TEST_BUG(a_u32_sqrt(A_U32_C(0x10000)) == A_U16_C(0x100));
16 TEST_BUG(a_u32_sqrt(A_U32_C(0xFFFFFFFF)) == A_U16_C(0xFFFF));
19 static void test_u64_sqrt(void)
21 TEST_BUG(a_u64_sqrt(A_U64_C(~0)) == A_U32_C(0xFFFFFFFF));
22 TEST_BUG(a_u64_sqrt(A_U64_C(0x100000000)) == A_U32_C(0x10000));
23 TEST_BUG(a_u64_sqrt(A_U64_C(0xFFFFFFFFFFFFFFFF)) == A_U32_C(0xFFFFFFFF));
26 static void test_f32_rsqrt(void)
28 a_f32 data[] = {
29 A_F32_C(-1.0),
30 A_F32_C(-0.0),
31 A_F32_C(1e-10),
32 A_F32_C(1.0),
33 A_F32_C(4.0),
34 A_F32_C(2.5) * A_F32_C(2.5),
36 for (unsigned int i = 0; i != sizeof(data) / sizeof(a_f32); ++i)
38 debug("1/sqrt(%g):\t%-10g%-10g\n", data[i], 1 / a_f32_sqrt(data[i]), a_f32_rsqrt(data[i]));
42 static void test_f64_rsqrt(void)
44 a_f64 data[] = {
45 A_F64_C(-1.0),
46 A_F64_C(-0.0),
47 A_F64_C(1e-10),
48 A_F64_C(1.0),
49 A_F64_C(4.0),
50 A_F64_C(2.5) * A_F64_C(2.5),
52 for (unsigned int i = 0; i != sizeof(data) / sizeof(a_f64); ++i)
54 debug("1/sqrt(%g):\t%-10g%-10g\n", data[i], 1 / a_f64_sqrt(data[i]), a_f64_rsqrt(data[i]));
58 int MAIN(int argc, char *argv[]) // NOLINT(misc-definitions-in-headers)
60 (void)(argc);
61 (void)(argv);
62 test_u32_sqrt();
63 test_u64_sqrt();
64 test_f32_rsqrt();
65 test_f64_rsqrt();
67 a_f64 min = A_F64_MIN;
68 a_f64 max = A_F64_MAX;
69 a_f64 inf = A_F64_INF;
70 a_f64 nan = A_F64_NAN;
71 debug("64 min = %-12g max = %g\n", min, max);
72 debug("64 inf = %-12g nan = %g\n", inf, nan);
75 a_f32 min = A_F32_MIN;
76 a_f32 max = A_F32_MAX;
77 a_f32 inf = A_F32_INF;
78 a_f32 nan = A_F32_NAN;
79 debug("32 min = %-12g max = %g\n", min, max);
80 debug("32 inf = %-12g nan = %g\n", inf, nan);
83 a_float min = A_FLOAT_MIN;
84 a_float max = A_FLOAT_MAX;
85 a_float inf = A_FLOAT_INF;
86 a_float nan = A_FLOAT_NAN;
87 debug("min = " A_FLOAT_PRI("-12", "g ") "max = " A_FLOAT_PRI("", "g\n"), min, max);
88 debug("inf = " A_FLOAT_PRI("-12", "g ") "nan = " A_FLOAT_PRI("", "g\n"), inf, nan);
91 #undef a_float_expm1
92 a_float x = A_FLOAT_EPSILON / 2;
93 TEST_BUG(isinf(a_float_expm1(A_FLOAT_INF)));
94 TEST_BUG(isnan(a_float_expm1(A_FLOAT_NAN)));
95 debug("expm1(" A_FLOAT_PRI(".15", "g") ")=" A_FLOAT_PRI(".15", "g\n"), x, a_float_expm1(x));
96 debug("exp(" A_FLOAT_PRI(".15", "g") ")-1=" A_FLOAT_PRI(".15", "g\n"), x, a_float_exp(x) - 1);
99 #undef a_float_log1p
100 a_float x = A_FLOAT_EPSILON / 2;
101 TEST_BUG(isinf(a_float_log1p(A_FLOAT_INF)));
102 TEST_BUG(isnan(a_float_log1p(A_FLOAT_NAN)));
103 debug("log1p(" A_FLOAT_PRI(".15", "g") ")=" A_FLOAT_PRI(".15", "g\n"), x, a_float_log1p(x));
104 debug("log(1+" A_FLOAT_PRI(".15", "g") ")=" A_FLOAT_PRI(".15", "g\n"), x, a_float_log(x + 1));
107 #undef a_float_hypot
108 a_float x = A_FLOAT_MAX / A_FLOAT_SQRT2;
109 a_float y = A_FLOAT_MAX / A_FLOAT_SQRT2;
110 TEST_BUG(!isinf(a_float_hypot(x, y)));
111 TEST_BUG(isinf(a_float_hypot(A_FLOAT_INF, A_FLOAT_NAN)));
112 TEST_BUG(isinf(a_float_hypot(A_FLOAT_NAN, A_FLOAT_INF)));
113 TEST_BUG(isnan(a_float_hypot(A_FLOAT_NAN, A_FLOAT_NAN)));
116 a_float x = A_FLOAT_MAX / A_FLOAT_SQRT3;
117 a_float y = A_FLOAT_MAX / A_FLOAT_SQRT3;
118 a_float z = A_FLOAT_MAX / A_FLOAT_SQRT3;
119 TEST_BUG(!isinf(a_float_hypot3(x, y, z)));
120 TEST_BUG(isinf(a_float_hypot3(A_FLOAT_INF, A_FLOAT_NAN, A_FLOAT_NAN)));
121 TEST_BUG(isinf(a_float_hypot3(A_FLOAT_NAN, A_FLOAT_INF, A_FLOAT_NAN)));
122 TEST_BUG(isinf(a_float_hypot3(A_FLOAT_NAN, A_FLOAT_NAN, A_FLOAT_INF)));
123 TEST_BUG(isnan(a_float_hypot3(A_FLOAT_NAN, A_FLOAT_NAN, A_FLOAT_NAN)));
125 return 0;