1/* $NetBSD: t_tan.c,v 1.7 2018/11/07 04:00:13 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 37static const struct { 38 int angle; 39 double x; 40 double y; 41 float fy; 42} angles[] = { 43 { -180, -3.141592653589793, 1.2246467991473532e-16, -8.7422777e-08 }, 44 { -135, -2.356194490192345, 1.0000000000000002, 999 }, 45 { -45, -0.785398163397448, -0.9999999999999992, 999 }, 46 { 0, 0.000000000000000, 0.0000000000000000, 999 }, 47 { 30, 0.5235987755982988, 0.57735026918962573, 999 }, 48 { 45, 0.785398163397448, 0.9999999999999992, 999 }, 49 { 60, 1.047197551196598, 1.7320508075688785, 1.7320509 }, 50 { 120, 2.094395102393195, -1.7320508075688801, -1.7320505 }, 51 { 135, 2.356194490192345, -1.0000000000000002, 999 }, 52 { 150, 2.617993877991494, -0.57735026918962629, -0.57735032 }, 53 { 180, 3.141592653589793, -1.2246467991473532e-16, 8.7422777e-08 }, 54 { 360, 6.283185307179586, -2.4492935982947064e-16, 1.7484555e-07 }, 55}; 56 57/* 58 * tan(3) 59 */ 60ATF_TC(tan_angles); 61ATF_TC_HEAD(tan_angles, tc) 62{ 63 atf_tc_set_md_var(tc, "descr", "Test some selected angles"); 64} 65 66ATF_TC_BODY(tan_angles, tc) 67{ 68 const double eps = DBL_EPSILON; 69 size_t i; 70 71 for (i = 0; i < __arraycount(angles); i++) { 72 int deg = angles[i].angle; 73 double theta = angles[i].x; 74 double tan_theta = angles[i].y; 75 bool ok; 76 77 if (theta == 0) { 78 /* Should be computed exactly. */ 79 assert(tan_theta == 0); 80 ok = (tan(theta) == 0); 81 } else { 82 assert(tan_theta != 0); 83 ok = (fabs((tan(theta) - tan_theta)/tan_theta) <= eps); 84 } 85 86 if (!ok) { 87 atf_tc_fail_nonfatal("tan(%d deg = %.17g) = %.17g" 88 " != %.17g", 89 deg, theta, tan(theta), tan_theta); 90 } 91 } 92} 93 94ATF_TC(tan_nan); 95ATF_TC_HEAD(tan_nan, tc) 96{ 97 atf_tc_set_md_var(tc, "descr", "Test tan(NaN) == NaN"); 98} 99 100ATF_TC_BODY(tan_nan, tc) 101{ 102 const double x = 0.0L / 0.0L; 103 104 ATF_CHECK(isnan(x) != 0); 105 ATF_CHECK(isnan(tan(x)) != 0); 106} 107 108ATF_TC(tan_inf_neg); 109ATF_TC_HEAD(tan_inf_neg, tc) 110{ 111 atf_tc_set_md_var(tc, "descr", "Test tan(-Inf) == NaN"); 112} 113 114ATF_TC_BODY(tan_inf_neg, tc) 115{ 116 const double x = -1.0L / 0.0L; 117 118 ATF_CHECK(isnan(tan(x)) != 0); 119} 120 121ATF_TC(tan_inf_pos); 122ATF_TC_HEAD(tan_inf_pos, tc) 123{ 124 atf_tc_set_md_var(tc, "descr", "Test tan(+Inf) == NaN"); 125} 126 127ATF_TC_BODY(tan_inf_pos, tc) 128{ 129 const double x = 1.0L / 0.0L; 130 131 ATF_CHECK(isnan(tan(x)) != 0); 132} 133 134 135ATF_TC(tan_zero_neg); 136ATF_TC_HEAD(tan_zero_neg, tc) 137{ 138 atf_tc_set_md_var(tc, "descr", "Test tan(-0.0) == -0.0"); 139} 140 141ATF_TC_BODY(tan_zero_neg, tc) 142{ 143 const double x = -0.0L; 144 145 ATF_CHECK(tan(x) == x); 146} 147 148ATF_TC(tan_zero_pos); 149ATF_TC_HEAD(tan_zero_pos, tc) 150{ 151 atf_tc_set_md_var(tc, "descr", "Test tan(+0.0) == +0.0"); 152} 153 154ATF_TC_BODY(tan_zero_pos, tc) 155{ 156 const double x = 0.0L; 157 158 ATF_CHECK(tan(x) == x); 159} 160 161/* 162 * tanf(3) 163 */ 164ATF_TC(tanf_angles); 165ATF_TC_HEAD(tanf_angles, tc) 166{ 167 atf_tc_set_md_var(tc, "descr", "Test some selected angles"); 168} 169 170ATF_TC_BODY(tanf_angles, tc) 171{ 172 const float eps = FLT_EPSILON; 173 size_t i; 174 175 for (i = 0; i < __arraycount(angles); i++) { 176 int deg = angles[i].angle; 177 float theta = angles[i].x; 178 float tan_theta = angles[i].fy; 179 bool ok; 180 181 if (tan_theta == 999) 182 tan_theta = angles[i].y; 183 184 if (theta == 0) { 185 /* Should be computed exactly. */ 186 assert(tan_theta == 0); 187 ok = (tan(theta) == 0); 188 } else { 189 assert(tan_theta != 0); 190 ok = (fabsf((tanf(theta) - tan_theta)/tan_theta) 191 <= eps); 192 } 193 194 if (!ok) { 195 atf_tc_fail_nonfatal("tanf(%d deg) = %.8g != %.8g", 196 deg, tanf(theta), tan_theta); 197 } 198 } 199} 200 201ATF_TC(tanf_nan); 202ATF_TC_HEAD(tanf_nan, tc) 203{ 204 atf_tc_set_md_var(tc, "descr", "Test tanf(NaN) == NaN"); 205} 206 207ATF_TC_BODY(tanf_nan, tc) 208{ 209 const float x = 0.0L / 0.0L; 210 211 ATF_CHECK(isnan(x) != 0); 212 ATF_CHECK(isnan(tanf(x)) != 0); 213} 214 215ATF_TC(tanf_inf_neg); 216ATF_TC_HEAD(tanf_inf_neg, tc) 217{ 218 atf_tc_set_md_var(tc, "descr", "Test tanf(-Inf) == NaN"); 219} 220 221ATF_TC_BODY(tanf_inf_neg, tc) 222{ 223 const float x = -1.0L / 0.0L; 224 225 if (isnan(tanf(x)) == 0) { 226 atf_tc_expect_fail("PR lib/45362"); 227 atf_tc_fail("tanf(-Inf) != NaN"); 228 } 229} 230 231ATF_TC(tanf_inf_pos); 232ATF_TC_HEAD(tanf_inf_pos, tc) 233{ 234 atf_tc_set_md_var(tc, "descr", "Test tanf(+Inf) == NaN"); 235} 236 237ATF_TC_BODY(tanf_inf_pos, tc) 238{ 239 const float x = 1.0L / 0.0L; 240 241 if (isnan(tanf(x)) == 0) { 242 atf_tc_expect_fail("PR lib/45362"); 243 atf_tc_fail("tanf(+Inf) != NaN"); 244 } 245} 246 247 248ATF_TC(tanf_zero_neg); 249ATF_TC_HEAD(tanf_zero_neg, tc) 250{ 251 atf_tc_set_md_var(tc, "descr", "Test tanf(-0.0) == -0.0"); 252} 253 254ATF_TC_BODY(tanf_zero_neg, tc) 255{ 256 const float x = -0.0L; 257 258 ATF_CHECK(tanf(x) == x); 259} 260 261ATF_TC(tanf_zero_pos); 262ATF_TC_HEAD(tanf_zero_pos, tc) 263{ 264 atf_tc_set_md_var(tc, "descr", "Test tanf(+0.0) == +0.0"); 265} 266 267ATF_TC_BODY(tanf_zero_pos, tc) 268{ 269 const float x = 0.0L; 270 271 ATF_CHECK(tanf(x) == x); 272} 273 274ATF_TP_ADD_TCS(tp) 275{ 276 277 ATF_TP_ADD_TC(tp, tan_angles); 278 ATF_TP_ADD_TC(tp, tan_nan); 279 ATF_TP_ADD_TC(tp, tan_inf_neg); 280 ATF_TP_ADD_TC(tp, tan_inf_pos); 281 ATF_TP_ADD_TC(tp, tan_zero_neg); 282 ATF_TP_ADD_TC(tp, tan_zero_pos); 283 284 ATF_TP_ADD_TC(tp, tanf_angles); 285 ATF_TP_ADD_TC(tp, tanf_nan); 286 ATF_TP_ADD_TC(tp, tanf_inf_neg); 287 ATF_TP_ADD_TC(tp, tanf_inf_pos); 288 ATF_TP_ADD_TC(tp, tanf_zero_neg); 289 ATF_TP_ADD_TC(tp, tanf_zero_pos); 290 291 return atf_no_error(); 292} 293