1 | /*
|
---|
2 | * Copyright (c) 2014 Martin Decky
|
---|
3 | * Copyright (c) 2015 Jiri Svoboda
|
---|
4 | * All rights reserved.
|
---|
5 | *
|
---|
6 | * Redistribution and use in source and binary forms, with or without
|
---|
7 | * modification, are permitted provided that the following conditions
|
---|
8 | * are met:
|
---|
9 | *
|
---|
10 | * - Redistributions of source code must retain the above copyright
|
---|
11 | * notice, this list of conditions and the following disclaimer.
|
---|
12 | * - Redistributions in binary form must reproduce the above copyright
|
---|
13 | * notice, this list of conditions and the following disclaimer in the
|
---|
14 | * documentation and/or other materials provided with the distribution.
|
---|
15 | * - The name of the author may not be used to endorse or promote products
|
---|
16 | * derived from this software without specific prior written permission.
|
---|
17 | *
|
---|
18 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
19 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
20 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
21 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
22 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
23 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
24 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
25 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
27 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
28 | */
|
---|
29 |
|
---|
30 | #include <stdbool.h>
|
---|
31 | #include <stdio.h>
|
---|
32 | #include <stdlib.h>
|
---|
33 | #include <math.h>
|
---|
34 | #include "../tester.h"
|
---|
35 |
|
---|
36 | #define OPERANDS 10
|
---|
37 | #define PRECISIONF 10000
|
---|
38 | #define PRECISION 100000000
|
---|
39 |
|
---|
40 | static double arguments[OPERANDS] = {
|
---|
41 | 3.5, -2.1, 100.0, 50.0, -1024.0, 0.0, 768.3156, 1080.499999, -600.0, 1.0
|
---|
42 | };
|
---|
43 |
|
---|
44 | static double arguments_exp[OPERANDS] = {
|
---|
45 | 3.5, -2.1, 50.0, 0.0, 1.0, 13.2, -1.1, -5.5, 0.1, -66.0
|
---|
46 | };
|
---|
47 |
|
---|
48 | static double arguments_log[OPERANDS] = {
|
---|
49 | 3.5, 100.0, 50.0, 768.3156, 1080.499999, 1.0, 66.0,
|
---|
50 | 2.718281828459045, 9.9, 0.001
|
---|
51 | };
|
---|
52 |
|
---|
53 |
|
---|
54 | static double results_ceil[OPERANDS] = {
|
---|
55 | 4.0, -2.0, 100.0, 50.0, -1024.0, 0.0, 769.0, 1081.0, -600.0, 1.0
|
---|
56 | };
|
---|
57 |
|
---|
58 | static double results_floor[OPERANDS] = {
|
---|
59 | 3.0, -3.0, 100.0, 50.0, -1024.0, 0.0, 768.0, 1080.0, -600.0, 1.0
|
---|
60 | };
|
---|
61 |
|
---|
62 | static double results_trunc[OPERANDS] = {
|
---|
63 | 3.0, -2.0, 100.0, 50.0, -1024.0, 0.0, 768.0, 1080.0, -600.0, 1.0
|
---|
64 | };
|
---|
65 |
|
---|
66 | static double results_sin[OPERANDS] = {
|
---|
67 | -0.350783227690, -0.863209366649, -0.506365641110, -0.262374853704,
|
---|
68 | 0.158533380044, 0.0, 0.980815184715, -0.206379975025, -0.044182448332,
|
---|
69 | 0.841470984808
|
---|
70 | };
|
---|
71 |
|
---|
72 | static double results_cos[OPERANDS] = {
|
---|
73 | -0.936456687291, -0.504846104600, 0.862318872288, 0.964966028492,
|
---|
74 | 0.987353618220, 1.0, -0.194939922623, 0.978471923925, -0.999023478833,
|
---|
75 | 0.540302305868
|
---|
76 | };
|
---|
77 |
|
---|
78 | static double results_log[OPERANDS] = {
|
---|
79 | 1.252762968495, 4.605170185988, 3.912023005428, 6.644200586236,
|
---|
80 | 6.985179175021, 0.000000000000, 4.189654742026, 1.000000000000,
|
---|
81 | 2.292534757141, -6.907755278982
|
---|
82 | };
|
---|
83 |
|
---|
84 | static double results_exp[OPERANDS] = {
|
---|
85 | 33.115451958692, 0.122456428253, 5184705528587072045056.0,
|
---|
86 | 1.000000000000, 2.718281828459, 540364.937246691552, 0.332871083698,
|
---|
87 | 0.004086771438, 1.105170918076, 0.000000000000
|
---|
88 | };
|
---|
89 |
|
---|
90 | static bool cmp_float(float a, float b)
|
---|
91 | {
|
---|
92 | float r;
|
---|
93 |
|
---|
94 | /* XXX Need fabsf() */
|
---|
95 | if (b < 1.0 / PRECISIONF && b > -1.0 / PRECISIONF)
|
---|
96 | r = a;
|
---|
97 | else
|
---|
98 | r = a / b - 1.0;
|
---|
99 |
|
---|
100 | /* XXX Need fabsf() */
|
---|
101 | if (r < 0.0)
|
---|
102 | r = -r;
|
---|
103 |
|
---|
104 | return r < 1.0 / PRECISIONF;
|
---|
105 | }
|
---|
106 |
|
---|
107 | static bool cmp_double(double a, double b)
|
---|
108 | {
|
---|
109 | double r;
|
---|
110 |
|
---|
111 | /* XXX Need fabs() */
|
---|
112 | if (b < 1.0 / PRECISION && b > -1.0 / PRECISION)
|
---|
113 | r = a;
|
---|
114 | else
|
---|
115 | r = a / b - 1.0;
|
---|
116 |
|
---|
117 | /* XXX Need fabs() */
|
---|
118 | if (r < 0.0)
|
---|
119 | r = -r;
|
---|
120 |
|
---|
121 | return r < 1.0 / PRECISION;
|
---|
122 | }
|
---|
123 |
|
---|
124 | const char *test_float2(void)
|
---|
125 | {
|
---|
126 | bool fail = false;
|
---|
127 |
|
---|
128 | for (unsigned int i = 0; i < OPERANDS; i++) {
|
---|
129 | double res = ceil(arguments[i]);
|
---|
130 |
|
---|
131 | if (!cmp_double(res, results_ceil[i])) {
|
---|
132 | TPRINTF("Double precision ceil failed "
|
---|
133 | "(%lf != %lf, arg %u)\n", res, results_ceil[i], i);
|
---|
134 | fail = true;
|
---|
135 | }
|
---|
136 | }
|
---|
137 |
|
---|
138 | for (unsigned int i = 0; i < OPERANDS; i++) {
|
---|
139 | float res = ceilf(arguments[i]);
|
---|
140 |
|
---|
141 | if (!cmp_float(res, results_ceil[i])) {
|
---|
142 | TPRINTF("Single precision ceil failed "
|
---|
143 | "(%f != %lf, arg %u)\n", res, results_ceil[i], i);
|
---|
144 | fail = true;
|
---|
145 | }
|
---|
146 | }
|
---|
147 |
|
---|
148 | for (unsigned int i = 0; i < OPERANDS; i++) {
|
---|
149 | double res = cos(arguments[i]);
|
---|
150 |
|
---|
151 | if (!cmp_double(res, results_cos[i])) {
|
---|
152 | TPRINTF("Double precision cos failed "
|
---|
153 | "(%lf != %lf, arg %u)\n", res, results_cos[i], i);
|
---|
154 | fail = true;
|
---|
155 | }
|
---|
156 | }
|
---|
157 |
|
---|
158 | for (unsigned int i = 0; i < OPERANDS; i++) {
|
---|
159 | float res = cosf(arguments[i]);
|
---|
160 |
|
---|
161 | if (!cmp_float(res, results_cos[i])) {
|
---|
162 | TPRINTF("Single precision cos failed "
|
---|
163 | "(%f != %lf, arg %u)\n", res, results_cos[i], i);
|
---|
164 | fail = true;
|
---|
165 | }
|
---|
166 | }
|
---|
167 |
|
---|
168 | for (unsigned int i = 0; i < OPERANDS; i++) {
|
---|
169 | double res = exp(arguments_exp[i]);
|
---|
170 |
|
---|
171 | if (!cmp_double(res, results_exp[i])) {
|
---|
172 | TPRINTF("Double precision exp failed "
|
---|
173 | "(%lf != %lf, arg %u)\n", res, results_exp[i], i);
|
---|
174 | fail = true;
|
---|
175 | }
|
---|
176 | }
|
---|
177 |
|
---|
178 | for (unsigned int i = 0; i < OPERANDS; i++) {
|
---|
179 | float res = expf(arguments_exp[i]);
|
---|
180 |
|
---|
181 | if (!cmp_float(res, results_exp[i])) {
|
---|
182 | TPRINTF("Single precision exp failed "
|
---|
183 | "(%f != %lf, arg %u)\n", res, results_exp[i], i);
|
---|
184 | fail = true;
|
---|
185 | }
|
---|
186 | }
|
---|
187 |
|
---|
188 | for (unsigned int i = 0; i < OPERANDS; i++) {
|
---|
189 | double res = floor(arguments[i]);
|
---|
190 |
|
---|
191 | if (!cmp_double(res, results_floor[i])) {
|
---|
192 | TPRINTF("Double precision floor failed "
|
---|
193 | "(%lf != %lf, arg %u)\n", res, results_floor[i], i);
|
---|
194 | fail = true;
|
---|
195 | }
|
---|
196 | }
|
---|
197 |
|
---|
198 | for (unsigned int i = 0; i < OPERANDS; i++) {
|
---|
199 | float res = floorf(arguments[i]);
|
---|
200 |
|
---|
201 | if (!cmp_float(res, results_floor[i])) {
|
---|
202 | TPRINTF("Single precision floor failed "
|
---|
203 | "(%f != %lf, arg %u)\n", res, results_floor[i], i);
|
---|
204 | fail = true;
|
---|
205 | }
|
---|
206 | }
|
---|
207 |
|
---|
208 | for (unsigned int i = 0; i < OPERANDS; i++) {
|
---|
209 | double res = log(arguments_log[i]);
|
---|
210 |
|
---|
211 | if (!cmp_double(res, results_log[i])) {
|
---|
212 | TPRINTF("Double precision log failed "
|
---|
213 | "(%lf != %lf, arg %u)\n", res, results_log[i], i);
|
---|
214 | fail = true;
|
---|
215 | }
|
---|
216 | }
|
---|
217 |
|
---|
218 | for (unsigned int i = 0; i < OPERANDS; i++) {
|
---|
219 | float res = logf(arguments_log[i]);
|
---|
220 |
|
---|
221 | if (!cmp_float(res, results_log[i])) {
|
---|
222 | TPRINTF("Single precision log failed "
|
---|
223 | "(%f != %lf, arg %u)\n", res, results_log[i], i);
|
---|
224 | fail = true;
|
---|
225 | }
|
---|
226 | }
|
---|
227 |
|
---|
228 | for (unsigned int i = 0; i < OPERANDS; i++) {
|
---|
229 | double res = sin(arguments[i]);
|
---|
230 |
|
---|
231 | if (!cmp_double(res, results_sin[i])) {
|
---|
232 | TPRINTF("Double precision sin failed "
|
---|
233 | "(%lf != %lf, arg %u)\n", res, results_sin[i], i);
|
---|
234 | fail = true;
|
---|
235 | }
|
---|
236 | }
|
---|
237 |
|
---|
238 | for (unsigned int i = 0; i < OPERANDS; i++) {
|
---|
239 | float res = sinf(arguments[i]);
|
---|
240 |
|
---|
241 | if (!cmp_float(res, results_sin[i])) {
|
---|
242 | TPRINTF("Single precision sin failed "
|
---|
243 | "(%f != %lf, arg %u)\n", res, results_sin[i], i);
|
---|
244 | fail = true;
|
---|
245 | }
|
---|
246 | }
|
---|
247 |
|
---|
248 | for (unsigned int i = 0; i < OPERANDS; i++) {
|
---|
249 | double res = trunc(arguments[i]);
|
---|
250 |
|
---|
251 | if (!cmp_double(res, results_trunc[i])) {
|
---|
252 | TPRINTF("Double precision trunc failed "
|
---|
253 | "(%lf != %lf, arg %u)\n", res, results_trunc[i], i);
|
---|
254 | fail = true;
|
---|
255 | }
|
---|
256 | }
|
---|
257 |
|
---|
258 | for (unsigned int i = 0; i < OPERANDS; i++) {
|
---|
259 | float res = truncf(arguments[i]);
|
---|
260 |
|
---|
261 | if (!cmp_float(res, results_trunc[i])) {
|
---|
262 | TPRINTF("truncgle precision trunc failed "
|
---|
263 | "(%f != %lf, arg %u)\n", res, results_trunc[i], i);
|
---|
264 | fail = true;
|
---|
265 | }
|
---|
266 | }
|
---|
267 |
|
---|
268 | if (fail)
|
---|
269 | return "Floating point imprecision";
|
---|
270 |
|
---|
271 | return NULL;
|
---|
272 | }
|
---|