2 * Copyright (c) 2007 David Schultz <das@FreeBSD.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * Tests for csqrt{,f}()
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
34 #include <sys/param.h>
42 #include "test-utils.h"
45 * This is a test hook that can point to csqrtl(), _csqrt(), or to _csqrtf().
46 * The latter two convert to float or double, respectively, and test csqrtf()
47 * and csqrt() with the same arguments.
49 long double complex (*t_csqrt)(long double complex);
51 static long double complex
52 _csqrtf(long double complex d)
55 return (csqrtf((float complex)d));
58 static long double complex
59 _csqrt(long double complex d)
62 return (csqrt((double complex)d));
65 #pragma STDC CX_LIMITED_RANGE OFF
68 * Compare d1 and d2 using special rules: NaN == NaN and +0 != -0.
69 * Fail an assertion if they differ.
72 assert_equal(long double complex d1, long double complex d2)
75 assert(cfpequal(d1, d2));
79 * Test csqrt for some finite arguments where the answer is exact.
80 * (We do not test if it produces correctly rounded answers when the
81 * result is inexact, nor do we check whether it throws spurious
87 static const double tests[] = {
88 /* csqrt(a + bI) = x + yI */
108 460766389075.0, 16762287900.0, 678910, 12345
111 * We also test some multiples of the above arguments. This
112 * array defines which multiples we use. Note that these have
113 * to be small enough to not cause overflow for float precision
114 * with all of the constants in the above table.
116 static const double mults[] = {
130 for (i = 0; i < nitems(tests); i += 4) {
131 for (j = 0; j < nitems(mults); j++) {
132 a = tests[i] * mults[j] * mults[j];
133 b = tests[i + 1] * mults[j] * mults[j];
134 x = tests[i + 2] * mults[j];
135 y = tests[i + 3] * mults[j];
136 assert(t_csqrt(CMPLXL(a, b)) == CMPLXL(x, y));
143 * Test the handling of +/- 0.
149 assert_equal(t_csqrt(CMPLXL(0.0, 0.0)), CMPLXL(0.0, 0.0));
150 assert_equal(t_csqrt(CMPLXL(-0.0, 0.0)), CMPLXL(0.0, 0.0));
151 assert_equal(t_csqrt(CMPLXL(0.0, -0.0)), CMPLXL(0.0, -0.0));
152 assert_equal(t_csqrt(CMPLXL(-0.0, -0.0)), CMPLXL(0.0, -0.0));
156 * Test the handling of infinities when the other argument is not NaN.
161 static const double vals[] = {
172 for (i = 0; i < nitems(vals); i++) {
173 if (isfinite(vals[i])) {
174 assert_equal(t_csqrt(CMPLXL(-INFINITY, vals[i])),
175 CMPLXL(0.0, copysignl(INFINITY, vals[i])));
176 assert_equal(t_csqrt(CMPLXL(INFINITY, vals[i])),
177 CMPLXL(INFINITY, copysignl(0.0, vals[i])));
179 assert_equal(t_csqrt(CMPLXL(vals[i], INFINITY)),
180 CMPLXL(INFINITY, INFINITY));
181 assert_equal(t_csqrt(CMPLXL(vals[i], -INFINITY)),
182 CMPLXL(INFINITY, -INFINITY));
187 * Test the handling of NaNs.
193 assert(creall(t_csqrt(CMPLXL(INFINITY, NAN))) == INFINITY);
194 assert(isnan(cimagl(t_csqrt(CMPLXL(INFINITY, NAN)))));
196 assert(isnan(creall(t_csqrt(CMPLXL(-INFINITY, NAN)))));
197 assert(isinf(cimagl(t_csqrt(CMPLXL(-INFINITY, NAN)))));
199 assert_equal(t_csqrt(CMPLXL(NAN, INFINITY)),
200 CMPLXL(INFINITY, INFINITY));
201 assert_equal(t_csqrt(CMPLXL(NAN, -INFINITY)),
202 CMPLXL(INFINITY, -INFINITY));
204 assert_equal(t_csqrt(CMPLXL(0.0, NAN)), CMPLXL(NAN, NAN));
205 assert_equal(t_csqrt(CMPLXL(-0.0, NAN)), CMPLXL(NAN, NAN));
206 assert_equal(t_csqrt(CMPLXL(42.0, NAN)), CMPLXL(NAN, NAN));
207 assert_equal(t_csqrt(CMPLXL(-42.0, NAN)), CMPLXL(NAN, NAN));
208 assert_equal(t_csqrt(CMPLXL(NAN, 0.0)), CMPLXL(NAN, NAN));
209 assert_equal(t_csqrt(CMPLXL(NAN, -0.0)), CMPLXL(NAN, NAN));
210 assert_equal(t_csqrt(CMPLXL(NAN, 42.0)), CMPLXL(NAN, NAN));
211 assert_equal(t_csqrt(CMPLXL(NAN, -42.0)), CMPLXL(NAN, NAN));
212 assert_equal(t_csqrt(CMPLXL(NAN, NAN)), CMPLXL(NAN, NAN));
216 * Test whether csqrt(a + bi) works for inputs that are large enough to
217 * cause overflow in hypot(a, b) + a. In this case we are using
218 * csqrt(115 + 252*I) == 14 + 9*I
219 * scaled up to near MAX_EXP.
222 test_overflow(int maxexp)
225 long double complex result;
227 a = ldexpl(115 * 0x1p-8, maxexp);
228 b = ldexpl(252 * 0x1p-8, maxexp);
229 result = t_csqrt(CMPLXL(a, b));
230 assert(creall(result) == ldexpl(14 * 0x1p-4, maxexp / 2));
231 assert(cimagl(result) == ldexpl(9 * 0x1p-4, maxexp / 2));
235 main(int argc, char *argv[])
244 printf("ok 1 - csqrt\n");
247 printf("ok 2 - csqrt\n");
250 printf("ok 3 - csqrt\n");
253 printf("ok 4 - csqrt\n");
255 test_overflow(DBL_MAX_EXP);
256 printf("ok 5 - csqrt\n");
258 /* Now test csqrtf() */
262 printf("ok 6 - csqrt\n");
265 printf("ok 7 - csqrt\n");
268 printf("ok 8 - csqrt\n");
271 printf("ok 9 - csqrt\n");
273 test_overflow(FLT_MAX_EXP);
274 printf("ok 10 - csqrt\n");
276 /* Now test csqrtl() */
280 printf("ok 11 - csqrt\n");
283 printf("ok 12 - csqrt\n");
286 printf("ok 13 - csqrt\n");
289 printf("ok 14 - csqrt\n");
291 test_overflow(LDBL_MAX_EXP);
292 printf("ok 15 - csqrt\n");