1 /* $NetBSD: t_cos.c,v 1.12 2024/06/09 16:53:12 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 32 #include <assert.h> 33 #include <atf-c.h> 34 #include <float.h> 35 #include <math.h> 36 37 static const struct { 38 int angle; 39 double x; 40 double y; 41 float fy; 42 } angles[] = { 43 { -180, -3.141592653589793, -1.0000000000000000, 999 }, 44 { -135, -2.356194490192345, -0.7071067811865476, 999 }, 45 { -90, -1.5707963267948966, 6.123233995736766e-17, -4.3711388e-08 }, 46 { -90, -1.5707963267948968, -1.6081226496766366e-16, -4.3711388e-08 }, 47 { -45, -0.785398163397448, 0.7071067811865478, 999 }, 48 { 0, 0.000000000000000, 1.0000000000000000, 999 }, 49 { 30, 0.523598775598299, 0.8660254037844386, 999 }, 50 { 45, 0.785398163397448, 0.7071067811865478, 999 }, 51 { 60, 1.0471975511965976, 0.5000000000000001, 999 }, 52 { 60, 1.0471975511965979, 0.4999999999999999, 999 }, 53 { 90, 1.570796326794897, -3.8285686989269494e-16, -4.3711388e-08 }, 54 { 120, 2.0943951023931953, -0.4999999999999998, 999 }, 55 { 120, 2.0943951023931957, -0.5000000000000002, 999 }, 56 { 135, 2.356194490192345, -0.7071067811865476, 999 }, 57 { 150, 2.617993877991494, -0.8660254037844386, 999 }, 58 { 180, 3.141592653589793, -1.0000000000000000, 999 }, 59 { 270, 4.712388980384690, -1.8369701987210297e-16, 1.1924881e-08 }, 60 { 360, 6.283185307179586, 1.0000000000000000, 999 }, 61 }; 62 63 /* 64 * cosl(3) 65 */ 66 ATF_TC(cosl_angles); 67 ATF_TC_HEAD(cosl_angles, tc) 68 { 69 atf_tc_set_md_var(tc, "descr", "Test some selected angles"); 70 } 71 72 ATF_TC_BODY(cosl_angles, tc) 73 { 74 /* 75 * XXX The given data is for double, so take that 76 * into account and expect less precise results.. 77 */ 78 const long double eps = DBL_EPSILON; 79 size_t i; 80 81 for (i = 0; i < __arraycount(angles); i++) { 82 int deg = angles[i].angle; 83 long double theta = angles[i].x; 84 long double cos_theta = angles[i].y; 85 86 assert(cos_theta != 0); 87 if (!(fabsl((cosl(theta) - cos_theta)/cos_theta) <= eps)) { 88 atf_tc_fail_nonfatal("cos(%d deg = %.17Lg) = %.17Lg" 89 " != %.17Lg", 90 deg, theta, cosl(theta), cos_theta); 91 } 92 } 93 } 94 95 ATF_TC(cosl_nan); 96 ATF_TC_HEAD(cosl_nan, tc) 97 { 98 atf_tc_set_md_var(tc, "descr", "Test cosl(NaN) == NaN"); 99 } 100 101 ATF_TC_BODY(cosl_nan, tc) 102 { 103 const long double x = 0.0L / 0.0L; 104 105 ATF_CHECK(isnan(x) != 0); 106 ATF_CHECK(isnan(cosl(x)) != 0); 107 } 108 109 ATF_TC(cosl_inf_neg); 110 ATF_TC_HEAD(cosl_inf_neg, tc) 111 { 112 atf_tc_set_md_var(tc, "descr", "Test cosl(-Inf) == NaN"); 113 } 114 115 ATF_TC_BODY(cosl_inf_neg, tc) 116 { 117 const volatile long double x = -1.0L / 0.0L; 118 const long double y = cosl(x); 119 120 ATF_CHECK_MSG(isnan(y), "y=%La", y); 121 } 122 123 ATF_TC(cosl_inf_pos); 124 ATF_TC_HEAD(cosl_inf_pos, tc) 125 { 126 atf_tc_set_md_var(tc, "descr", "Test cosl(+Inf) == NaN"); 127 } 128 129 ATF_TC_BODY(cosl_inf_pos, tc) 130 { 131 const volatile long double x = 1.0L / 0.0L; 132 const long double y = cosl(x); 133 134 ATF_CHECK_MSG(isnan(y), "y=%La", y); 135 } 136 137 ATF_TC(cosl_zero_neg); 138 ATF_TC_HEAD(cosl_zero_neg, tc) 139 { 140 atf_tc_set_md_var(tc, "descr", "Test cosl(-0.0) == 1.0"); 141 } 142 143 ATF_TC_BODY(cosl_zero_neg, tc) 144 { 145 const long double x = -0.0L; 146 147 ATF_CHECK(cosl(x) == 1.0); 148 } 149 150 ATF_TC(cosl_zero_pos); 151 ATF_TC_HEAD(cosl_zero_pos, tc) 152 { 153 atf_tc_set_md_var(tc, "descr", "Test cosl(+0.0) == 1.0"); 154 } 155 156 ATF_TC_BODY(cosl_zero_pos, tc) 157 { 158 const long double x = 0.0L; 159 160 ATF_CHECK(cosl(x) == 1.0); 161 } 162 163 /* 164 * cos(3) 165 */ 166 ATF_TC(cos_angles); 167 ATF_TC_HEAD(cos_angles, tc) 168 { 169 atf_tc_set_md_var(tc, "descr", "Test some selected angles"); 170 } 171 172 ATF_TC_BODY(cos_angles, tc) 173 { 174 const double eps = DBL_EPSILON; 175 size_t i; 176 177 for (i = 0; i < __arraycount(angles); i++) { 178 int deg = angles[i].angle; 179 double theta = angles[i].x; 180 double cos_theta = angles[i].y; 181 182 assert(cos_theta != 0); 183 if (!(fabs((cos(theta) - cos_theta)/cos_theta) <= eps)) { 184 atf_tc_fail_nonfatal("cos(%d deg = %.17g) = %.17g" 185 " != %.17g", 186 deg, theta, cos(theta), cos_theta); 187 } 188 } 189 } 190 191 ATF_TC(cos_nan); 192 ATF_TC_HEAD(cos_nan, tc) 193 { 194 atf_tc_set_md_var(tc, "descr", "Test cos(NaN) == NaN"); 195 } 196 197 ATF_TC_BODY(cos_nan, tc) 198 { 199 const double x = 0.0L / 0.0L; 200 201 ATF_CHECK(isnan(x) != 0); 202 ATF_CHECK(isnan(cos(x)) != 0); 203 } 204 205 ATF_TC(cos_inf_neg); 206 ATF_TC_HEAD(cos_inf_neg, tc) 207 { 208 atf_tc_set_md_var(tc, "descr", "Test cos(-Inf) == NaN"); 209 } 210 211 ATF_TC_BODY(cos_inf_neg, tc) 212 { 213 const volatile double x = -1.0 / 0.0; 214 const double y = cos(x); 215 216 ATF_CHECK_MSG(isnan(y), "y=%a", y); 217 } 218 219 ATF_TC(cos_inf_pos); 220 ATF_TC_HEAD(cos_inf_pos, tc) 221 { 222 atf_tc_set_md_var(tc, "descr", "Test cos(+Inf) == NaN"); 223 } 224 225 ATF_TC_BODY(cos_inf_pos, tc) 226 { 227 const volatile double x = 1.0 / 0.0; 228 const double y = cos(x); 229 230 ATF_CHECK_MSG(isnan(y), "y=%a", y); 231 } 232 233 ATF_TC(cos_zero_neg); 234 ATF_TC_HEAD(cos_zero_neg, tc) 235 { 236 atf_tc_set_md_var(tc, "descr", "Test cos(-0.0) == 1.0"); 237 } 238 239 ATF_TC_BODY(cos_zero_neg, tc) 240 { 241 const double x = -0.0L; 242 243 ATF_CHECK(cos(x) == 1.0); 244 } 245 246 ATF_TC(cos_zero_pos); 247 ATF_TC_HEAD(cos_zero_pos, tc) 248 { 249 atf_tc_set_md_var(tc, "descr", "Test cos(+0.0) == 1.0"); 250 } 251 252 ATF_TC_BODY(cos_zero_pos, tc) 253 { 254 const double x = 0.0L; 255 256 ATF_CHECK(cos(x) == 1.0); 257 } 258 259 /* 260 * cosf(3) 261 */ 262 ATF_TC(cosf_angles); 263 ATF_TC_HEAD(cosf_angles, tc) 264 { 265 atf_tc_set_md_var(tc, "descr", "Test some selected angles"); 266 } 267 268 ATF_TC_BODY(cosf_angles, tc) 269 { 270 const float eps = FLT_EPSILON; 271 size_t i; 272 273 for (i = 0; i < __arraycount(angles); i++) { 274 int deg = angles[i].angle; 275 float theta = angles[i].x; 276 float cos_theta = angles[i].fy; 277 278 /* 279 * Force rounding to float even if FLT_EVAL_METHOD=2, 280 * as is the case on i386. 281 * 282 * The volatile should not be necessary, by C99 Sec. 283 * 5.2.4.2.2. para. 8 on p. 24 which specifies that 284 * assignment and cast remove all extra range and 285 * precision, but is needed when we compile with 286 * -std=gnu99 which doesn't implement this semantics. 287 */ 288 volatile float result = cosf(theta); 289 290 if (cos_theta == 999) 291 cos_theta = angles[i].y; 292 293 assert(cos_theta != 0); 294 if (!(fabsf((result - cos_theta)/cos_theta) <= eps)) { 295 atf_tc_fail_nonfatal("cosf(%d deg = %.8g) = %.8g" 296 " != %.8g", deg, theta, result, cos_theta); 297 } 298 } 299 } 300 301 ATF_TC(cosf_nan); 302 ATF_TC_HEAD(cosf_nan, tc) 303 { 304 atf_tc_set_md_var(tc, "descr", "Test cosf(NaN) == NaN"); 305 } 306 307 ATF_TC_BODY(cosf_nan, tc) 308 { 309 const float x = 0.0L / 0.0L; 310 311 ATF_CHECK(isnan(x) != 0); 312 ATF_CHECK(isnan(cosf(x)) != 0); 313 } 314 315 ATF_TC(cosf_inf_neg); 316 ATF_TC_HEAD(cosf_inf_neg, tc) 317 { 318 atf_tc_set_md_var(tc, "descr", "Test cosf(-Inf) == NaN"); 319 } 320 321 ATF_TC_BODY(cosf_inf_neg, tc) 322 { 323 const volatile float x = -1.0f / 0.0f; 324 const float y = cosf(x); 325 326 ATF_CHECK_MSG(isnan(y), "y=%a", y); 327 } 328 329 ATF_TC(cosf_inf_pos); 330 ATF_TC_HEAD(cosf_inf_pos, tc) 331 { 332 atf_tc_set_md_var(tc, "descr", "Test cosf(+Inf) == NaN"); 333 } 334 335 ATF_TC_BODY(cosf_inf_pos, tc) 336 { 337 const volatile float x = 1.0f / 0.0f; 338 const float y = cosf(x); 339 340 ATF_CHECK_MSG(isnan(y), "y=%a", y); 341 } 342 343 344 ATF_TC(cosf_zero_neg); 345 ATF_TC_HEAD(cosf_zero_neg, tc) 346 { 347 atf_tc_set_md_var(tc, "descr", "Test cosf(-0.0) == 1.0"); 348 } 349 350 ATF_TC_BODY(cosf_zero_neg, tc) 351 { 352 const float x = -0.0L; 353 354 ATF_CHECK(cosf(x) == 1.0); 355 } 356 357 ATF_TC(cosf_zero_pos); 358 ATF_TC_HEAD(cosf_zero_pos, tc) 359 { 360 atf_tc_set_md_var(tc, "descr", "Test cosf(+0.0) == 1.0"); 361 } 362 363 ATF_TC_BODY(cosf_zero_pos, tc) 364 { 365 const float x = 0.0L; 366 367 ATF_CHECK(cosf(x) == 1.0); 368 } 369 370 ATF_TP_ADD_TCS(tp) 371 { 372 373 ATF_TP_ADD_TC(tp, cosl_angles); 374 ATF_TP_ADD_TC(tp, cosl_nan); 375 ATF_TP_ADD_TC(tp, cosl_inf_neg); 376 ATF_TP_ADD_TC(tp, cosl_inf_pos); 377 ATF_TP_ADD_TC(tp, cosl_zero_neg); 378 ATF_TP_ADD_TC(tp, cosl_zero_pos); 379 380 ATF_TP_ADD_TC(tp, cos_angles); 381 ATF_TP_ADD_TC(tp, cos_nan); 382 ATF_TP_ADD_TC(tp, cos_inf_neg); 383 ATF_TP_ADD_TC(tp, cos_inf_pos); 384 ATF_TP_ADD_TC(tp, cos_zero_neg); 385 ATF_TP_ADD_TC(tp, cos_zero_pos); 386 387 ATF_TP_ADD_TC(tp, cosf_angles); 388 ATF_TP_ADD_TC(tp, cosf_nan); 389 ATF_TP_ADD_TC(tp, cosf_inf_neg); 390 ATF_TP_ADD_TC(tp, cosf_inf_pos); 391 ATF_TP_ADD_TC(tp, cosf_zero_neg); 392 ATF_TP_ADD_TC(tp, cosf_zero_pos); 393 394 return atf_no_error(); 395 } 396