release 0.1.13
[liba.git] / test / math.h
blob13ee08e4170ad8902fc1af4f8f4f7d69aa33b2fc
1 #define MAIN(x) math##x
2 #include "test.h"
3 #include "a/math.h"
5 #if A_PREREQ_GNUC(3, 0)
6 #pragma GCC diagnostic ignored "-Wfloat-conversion"
7 #endif /* -Wfloat-conversion */
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(0x10000)) == A_U32_C(0x100));
23 TEST_BUG(a_u64_sqrt(A_U64_C(0xFFFFFFFF)) == A_U32_C(0xFFFF));
24 TEST_BUG(a_u64_sqrt(A_U64_C(0x100000000)) == A_U32_C(0x10000));
25 TEST_BUG(a_u64_sqrt(A_U64_C(0xFFFFFFFFFFFFFFFF)) == A_U32_C(0xFFFFFFFF));
28 static void test_f32_rsqrt(void)
30 a_f32 data[] = {
31 A_F32_C(0.0),
32 A_F32_C(-0.0),
33 A_F32_C(-1.0),
34 A_F32_C(1e-10),
35 A_F32_C(1.0),
36 A_F32_C(4.0),
37 A_F32_C(2.5) * A_F32_C(2.5),
39 for (unsigned int i = 0; i != sizeof(data) / sizeof(a_f32); ++i)
41 debug("1/sqrt(%g):\t%-10g%-10g\n", data[i], 1 / a_f32_sqrt(data[i]), a_f32_rsqrt(data[i]));
45 static void test_f64_rsqrt(void)
47 a_f64 data[] = {
48 A_F64_C(0.0),
49 A_F64_C(-0.0),
50 A_F64_C(-1.0),
51 A_F64_C(1e-10),
52 A_F64_C(1.0),
53 A_F64_C(4.0),
54 A_F64_C(2.5) * A_F64_C(2.5),
56 for (unsigned int i = 0; i != sizeof(data) / sizeof(a_f64); ++i)
58 debug("1/sqrt(%g):\t%-10g%-10g\n", data[i], 1 / a_f64_sqrt(data[i]), a_f64_rsqrt(data[i]));
62 int main(int argc, char *argv[]) // NOLINT(misc-definitions-in-headers)
64 (void)argc;
65 (void)argv;
66 test_u32_sqrt();
67 test_u64_sqrt();
68 test_f32_rsqrt();
69 test_f64_rsqrt();
71 a_f64 min = A_F64_MIN;
72 a_f64 max = A_F64_MAX;
73 a_f64 inf = A_F64_INF;
74 a_f64 nan = A_F64_NAN;
75 debug("64 min = %-12g max = %g\n", min, max);
76 debug("64 inf = %-12g nan = %g\n", inf, nan);
79 a_f32 min = A_F32_MIN;
80 a_f32 max = A_F32_MAX;
81 a_f32 inf = A_F32_INF;
82 a_f32 nan = A_F32_NAN;
83 debug("32 min = %-12g max = %g\n", min, max);
84 debug("32 inf = %-12g nan = %g\n", inf, nan);
87 a_float min = A_FLOAT_MIN;
88 a_float max = A_FLOAT_MAX;
89 a_float inf = A_FLOAT_INF;
90 a_float nan = A_FLOAT_NAN;
91 debug("min = " A_FLOAT_PRI("-12", "g ") "max = " A_FLOAT_PRI("", "g\n"), min, max);
92 debug("inf = " A_FLOAT_PRI("-12", "g ") "nan = " A_FLOAT_PRI("", "g\n"), inf, nan);
95 #undef a_float_expm1
96 a_float x = A_FLOAT_EPSILON / 2;
97 TEST_BUG(isinf(a_float_expm1(A_FLOAT_INF)));
98 TEST_BUG(isnan(a_float_expm1(A_FLOAT_NAN)));
99 debug("expm1(" A_FLOAT_PRI(".15", "g") ")=" A_FLOAT_PRI(".15", "g\n"), x, a_float_expm1(x));
100 debug("exp(" A_FLOAT_PRI(".15", "g") ")-1=" A_FLOAT_PRI(".15", "g\n"), x, a_float_exp(x) - 1);
103 #undef a_float_log1p
104 a_float x = A_FLOAT_EPSILON / 2;
105 TEST_BUG(isinf(a_float_log1p(A_FLOAT_INF)));
106 TEST_BUG(isnan(a_float_log1p(A_FLOAT_NAN)));
107 debug("log1p(" A_FLOAT_PRI(".15", "g") ")=" A_FLOAT_PRI(".15", "g\n"), x, a_float_log1p(x));
108 debug("log(1+" A_FLOAT_PRI(".15", "g") ")=" A_FLOAT_PRI(".15", "g\n"), x, a_float_log(x + 1));
111 #undef a_float_hypot
112 a_float x = A_FLOAT_MAX / A_FLOAT_SQRT2;
113 a_float y = A_FLOAT_MAX / A_FLOAT_SQRT2;
114 TEST_BUG(!isinf(a_float_hypot(x, y)));
115 TEST_BUG(isinf(a_float_hypot(A_FLOAT_INF, A_FLOAT_NAN)));
116 TEST_BUG(isinf(a_float_hypot(A_FLOAT_NAN, A_FLOAT_INF)));
117 TEST_BUG(isnan(a_float_hypot(A_FLOAT_NAN, A_FLOAT_NAN)));
120 a_float x = A_FLOAT_MAX / A_FLOAT_SQRT3;
121 a_float y = A_FLOAT_MAX / A_FLOAT_SQRT3;
122 a_float z = A_FLOAT_MAX / A_FLOAT_SQRT3;
123 TEST_BUG(!isinf(a_float_hypot3(x, y, z)));
124 TEST_BUG(isinf(a_float_hypot3(A_FLOAT_INF, A_FLOAT_NAN, A_FLOAT_NAN)));
125 TEST_BUG(isinf(a_float_hypot3(A_FLOAT_NAN, A_FLOAT_INF, A_FLOAT_NAN)));
126 TEST_BUG(isinf(a_float_hypot3(A_FLOAT_NAN, A_FLOAT_NAN, A_FLOAT_INF)));
127 TEST_BUG(isnan(a_float_hypot3(A_FLOAT_NAN, A_FLOAT_NAN, A_FLOAT_NAN)));
129 return 0;