xref: /netbsd-src/tests/lib/libm/t_round.c (revision b49cc1491953ef2348eff9c84520ffd0678a5c8d)
1 /* $NetBSD: t_round.c,v 1.2 2011/07/04 22:33:29 mrg Exp $ */
2 
3 #include <atf-c.h>
4 #include <math.h>
5 
6 /*
7  * This tests for a bug in the initial implementation where
8  * precision was lost in an internal substraction, leading to
9  * rounding into the wrong direction.
10  */
11 
12 /* 0.5 - EPSILON */
13 #define VAL	0x0.7ffffffffffffcp0
14 #define VALF	0x0.7fffff8p0
15 
16 #ifdef __vax__
17 #define SMALL_NUM	1.0e-38
18 #else
19 #define SMALL_NUM	1.0e-40
20 #endif
21 
22 ATF_TC(round_dir);
23 ATF_TC_HEAD(round_dir, tc)
24 {
25 	atf_tc_set_md_var(tc, "descr","Check for rounding in wrong direction");
26 }
27 
28 ATF_TC_BODY(round_dir, tc)
29 {
30 	double a = VAL, b, c;
31 	float af = VALF, bf, cf;
32 
33 	b = round(a);
34 	bf = roundf(af);
35 
36 	ATF_REQUIRE(fabs(b) < SMALL_NUM);
37 	ATF_REQUIRE(fabsf(bf) < SMALL_NUM);
38 
39 	c = round(-a);
40 	cf = roundf(-af);
41 
42 	ATF_REQUIRE(fabs(c) < SMALL_NUM);
43 	ATF_REQUIRE(fabsf(cf) < SMALL_NUM);
44 }
45 
46 ATF_TP_ADD_TCS(tp)
47 {
48 
49 	ATF_TP_ADD_TC(tp, round_dir);
50 
51 	return atf_no_error();
52 }
53