xref: /freebsd/contrib/netbsd-tests/lib/libm/t_round.c (revision 5819c3eb8a1e2ac0a4f96a3aff5fefac276ae581)
1 /* $NetBSD: t_round.c,v 1.4 2013/11/11 23:57:34 joerg Exp $ */
2 
3 /*-
4  * Copyright (c) 2011 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. 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  *
16  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26  * POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 #include <atf-c.h>
30 #include <float.h>
31 #include <math.h>
32 
33 /*
34  * This tests for a bug in the initial implementation where
35  * precision was lost in an internal substraction, leading to
36  * rounding into the wrong direction.
37  */
38 
39 /* 0.5 - EPSILON */
40 #define VAL	0x0.7ffffffffffffcp0
41 #define VALF	0x0.7fffff8p0
42 #define VALL	(0.5 - LDBL_EPSILON)
43 
44 #ifdef __vax__
45 #define SMALL_NUM	1.0e-38
46 #else
47 #define SMALL_NUM	1.0e-40
48 #endif
49 
50 ATF_TC(round_dir);
51 ATF_TC_HEAD(round_dir, tc)
52 {
53 	atf_tc_set_md_var(tc, "descr","Check for rounding in wrong direction");
54 }
55 
56 ATF_TC_BODY(round_dir, tc)
57 {
58 	double a = VAL, b, c;
59 	float af = VALF, bf, cf;
60 	long double al = VALL, bl, cl;
61 
62 	b = round(a);
63 	bf = roundf(af);
64 	bl = roundl(al);
65 
66 	ATF_CHECK(fabs(b) < SMALL_NUM);
67 	ATF_CHECK(fabsf(bf) < SMALL_NUM);
68 	ATF_CHECK(fabsl(bl) < SMALL_NUM);
69 
70 	c = round(-a);
71 	cf = roundf(-af);
72 	cl = roundl(-al);
73 
74 	ATF_CHECK(fabs(c) < SMALL_NUM);
75 	ATF_CHECK(fabsf(cf) < SMALL_NUM);
76 	ATF_CHECK(fabsl(cl) < SMALL_NUM);
77 }
78 
79 ATF_TP_ADD_TCS(tp)
80 {
81 
82 	ATF_TP_ADD_TC(tp, round_dir);
83 
84 	return atf_no_error();
85 }
86