1 /* $NetBSD: t_cos.c,v 1.7 2018/11/10 23:04:16 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 * cos(3) 65 */ 66 ATF_TC(cos_angles); 67 ATF_TC_HEAD(cos_angles, tc) 68 { 69 atf_tc_set_md_var(tc, "descr", "Test some selected angles"); 70 } 71 72 ATF_TC_BODY(cos_angles, tc) 73 { 74 const double eps = DBL_EPSILON; 75 size_t i; 76 77 for (i = 0; i < __arraycount(angles); i++) { 78 int deg = angles[i].angle; 79 double theta = angles[i].x; 80 double cos_theta = angles[i].y; 81 82 assert(cos_theta != 0); 83 if (!(fabs((cos(theta) - cos_theta)/cos_theta) <= eps)) { 84 atf_tc_fail_nonfatal("cos(%d deg = %.17g) = %.17g" 85 " != %.17g", 86 deg, theta, cos(theta), cos_theta); 87 } 88 } 89 } 90 91 ATF_TC(cos_nan); 92 ATF_TC_HEAD(cos_nan, tc) 93 { 94 atf_tc_set_md_var(tc, "descr", "Test cos(NaN) == NaN"); 95 } 96 97 ATF_TC_BODY(cos_nan, tc) 98 { 99 const double x = 0.0L / 0.0L; 100 101 ATF_CHECK(isnan(x) != 0); 102 ATF_CHECK(isnan(cos(x)) != 0); 103 } 104 105 ATF_TC(cos_inf_neg); 106 ATF_TC_HEAD(cos_inf_neg, tc) 107 { 108 atf_tc_set_md_var(tc, "descr", "Test cos(-Inf) == NaN"); 109 } 110 111 ATF_TC_BODY(cos_inf_neg, tc) 112 { 113 const double x = -1.0L / 0.0L; 114 115 ATF_CHECK(isnan(cos(x)) != 0); 116 } 117 118 ATF_TC(cos_inf_pos); 119 ATF_TC_HEAD(cos_inf_pos, tc) 120 { 121 atf_tc_set_md_var(tc, "descr", "Test cos(+Inf) == NaN"); 122 } 123 124 ATF_TC_BODY(cos_inf_pos, tc) 125 { 126 const double x = 1.0L / 0.0L; 127 128 ATF_CHECK(isnan(cos(x)) != 0); 129 } 130 131 132 ATF_TC(cos_zero_neg); 133 ATF_TC_HEAD(cos_zero_neg, tc) 134 { 135 atf_tc_set_md_var(tc, "descr", "Test cos(-0.0) == 1.0"); 136 } 137 138 ATF_TC_BODY(cos_zero_neg, tc) 139 { 140 const double x = -0.0L; 141 142 ATF_CHECK(cos(x) == 1.0); 143 } 144 145 ATF_TC(cos_zero_pos); 146 ATF_TC_HEAD(cos_zero_pos, tc) 147 { 148 atf_tc_set_md_var(tc, "descr", "Test cos(+0.0) == 1.0"); 149 } 150 151 ATF_TC_BODY(cos_zero_pos, tc) 152 { 153 const double x = 0.0L; 154 155 ATF_CHECK(cos(x) == 1.0); 156 } 157 158 /* 159 * cosf(3) 160 */ 161 ATF_TC(cosf_angles); 162 ATF_TC_HEAD(cosf_angles, tc) 163 { 164 atf_tc_set_md_var(tc, "descr", "Test some selected angles"); 165 } 166 167 ATF_TC_BODY(cosf_angles, tc) 168 { 169 const float eps = FLT_EPSILON; 170 size_t i; 171 172 for (i = 0; i < __arraycount(angles); i++) { 173 int deg = angles[i].angle; 174 float theta = angles[i].x; 175 float cos_theta = angles[i].fy; 176 177 if (cos_theta == 999) 178 cos_theta = angles[i].y; 179 180 assert(cos_theta != 0); 181 if (!(fabsf((cosf(theta) - cos_theta)/cos_theta) <= eps)) { 182 atf_tc_fail_nonfatal("cosf(%d deg = %.8g) = %.8g" 183 " != %.8g", deg, theta, cos(theta), cos_theta); 184 } 185 } 186 } 187 188 ATF_TC(cosf_nan); 189 ATF_TC_HEAD(cosf_nan, tc) 190 { 191 atf_tc_set_md_var(tc, "descr", "Test cosf(NaN) == NaN"); 192 } 193 194 ATF_TC_BODY(cosf_nan, tc) 195 { 196 const float x = 0.0L / 0.0L; 197 198 ATF_CHECK(isnan(x) != 0); 199 ATF_CHECK(isnan(cosf(x)) != 0); 200 } 201 202 ATF_TC(cosf_inf_neg); 203 ATF_TC_HEAD(cosf_inf_neg, tc) 204 { 205 atf_tc_set_md_var(tc, "descr", "Test cosf(-Inf) == NaN"); 206 } 207 208 ATF_TC_BODY(cosf_inf_neg, tc) 209 { 210 const float x = -1.0L / 0.0L; 211 212 if (isnan(cosf(x)) == 0) { 213 atf_tc_expect_fail("PR lib/45362"); 214 atf_tc_fail("cosf(-Inf) != NaN"); 215 } 216 } 217 218 ATF_TC(cosf_inf_pos); 219 ATF_TC_HEAD(cosf_inf_pos, tc) 220 { 221 atf_tc_set_md_var(tc, "descr", "Test cosf(+Inf) == NaN"); 222 } 223 224 ATF_TC_BODY(cosf_inf_pos, tc) 225 { 226 const float x = 1.0L / 0.0L; 227 228 if (isnan(cosf(x)) == 0) { 229 atf_tc_expect_fail("PR lib/45362"); 230 atf_tc_fail("cosf(+Inf) != NaN"); 231 } 232 } 233 234 235 ATF_TC(cosf_zero_neg); 236 ATF_TC_HEAD(cosf_zero_neg, tc) 237 { 238 atf_tc_set_md_var(tc, "descr", "Test cosf(-0.0) == 1.0"); 239 } 240 241 ATF_TC_BODY(cosf_zero_neg, tc) 242 { 243 const float x = -0.0L; 244 245 ATF_CHECK(cosf(x) == 1.0); 246 } 247 248 ATF_TC(cosf_zero_pos); 249 ATF_TC_HEAD(cosf_zero_pos, tc) 250 { 251 atf_tc_set_md_var(tc, "descr", "Test cosf(+0.0) == 1.0"); 252 } 253 254 ATF_TC_BODY(cosf_zero_pos, tc) 255 { 256 const float x = 0.0L; 257 258 ATF_CHECK(cosf(x) == 1.0); 259 } 260 261 ATF_TP_ADD_TCS(tp) 262 { 263 264 ATF_TP_ADD_TC(tp, cos_angles); 265 ATF_TP_ADD_TC(tp, cos_nan); 266 ATF_TP_ADD_TC(tp, cos_inf_neg); 267 ATF_TP_ADD_TC(tp, cos_inf_pos); 268 ATF_TP_ADD_TC(tp, cos_zero_neg); 269 ATF_TP_ADD_TC(tp, cos_zero_pos); 270 271 ATF_TP_ADD_TC(tp, cosf_angles); 272 ATF_TP_ADD_TC(tp, cosf_nan); 273 ATF_TP_ADD_TC(tp, cosf_inf_neg); 274 ATF_TP_ADD_TC(tp, cosf_inf_pos); 275 ATF_TP_ADD_TC(tp, cosf_zero_neg); 276 ATF_TP_ADD_TC(tp, cosf_zero_pos); 277 278 return atf_no_error(); 279 } 280