2 * Copyright (c) 2010 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 nearbyint{,f,l}()
31 * - adapt tests for rint(3)
32 * - tests for harder values (more mantissa bits than float)
33 * - tests in other rounding modes
36 #include <sys/cdefs.h>
37 __FBSDID("$FreeBSD$");
44 #define ALL_STD_EXCEPT (FE_DIVBYZERO | FE_INEXACT | FE_INVALID | \
45 FE_OVERFLOW | FE_UNDERFLOW)
48 * Compare d1 and d2 using special rules: NaN == NaN and +0 != -0.
49 * Fail an assertion if they differ.
52 fpequal(long double d1, long double d2)
56 return (isnan(d1) && isnan(d2));
57 return (copysignl(1.0, d1) == copysignl(1.0, d2));
60 static void testit(int testnum, float in, float out)
63 feclearexcept(ALL_STD_EXCEPT);
64 assert(fpequal(out, nearbyintf(in)));
65 assert(fpequal(-out, nearbyintf(-in)));
66 assert(fetestexcept(ALL_STD_EXCEPT) == 0);
68 assert(fpequal(out, nearbyint(in)));
69 assert(fpequal(-out, nearbyint(-in)));
70 assert(fetestexcept(ALL_STD_EXCEPT) == 0);
72 assert(fpequal(out, nearbyintl(in)));
73 assert(fpequal(-out, nearbyintl(-in)));
74 assert(fetestexcept(ALL_STD_EXCEPT) == 0);
76 printf("ok %d\t\t# nearbyint(%g)\n", testnum, in);
79 static const float tests[] = {
80 /* input output (expected) */
90 main(int argc, char *argv[])
92 static const int ntests = sizeof(tests) / sizeof(tests[0]) / 2;
95 printf("1..%d\n", ntests);
96 for (i = 0; i < ntests; i++)
97 testit(i + 1, tests[i * 2], tests[i * 2 + 1]);