xref: /freebsd/contrib/netbsd-tests/lib/libm/t_sinh.c (revision 9768746b)
1 /* $NetBSD: t_sinh.c,v 1.7 2018/11/07 03:59:36 riastradh Exp $ */
2 
3 /*-
4  * Copyright (c) 2011 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Jukka Ruohonen.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 #include <sys/cdefs.h>
32 __RCSID("$NetBSD: t_sinh.c,v 1.7 2018/11/07 03:59:36 riastradh Exp $");
33 
34 #include <atf-c.h>
35 #include <float.h>
36 #include <math.h>
37 #include <stdio.h>
38 
39 static const struct {
40 	double x;
41 	double y;
42 } values[] = {
43 	{ -10,   -11013.232874703393, },
44 	{ -2,    -3.626860407847019, },
45 	{ -1,    -1.1752011936438014, },
46 	{ -0.05, -0.050020835937655016, },
47 	{ -0.001,-0.0010000001666666751, },
48 	{  0.001, 0.0010000001666666751, },
49 	{  0.05,  0.050020835937655016, },
50 	{  1,     1.1752011936438014, },
51 	{  2,     3.626860407847019, },
52 	{  10,    11013.232874703393, },
53 };
54 
55 /*
56  * sinh(3)
57  */
58 ATF_TC(sinh_inrange);
59 ATF_TC_HEAD(sinh_inrange, tc)
60 {
61 	atf_tc_set_md_var(tc, "descr", "sinh(x) for some values");
62 }
63 
64 ATF_TC_BODY(sinh_inrange, tc)
65 {
66 	const double eps = DBL_EPSILON;
67 	size_t i;
68 
69 	for (i = 0; i < __arraycount(values); i++) {
70 		double x = values[i].x;
71 		double sinh_x = values[i].y;
72 
73 		if (!(fabs((sinh(x) - sinh_x)/sinh_x) <= eps)) {
74 			atf_tc_fail_nonfatal("sinh(%.17g) = %.17g != %.17g\n",
75 			    x, sinh(x), sinh_x);
76 		}
77 	}
78 }
79 
80 ATF_TC(sinh_nan);
81 ATF_TC_HEAD(sinh_nan, tc)
82 {
83 	atf_tc_set_md_var(tc, "descr", "Test sinh(NaN) == NaN");
84 }
85 
86 ATF_TC_BODY(sinh_nan, tc)
87 {
88 	const double x = 0.0L / 0.0L;
89 
90 	ATF_CHECK(isnan(x) != 0);
91 	ATF_CHECK(isnan(sinh(x)) != 0);
92 }
93 
94 ATF_TC(sinh_inf_neg);
95 ATF_TC_HEAD(sinh_inf_neg, tc)
96 {
97 	atf_tc_set_md_var(tc, "descr", "Test sinh(-Inf) == -Inf");
98 }
99 
100 ATF_TC_BODY(sinh_inf_neg, tc)
101 {
102 	const double x = -1.0L / 0.0L;
103 	double y = sinh(x);
104 
105 	ATF_CHECK(isinf(y) != 0);
106 	ATF_CHECK(signbit(y) != 0);
107 }
108 
109 ATF_TC(sinh_inf_pos);
110 ATF_TC_HEAD(sinh_inf_pos, tc)
111 {
112 	atf_tc_set_md_var(tc, "descr", "Test sinh(+Inf) == +Inf");
113 }
114 
115 ATF_TC_BODY(sinh_inf_pos, tc)
116 {
117 	const double x = 1.0L / 0.0L;
118 	double y = sinh(x);
119 
120 	ATF_CHECK(isinf(y) != 0);
121 	ATF_CHECK(signbit(y) == 0);
122 }
123 
124 ATF_TC(sinh_zero_neg);
125 ATF_TC_HEAD(sinh_zero_neg, tc)
126 {
127 	atf_tc_set_md_var(tc, "descr", "Test sinh(-0.0) == -0.0");
128 }
129 
130 ATF_TC_BODY(sinh_zero_neg, tc)
131 {
132 	const double x = -0.0L;
133 	double y = sinh(x);
134 
135 	if (fabs(y) > 0.0 || signbit(y) == 0)
136 		atf_tc_fail_nonfatal("sinh(-0.0) != -0.0");
137 }
138 
139 ATF_TC(sinh_zero_pos);
140 ATF_TC_HEAD(sinh_zero_pos, tc)
141 {
142 	atf_tc_set_md_var(tc, "descr", "Test sinh(+0.0) == +0.0");
143 }
144 
145 ATF_TC_BODY(sinh_zero_pos, tc)
146 {
147 	const double x = 0.0L;
148 	double y = sinh(x);
149 
150 	if (fabs(y) > 0.0 || signbit(y) != 0)
151 		atf_tc_fail_nonfatal("sinh(+0.0) != +0.0");
152 }
153 
154 /*
155  * sinhf(3)
156  */
157 ATF_TC(sinhf_inrange);
158 ATF_TC_HEAD(sinhf_inrange, tc)
159 {
160 	atf_tc_set_md_var(tc, "descr", "sinhf(x) for some values");
161 }
162 
163 ATF_TC_BODY(sinhf_inrange, tc)
164 {
165 	const float eps = FLT_EPSILON;
166 	size_t i;
167 
168 	for (i = 0; i < __arraycount(values); i++) {
169 		float x = values[i].x;
170 		float sinh_x = values[i].y;
171 
172 		if (!(fabsf((sinhf(x) - sinh_x)/sinh_x) <= eps)) {
173 			atf_tc_fail_nonfatal("sinhf(%.8g) = %.8g != %.8g\n",
174 			    (double)x, (double)sinhf(x), (double)sinh_x);
175 		}
176 	}
177 }
178 
179 ATF_TC(sinhf_nan);
180 ATF_TC_HEAD(sinhf_nan, tc)
181 {
182 	atf_tc_set_md_var(tc, "descr", "Test sinhf(NaN) == NaN");
183 }
184 
185 ATF_TC_BODY(sinhf_nan, tc)
186 {
187 	const float x = 0.0L / 0.0L;
188 
189 	ATF_CHECK(isnan(x) != 0);
190 	ATF_CHECK(isnan(sinhf(x)) != 0);
191 }
192 
193 ATF_TC(sinhf_inf_neg);
194 ATF_TC_HEAD(sinhf_inf_neg, tc)
195 {
196 	atf_tc_set_md_var(tc, "descr", "Test sinhf(-Inf) == -Inf");
197 }
198 
199 ATF_TC_BODY(sinhf_inf_neg, tc)
200 {
201 	const float x = -1.0L / 0.0L;
202 	float y = sinhf(x);
203 
204 	ATF_CHECK(isinf(y) != 0);
205 	ATF_CHECK(signbit(y) != 0);
206 }
207 
208 ATF_TC(sinhf_inf_pos);
209 ATF_TC_HEAD(sinhf_inf_pos, tc)
210 {
211 	atf_tc_set_md_var(tc, "descr", "Test sinhf(+Inf) == +Inf");
212 }
213 
214 ATF_TC_BODY(sinhf_inf_pos, tc)
215 {
216 	const float x = 1.0L / 0.0L;
217 	float y = sinhf(x);
218 
219 	ATF_CHECK(isinf(y) != 0);
220 	ATF_CHECK(signbit(y) == 0);
221 }
222 
223 ATF_TC(sinhf_zero_neg);
224 ATF_TC_HEAD(sinhf_zero_neg, tc)
225 {
226 	atf_tc_set_md_var(tc, "descr", "Test sinhf(-0.0) == -0.0");
227 }
228 
229 ATF_TC_BODY(sinhf_zero_neg, tc)
230 {
231 	const float x = -0.0L;
232 	float y = sinhf(x);
233 
234 	if (fabsf(y) > 0.0 || signbit(y) == 0)
235 		atf_tc_fail_nonfatal("sinhf(-0.0) != -0.0");
236 }
237 
238 ATF_TC(sinhf_zero_pos);
239 ATF_TC_HEAD(sinhf_zero_pos, tc)
240 {
241 	atf_tc_set_md_var(tc, "descr", "Test sinhf(+0.0) == +0.0");
242 }
243 
244 ATF_TC_BODY(sinhf_zero_pos, tc)
245 {
246 	const float x = 0.0L;
247 	float y = sinhf(x);
248 
249 	if (fabsf(y) > 0.0 || signbit(y) != 0)
250 		atf_tc_fail_nonfatal("sinhf(+0.0) != +0.0");
251 }
252 
253 ATF_TP_ADD_TCS(tp)
254 {
255 
256 	ATF_TP_ADD_TC(tp, sinh_inrange);
257 	ATF_TP_ADD_TC(tp, sinh_nan);
258 	ATF_TP_ADD_TC(tp, sinh_inf_neg);
259 	ATF_TP_ADD_TC(tp, sinh_inf_pos);
260 	ATF_TP_ADD_TC(tp, sinh_zero_neg);
261 	ATF_TP_ADD_TC(tp, sinh_zero_pos);
262 
263 	ATF_TP_ADD_TC(tp, sinhf_inrange);
264 	ATF_TP_ADD_TC(tp, sinhf_nan);
265 	ATF_TP_ADD_TC(tp, sinhf_inf_neg);
266 	ATF_TP_ADD_TC(tp, sinhf_inf_pos);
267 	ATF_TP_ADD_TC(tp, sinhf_zero_neg);
268 	ATF_TP_ADD_TC(tp, sinhf_zero_pos);
269 
270 	return atf_no_error();
271 }
272