Home | History | Annotate | Line # | Download | only in libm
      1 /* $NetBSD: t_tan.c,v 1.8 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.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  */
     60 ATF_TC(tan_angles);
     61 ATF_TC_HEAD(tan_angles, tc)
     62 {
     63 	atf_tc_set_md_var(tc, "descr", "Test some selected angles");
     64 }
     65 
     66 ATF_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 
     94 ATF_TC(tan_nan);
     95 ATF_TC_HEAD(tan_nan, tc)
     96 {
     97 	atf_tc_set_md_var(tc, "descr", "Test tan(NaN) == NaN");
     98 }
     99 
    100 ATF_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 
    108 ATF_TC(tan_inf_neg);
    109 ATF_TC_HEAD(tan_inf_neg, tc)
    110 {
    111 	atf_tc_set_md_var(tc, "descr", "Test tan(-Inf) == NaN");
    112 }
    113 
    114 ATF_TC_BODY(tan_inf_neg, tc)
    115 {
    116 	const volatile double x = -1.0 / 0.0;
    117 	const double y = tan(x);
    118 
    119 	ATF_CHECK_MSG(isnan(y), "y=%a", y);
    120 }
    121 
    122 ATF_TC(tan_inf_pos);
    123 ATF_TC_HEAD(tan_inf_pos, tc)
    124 {
    125 	atf_tc_set_md_var(tc, "descr", "Test tan(+Inf) == NaN");
    126 }
    127 
    128 ATF_TC_BODY(tan_inf_pos, tc)
    129 {
    130 	const volatile double x = 1.0 / 0.0;
    131 	const double y = tan(x);
    132 
    133 	ATF_CHECK_MSG(isnan(y), "y=%a", y);
    134 }
    135 
    136 
    137 ATF_TC(tan_zero_neg);
    138 ATF_TC_HEAD(tan_zero_neg, tc)
    139 {
    140 	atf_tc_set_md_var(tc, "descr", "Test tan(-0.0) == -0.0");
    141 }
    142 
    143 ATF_TC_BODY(tan_zero_neg, tc)
    144 {
    145 	const double x = -0.0L;
    146 
    147 	ATF_CHECK(tan(x) == x);
    148 }
    149 
    150 ATF_TC(tan_zero_pos);
    151 ATF_TC_HEAD(tan_zero_pos, tc)
    152 {
    153 	atf_tc_set_md_var(tc, "descr", "Test tan(+0.0) == +0.0");
    154 }
    155 
    156 ATF_TC_BODY(tan_zero_pos, tc)
    157 {
    158 	const double x = 0.0L;
    159 
    160 	ATF_CHECK(tan(x) == x);
    161 }
    162 
    163 /*
    164  * tanf(3)
    165  */
    166 ATF_TC(tanf_angles);
    167 ATF_TC_HEAD(tanf_angles, tc)
    168 {
    169 	atf_tc_set_md_var(tc, "descr", "Test some selected angles");
    170 }
    171 
    172 ATF_TC_BODY(tanf_angles, tc)
    173 {
    174 	const float eps = FLT_EPSILON;
    175 	size_t i;
    176 
    177 	for (i = 0; i < __arraycount(angles); i++) {
    178 		int deg = angles[i].angle;
    179 		float theta = angles[i].x;
    180 		float tan_theta = angles[i].fy;
    181 		bool ok;
    182 
    183 		if (tan_theta == 999)
    184 			tan_theta = angles[i].y;
    185 
    186 		if (theta == 0) {
    187 			/* Should be computed exactly.  */
    188 			assert(tan_theta == 0);
    189 			ok = (tan(theta) == 0);
    190 		} else {
    191 			assert(tan_theta != 0);
    192 			ok = (fabsf((tanf(theta) - tan_theta)/tan_theta)
    193 			    <= eps);
    194 		}
    195 
    196 		if (!ok) {
    197 			atf_tc_fail_nonfatal("tanf(%d deg) = %.8g != %.8g",
    198 			    deg, tanf(theta), tan_theta);
    199 		}
    200 	}
    201 }
    202 
    203 ATF_TC(tanf_nan);
    204 ATF_TC_HEAD(tanf_nan, tc)
    205 {
    206 	atf_tc_set_md_var(tc, "descr", "Test tanf(NaN) == NaN");
    207 }
    208 
    209 ATF_TC_BODY(tanf_nan, tc)
    210 {
    211 	const float x = 0.0L / 0.0L;
    212 
    213 	ATF_CHECK(isnan(x) != 0);
    214 	ATF_CHECK(isnan(tanf(x)) != 0);
    215 }
    216 
    217 ATF_TC(tanf_inf_neg);
    218 ATF_TC_HEAD(tanf_inf_neg, tc)
    219 {
    220 	atf_tc_set_md_var(tc, "descr", "Test tanf(-Inf) == NaN");
    221 }
    222 
    223 ATF_TC_BODY(tanf_inf_neg, tc)
    224 {
    225 	const volatile float x = -1.0f / 0.0f;
    226 	const float y = tanf(x);
    227 
    228 	ATF_CHECK_MSG(isnan(y), "y=%a", y);
    229 }
    230 
    231 ATF_TC(tanf_inf_pos);
    232 ATF_TC_HEAD(tanf_inf_pos, tc)
    233 {
    234 	atf_tc_set_md_var(tc, "descr", "Test tanf(+Inf) == NaN");
    235 }
    236 
    237 ATF_TC_BODY(tanf_inf_pos, tc)
    238 {
    239 	const volatile float x = 1.0f / 0.0f;
    240 	const float y = tanf(x);
    241 
    242 	ATF_CHECK_MSG(isnan(y), "y=%a", y);
    243 }
    244 
    245 
    246 ATF_TC(tanf_zero_neg);
    247 ATF_TC_HEAD(tanf_zero_neg, tc)
    248 {
    249 	atf_tc_set_md_var(tc, "descr", "Test tanf(-0.0) == -0.0");
    250 }
    251 
    252 ATF_TC_BODY(tanf_zero_neg, tc)
    253 {
    254 	const float x = -0.0L;
    255 
    256 	ATF_CHECK(tanf(x) == x);
    257 }
    258 
    259 ATF_TC(tanf_zero_pos);
    260 ATF_TC_HEAD(tanf_zero_pos, tc)
    261 {
    262 	atf_tc_set_md_var(tc, "descr", "Test tanf(+0.0) == +0.0");
    263 }
    264 
    265 ATF_TC_BODY(tanf_zero_pos, tc)
    266 {
    267 	const float x = 0.0L;
    268 
    269 	ATF_CHECK(tanf(x) == x);
    270 }
    271 
    272 ATF_TP_ADD_TCS(tp)
    273 {
    274 
    275 	ATF_TP_ADD_TC(tp, tan_angles);
    276 	ATF_TP_ADD_TC(tp, tan_nan);
    277 	ATF_TP_ADD_TC(tp, tan_inf_neg);
    278 	ATF_TP_ADD_TC(tp, tan_inf_pos);
    279 	ATF_TP_ADD_TC(tp, tan_zero_neg);
    280 	ATF_TP_ADD_TC(tp, tan_zero_pos);
    281 
    282 	ATF_TP_ADD_TC(tp, tanf_angles);
    283 	ATF_TP_ADD_TC(tp, tanf_nan);
    284 	ATF_TP_ADD_TC(tp, tanf_inf_neg);
    285 	ATF_TP_ADD_TC(tp, tanf_inf_pos);
    286 	ATF_TP_ADD_TC(tp, tanf_zero_neg);
    287 	ATF_TP_ADD_TC(tp, tanf_zero_pos);
    288 
    289 	return atf_no_error();
    290 }
    291