xref: /minix3/tests/lib/libm/t_tan.c (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1*0a6a1f1dSLionel Sambuc /* $NetBSD: t_tan.c,v 1.5 2014/03/03 10:39:08 martin Exp $ */
211be35a1SLionel Sambuc 
311be35a1SLionel Sambuc /*-
411be35a1SLionel Sambuc  * Copyright (c) 2011 The NetBSD Foundation, Inc.
511be35a1SLionel Sambuc  * All rights reserved.
611be35a1SLionel Sambuc  *
711be35a1SLionel Sambuc  * This code is derived from software contributed to The NetBSD Foundation
811be35a1SLionel Sambuc  * by Jukka Ruohonen.
911be35a1SLionel Sambuc  *
1011be35a1SLionel Sambuc  * Redistribution and use in source and binary forms, with or without
1111be35a1SLionel Sambuc  * modification, are permitted provided that the following conditions
1211be35a1SLionel Sambuc  * are met:
1311be35a1SLionel Sambuc  * 1. Redistributions of source code must retain the above copyright
1411be35a1SLionel Sambuc  *    notice, this list of conditions and the following disclaimer.
1511be35a1SLionel Sambuc  * 2. Redistributions in binary form must reproduce the above copyright
1611be35a1SLionel Sambuc  *    notice, this list of conditions and the following disclaimer in the
1711be35a1SLionel Sambuc  *    documentation and/or other materials provided with the distribution.
1811be35a1SLionel Sambuc  *
1911be35a1SLionel Sambuc  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
2011be35a1SLionel Sambuc  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
2111be35a1SLionel Sambuc  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
2211be35a1SLionel Sambuc  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
2311be35a1SLionel Sambuc  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2411be35a1SLionel Sambuc  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2511be35a1SLionel Sambuc  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2611be35a1SLionel Sambuc  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
2711be35a1SLionel Sambuc  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
2811be35a1SLionel Sambuc  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
2911be35a1SLionel Sambuc  * POSSIBILITY OF SUCH DAMAGE.
3011be35a1SLionel Sambuc  */
3111be35a1SLionel Sambuc 
3211be35a1SLionel Sambuc #include <atf-c.h>
3311be35a1SLionel Sambuc #include <math.h>
3411be35a1SLionel Sambuc 
3511be35a1SLionel Sambuc static const struct {
3611be35a1SLionel Sambuc 	int		angle;
3711be35a1SLionel Sambuc 	double		x;
3811be35a1SLionel Sambuc 	double		y;
3911be35a1SLionel Sambuc } angles[] = {
4011be35a1SLionel Sambuc 	{ -180, -3.141592653589793,  0.0000000000000000 },
4111be35a1SLionel Sambuc 	{ -135, -2.356194490192345,  1.0000000000000000 },
4211be35a1SLionel Sambuc 	{  -45, -0.785398163397448, -1.0000000000000000 },
4311be35a1SLionel Sambuc 	{    0,  0.000000000000000,  0.0000000000000000 },
4411be35a1SLionel Sambuc 	{   30,  0.523598775598299,  0.5773502691896258 },
4511be35a1SLionel Sambuc 	{   45,  0.785398163397448,  1.0000000000000000 },
4611be35a1SLionel Sambuc 	{   60,  1.047197551196598,  1.7320508075688773 },
4711be35a1SLionel Sambuc 	{  120,  2.094395102393195, -1.7320508075688773 },
4811be35a1SLionel Sambuc 	{  135,  2.356194490192345, -1.0000000000000000 },
4911be35a1SLionel Sambuc 	{  150,  2.617993877991494, -0.5773502691896258 },
5011be35a1SLionel Sambuc 	{  180,  3.141592653589793,  0.0000000000000000 },
5111be35a1SLionel Sambuc 	{  360,  6.283185307179586,  0.0000000000000000 }
5211be35a1SLionel Sambuc };
5311be35a1SLionel Sambuc 
5411be35a1SLionel Sambuc /*
5511be35a1SLionel Sambuc  * tan(3)
5611be35a1SLionel Sambuc  */
5711be35a1SLionel Sambuc ATF_TC(tan_angles);
ATF_TC_HEAD(tan_angles,tc)5811be35a1SLionel Sambuc ATF_TC_HEAD(tan_angles, tc)
5911be35a1SLionel Sambuc {
6011be35a1SLionel Sambuc 	atf_tc_set_md_var(tc, "descr", "Test some selected angles");
6111be35a1SLionel Sambuc }
6211be35a1SLionel Sambuc 
ATF_TC_BODY(tan_angles,tc)6311be35a1SLionel Sambuc ATF_TC_BODY(tan_angles, tc)
6411be35a1SLionel Sambuc {
6511be35a1SLionel Sambuc 	const double eps = 1.0e-14;
6611be35a1SLionel Sambuc 	size_t i;
6711be35a1SLionel Sambuc 
6811be35a1SLionel Sambuc 	for (i = 0; i < __arraycount(angles); i++) {
6911be35a1SLionel Sambuc 
7011be35a1SLionel Sambuc 		if (fabs(tan(angles[i].x) - angles[i].y) > eps)
7111be35a1SLionel Sambuc 			atf_tc_fail_nonfatal("tan(%d deg) != %0.01f",
7211be35a1SLionel Sambuc 			    angles[i].angle, angles[i].y);
7311be35a1SLionel Sambuc 	}
7411be35a1SLionel Sambuc }
7511be35a1SLionel Sambuc 
7611be35a1SLionel Sambuc ATF_TC(tan_nan);
ATF_TC_HEAD(tan_nan,tc)7711be35a1SLionel Sambuc ATF_TC_HEAD(tan_nan, tc)
7811be35a1SLionel Sambuc {
7911be35a1SLionel Sambuc 	atf_tc_set_md_var(tc, "descr", "Test tan(NaN) == NaN");
8011be35a1SLionel Sambuc }
8111be35a1SLionel Sambuc 
ATF_TC_BODY(tan_nan,tc)8211be35a1SLionel Sambuc ATF_TC_BODY(tan_nan, tc)
8311be35a1SLionel Sambuc {
8411be35a1SLionel Sambuc 	const double x = 0.0L / 0.0L;
8511be35a1SLionel Sambuc 
8611be35a1SLionel Sambuc 	ATF_CHECK(isnan(x) != 0);
8711be35a1SLionel Sambuc 	ATF_CHECK(isnan(tan(x)) != 0);
8811be35a1SLionel Sambuc }
8911be35a1SLionel Sambuc 
9011be35a1SLionel Sambuc ATF_TC(tan_inf_neg);
ATF_TC_HEAD(tan_inf_neg,tc)9111be35a1SLionel Sambuc ATF_TC_HEAD(tan_inf_neg, tc)
9211be35a1SLionel Sambuc {
9311be35a1SLionel Sambuc 	atf_tc_set_md_var(tc, "descr", "Test tan(-Inf) == NaN");
9411be35a1SLionel Sambuc }
9511be35a1SLionel Sambuc 
ATF_TC_BODY(tan_inf_neg,tc)9611be35a1SLionel Sambuc ATF_TC_BODY(tan_inf_neg, tc)
9711be35a1SLionel Sambuc {
9811be35a1SLionel Sambuc 	const double x = -1.0L / 0.0L;
9911be35a1SLionel Sambuc 
10011be35a1SLionel Sambuc 	ATF_CHECK(isnan(tan(x)) != 0);
10111be35a1SLionel Sambuc }
10211be35a1SLionel Sambuc 
10311be35a1SLionel Sambuc ATF_TC(tan_inf_pos);
ATF_TC_HEAD(tan_inf_pos,tc)10411be35a1SLionel Sambuc ATF_TC_HEAD(tan_inf_pos, tc)
10511be35a1SLionel Sambuc {
10611be35a1SLionel Sambuc 	atf_tc_set_md_var(tc, "descr", "Test tan(+Inf) == NaN");
10711be35a1SLionel Sambuc }
10811be35a1SLionel Sambuc 
ATF_TC_BODY(tan_inf_pos,tc)10911be35a1SLionel Sambuc ATF_TC_BODY(tan_inf_pos, tc)
11011be35a1SLionel Sambuc {
11111be35a1SLionel Sambuc 	const double x = 1.0L / 0.0L;
11211be35a1SLionel Sambuc 
11311be35a1SLionel Sambuc 	ATF_CHECK(isnan(tan(x)) != 0);
11411be35a1SLionel Sambuc }
11511be35a1SLionel Sambuc 
11611be35a1SLionel Sambuc 
11711be35a1SLionel Sambuc ATF_TC(tan_zero_neg);
ATF_TC_HEAD(tan_zero_neg,tc)11811be35a1SLionel Sambuc ATF_TC_HEAD(tan_zero_neg, tc)
11911be35a1SLionel Sambuc {
12011be35a1SLionel Sambuc 	atf_tc_set_md_var(tc, "descr", "Test tan(-0.0) == -0.0");
12111be35a1SLionel Sambuc }
12211be35a1SLionel Sambuc 
ATF_TC_BODY(tan_zero_neg,tc)12311be35a1SLionel Sambuc ATF_TC_BODY(tan_zero_neg, tc)
12411be35a1SLionel Sambuc {
12511be35a1SLionel Sambuc 	const double x = -0.0L;
12611be35a1SLionel Sambuc 
12711be35a1SLionel Sambuc 	ATF_CHECK(tan(x) == x);
12811be35a1SLionel Sambuc }
12911be35a1SLionel Sambuc 
13011be35a1SLionel Sambuc ATF_TC(tan_zero_pos);
ATF_TC_HEAD(tan_zero_pos,tc)13111be35a1SLionel Sambuc ATF_TC_HEAD(tan_zero_pos, tc)
13211be35a1SLionel Sambuc {
13311be35a1SLionel Sambuc 	atf_tc_set_md_var(tc, "descr", "Test tan(+0.0) == +0.0");
13411be35a1SLionel Sambuc }
13511be35a1SLionel Sambuc 
ATF_TC_BODY(tan_zero_pos,tc)13611be35a1SLionel Sambuc ATF_TC_BODY(tan_zero_pos, tc)
13711be35a1SLionel Sambuc {
13811be35a1SLionel Sambuc 	const double x = 0.0L;
13911be35a1SLionel Sambuc 
14011be35a1SLionel Sambuc 	ATF_CHECK(tan(x) == x);
14111be35a1SLionel Sambuc }
14211be35a1SLionel Sambuc 
14311be35a1SLionel Sambuc /*
14411be35a1SLionel Sambuc  * tanf(3)
14511be35a1SLionel Sambuc  */
14611be35a1SLionel Sambuc ATF_TC(tanf_angles);
ATF_TC_HEAD(tanf_angles,tc)14711be35a1SLionel Sambuc ATF_TC_HEAD(tanf_angles, tc)
14811be35a1SLionel Sambuc {
14911be35a1SLionel Sambuc 	atf_tc_set_md_var(tc, "descr", "Test some selected angles");
15011be35a1SLionel Sambuc }
15111be35a1SLionel Sambuc 
ATF_TC_BODY(tanf_angles,tc)15211be35a1SLionel Sambuc ATF_TC_BODY(tanf_angles, tc)
15311be35a1SLionel Sambuc {
15411be35a1SLionel Sambuc 	const float eps = 1.0e-6;
15511be35a1SLionel Sambuc 	float x, y;
15611be35a1SLionel Sambuc 	size_t i;
15711be35a1SLionel Sambuc 
15811be35a1SLionel Sambuc 	for (i = 0; i < __arraycount(angles); i++) {
15911be35a1SLionel Sambuc 
16011be35a1SLionel Sambuc 		x = angles[i].x;
16111be35a1SLionel Sambuc 		y = angles[i].y;
16211be35a1SLionel Sambuc 
16311be35a1SLionel Sambuc 		if (fabsf(tanf(x) - y) > eps)
16411be35a1SLionel Sambuc 			atf_tc_fail_nonfatal("tanf(%d deg) != %0.01f",
16511be35a1SLionel Sambuc 			    angles[i].angle, angles[i].y);
16611be35a1SLionel Sambuc 	}
16711be35a1SLionel Sambuc }
16811be35a1SLionel Sambuc 
16911be35a1SLionel Sambuc ATF_TC(tanf_nan);
ATF_TC_HEAD(tanf_nan,tc)17011be35a1SLionel Sambuc ATF_TC_HEAD(tanf_nan, tc)
17111be35a1SLionel Sambuc {
17211be35a1SLionel Sambuc 	atf_tc_set_md_var(tc, "descr", "Test tanf(NaN) == NaN");
17311be35a1SLionel Sambuc }
17411be35a1SLionel Sambuc 
ATF_TC_BODY(tanf_nan,tc)17511be35a1SLionel Sambuc ATF_TC_BODY(tanf_nan, tc)
17611be35a1SLionel Sambuc {
17711be35a1SLionel Sambuc 	const float x = 0.0L / 0.0L;
17811be35a1SLionel Sambuc 
17911be35a1SLionel Sambuc 	ATF_CHECK(isnan(x) != 0);
18011be35a1SLionel Sambuc 	ATF_CHECK(isnan(tanf(x)) != 0);
18111be35a1SLionel Sambuc }
18211be35a1SLionel Sambuc 
18311be35a1SLionel Sambuc ATF_TC(tanf_inf_neg);
ATF_TC_HEAD(tanf_inf_neg,tc)18411be35a1SLionel Sambuc ATF_TC_HEAD(tanf_inf_neg, tc)
18511be35a1SLionel Sambuc {
18611be35a1SLionel Sambuc 	atf_tc_set_md_var(tc, "descr", "Test tanf(-Inf) == NaN");
18711be35a1SLionel Sambuc }
18811be35a1SLionel Sambuc 
ATF_TC_BODY(tanf_inf_neg,tc)18911be35a1SLionel Sambuc ATF_TC_BODY(tanf_inf_neg, tc)
19011be35a1SLionel Sambuc {
19111be35a1SLionel Sambuc 	const float x = -1.0L / 0.0L;
19211be35a1SLionel Sambuc 
19311be35a1SLionel Sambuc 	if (isnan(tanf(x)) == 0) {
19411be35a1SLionel Sambuc 		atf_tc_expect_fail("PR lib/45362");
19511be35a1SLionel Sambuc 		atf_tc_fail("tanf(-Inf) != NaN");
19611be35a1SLionel Sambuc 	}
19711be35a1SLionel Sambuc }
19811be35a1SLionel Sambuc 
19911be35a1SLionel Sambuc ATF_TC(tanf_inf_pos);
ATF_TC_HEAD(tanf_inf_pos,tc)20011be35a1SLionel Sambuc ATF_TC_HEAD(tanf_inf_pos, tc)
20111be35a1SLionel Sambuc {
20211be35a1SLionel Sambuc 	atf_tc_set_md_var(tc, "descr", "Test tanf(+Inf) == NaN");
20311be35a1SLionel Sambuc }
20411be35a1SLionel Sambuc 
ATF_TC_BODY(tanf_inf_pos,tc)20511be35a1SLionel Sambuc ATF_TC_BODY(tanf_inf_pos, tc)
20611be35a1SLionel Sambuc {
20711be35a1SLionel Sambuc 	const float x = 1.0L / 0.0L;
20811be35a1SLionel Sambuc 
20911be35a1SLionel Sambuc 	if (isnan(tanf(x)) == 0) {
21011be35a1SLionel Sambuc 		atf_tc_expect_fail("PR lib/45362");
21111be35a1SLionel Sambuc 		atf_tc_fail("tanf(+Inf) != NaN");
21211be35a1SLionel Sambuc 	}
21311be35a1SLionel Sambuc }
21411be35a1SLionel Sambuc 
21511be35a1SLionel Sambuc 
21611be35a1SLionel Sambuc ATF_TC(tanf_zero_neg);
ATF_TC_HEAD(tanf_zero_neg,tc)21711be35a1SLionel Sambuc ATF_TC_HEAD(tanf_zero_neg, tc)
21811be35a1SLionel Sambuc {
21911be35a1SLionel Sambuc 	atf_tc_set_md_var(tc, "descr", "Test tanf(-0.0) == -0.0");
22011be35a1SLionel Sambuc }
22111be35a1SLionel Sambuc 
ATF_TC_BODY(tanf_zero_neg,tc)22211be35a1SLionel Sambuc ATF_TC_BODY(tanf_zero_neg, tc)
22311be35a1SLionel Sambuc {
22411be35a1SLionel Sambuc 	const float x = -0.0L;
22511be35a1SLionel Sambuc 
22611be35a1SLionel Sambuc 	ATF_CHECK(tanf(x) == x);
22711be35a1SLionel Sambuc }
22811be35a1SLionel Sambuc 
22911be35a1SLionel Sambuc ATF_TC(tanf_zero_pos);
ATF_TC_HEAD(tanf_zero_pos,tc)23011be35a1SLionel Sambuc ATF_TC_HEAD(tanf_zero_pos, tc)
23111be35a1SLionel Sambuc {
23211be35a1SLionel Sambuc 	atf_tc_set_md_var(tc, "descr", "Test tanf(+0.0) == +0.0");
23311be35a1SLionel Sambuc }
23411be35a1SLionel Sambuc 
ATF_TC_BODY(tanf_zero_pos,tc)23511be35a1SLionel Sambuc ATF_TC_BODY(tanf_zero_pos, tc)
23611be35a1SLionel Sambuc {
23711be35a1SLionel Sambuc 	const float x = 0.0L;
23811be35a1SLionel Sambuc 
23911be35a1SLionel Sambuc 	ATF_CHECK(tanf(x) == x);
24011be35a1SLionel Sambuc }
24111be35a1SLionel Sambuc 
ATF_TP_ADD_TCS(tp)24211be35a1SLionel Sambuc ATF_TP_ADD_TCS(tp)
24311be35a1SLionel Sambuc {
24411be35a1SLionel Sambuc 
24511be35a1SLionel Sambuc 	ATF_TP_ADD_TC(tp, tan_angles);
24611be35a1SLionel Sambuc 	ATF_TP_ADD_TC(tp, tan_nan);
24711be35a1SLionel Sambuc 	ATF_TP_ADD_TC(tp, tan_inf_neg);
24811be35a1SLionel Sambuc 	ATF_TP_ADD_TC(tp, tan_inf_pos);
24911be35a1SLionel Sambuc 	ATF_TP_ADD_TC(tp, tan_zero_neg);
25011be35a1SLionel Sambuc 	ATF_TP_ADD_TC(tp, tan_zero_pos);
25111be35a1SLionel Sambuc 
25211be35a1SLionel Sambuc 	ATF_TP_ADD_TC(tp, tanf_angles);
25311be35a1SLionel Sambuc 	ATF_TP_ADD_TC(tp, tanf_nan);
25411be35a1SLionel Sambuc 	ATF_TP_ADD_TC(tp, tanf_inf_neg);
25511be35a1SLionel Sambuc 	ATF_TP_ADD_TC(tp, tanf_inf_pos);
25611be35a1SLionel Sambuc 	ATF_TP_ADD_TC(tp, tanf_zero_neg);
25711be35a1SLionel Sambuc 	ATF_TP_ADD_TC(tp, tanf_zero_pos);
25811be35a1SLionel Sambuc 
25911be35a1SLionel Sambuc 	return atf_no_error();
26011be35a1SLionel Sambuc }
261