xref: /minix3/tests/lib/libm/t_cos.c (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1*0a6a1f1dSLionel Sambuc /* $NetBSD: t_cos.c,v 1.4 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, -1.0000000000000000 },
4111be35a1SLionel Sambuc 	{ -135, -2.356194490192345, -0.7071067811865476 },
4211be35a1SLionel Sambuc 	{  -90, -1.570796326794897,  0.0000000000000000 },
4311be35a1SLionel Sambuc 	{  -45, -0.785398163397448,  0.7071067811865476 },
4411be35a1SLionel Sambuc 	{    0,  0.000000000000000,  1.0000000000000000 },
4511be35a1SLionel Sambuc 	{   30,  0.523598775598299,  0.8660254037844386 },
4611be35a1SLionel Sambuc 	{   45,  0.785398163397448,  0.7071067811865476 },
4711be35a1SLionel Sambuc 	{   60,  1.047197551196598,  0.5000000000000000 },
4811be35a1SLionel Sambuc 	{   90,  1.570796326794897,  0.0000000000000000 },
4911be35a1SLionel Sambuc 	{  120,  2.094395102393195, -0.5000000000000000 },
5011be35a1SLionel Sambuc 	{  135,  2.356194490192345, -0.7071067811865476 },
5111be35a1SLionel Sambuc 	{  150,  2.617993877991494, -0.8660254037844386 },
5211be35a1SLionel Sambuc 	{  180,  3.141592653589793, -1.0000000000000000 },
5311be35a1SLionel Sambuc 	{  270,  4.712388980384690,  0.0000000000000000 },
5411be35a1SLionel Sambuc 	{  360,  6.283185307179586,  1.0000000000000000 }
5511be35a1SLionel Sambuc };
5611be35a1SLionel Sambuc 
5711be35a1SLionel Sambuc /*
5811be35a1SLionel Sambuc  * cos(3)
5911be35a1SLionel Sambuc  */
6011be35a1SLionel Sambuc ATF_TC(cos_angles);
ATF_TC_HEAD(cos_angles,tc)6111be35a1SLionel Sambuc ATF_TC_HEAD(cos_angles, tc)
6211be35a1SLionel Sambuc {
6311be35a1SLionel Sambuc 	atf_tc_set_md_var(tc, "descr", "Test some selected angles");
6411be35a1SLionel Sambuc }
6511be35a1SLionel Sambuc 
ATF_TC_BODY(cos_angles,tc)6611be35a1SLionel Sambuc ATF_TC_BODY(cos_angles, tc)
6711be35a1SLionel Sambuc {
6811be35a1SLionel Sambuc 	const double eps = 1.0e-15;
6911be35a1SLionel Sambuc 	size_t i;
7011be35a1SLionel Sambuc 
7111be35a1SLionel Sambuc 	for (i = 0; i < __arraycount(angles); i++) {
7211be35a1SLionel Sambuc 
7311be35a1SLionel Sambuc 		if (fabs(cos(angles[i].x) - angles[i].y) > eps)
7411be35a1SLionel Sambuc 			atf_tc_fail_nonfatal("cos(%d deg) != %0.01f",
7511be35a1SLionel Sambuc 			    angles[i].angle, angles[i].y);
7611be35a1SLionel Sambuc 	}
7711be35a1SLionel Sambuc }
7811be35a1SLionel Sambuc 
7911be35a1SLionel Sambuc ATF_TC(cos_nan);
ATF_TC_HEAD(cos_nan,tc)8011be35a1SLionel Sambuc ATF_TC_HEAD(cos_nan, tc)
8111be35a1SLionel Sambuc {
8211be35a1SLionel Sambuc 	atf_tc_set_md_var(tc, "descr", "Test cos(NaN) == NaN");
8311be35a1SLionel Sambuc }
8411be35a1SLionel Sambuc 
ATF_TC_BODY(cos_nan,tc)8511be35a1SLionel Sambuc ATF_TC_BODY(cos_nan, tc)
8611be35a1SLionel Sambuc {
8711be35a1SLionel Sambuc 	const double x = 0.0L / 0.0L;
8811be35a1SLionel Sambuc 
8911be35a1SLionel Sambuc 	ATF_CHECK(isnan(x) != 0);
9011be35a1SLionel Sambuc 	ATF_CHECK(isnan(cos(x)) != 0);
9111be35a1SLionel Sambuc }
9211be35a1SLionel Sambuc 
9311be35a1SLionel Sambuc ATF_TC(cos_inf_neg);
ATF_TC_HEAD(cos_inf_neg,tc)9411be35a1SLionel Sambuc ATF_TC_HEAD(cos_inf_neg, tc)
9511be35a1SLionel Sambuc {
9611be35a1SLionel Sambuc 	atf_tc_set_md_var(tc, "descr", "Test cos(-Inf) == NaN");
9711be35a1SLionel Sambuc }
9811be35a1SLionel Sambuc 
ATF_TC_BODY(cos_inf_neg,tc)9911be35a1SLionel Sambuc ATF_TC_BODY(cos_inf_neg, tc)
10011be35a1SLionel Sambuc {
10111be35a1SLionel Sambuc 	const double x = -1.0L / 0.0L;
10211be35a1SLionel Sambuc 
10311be35a1SLionel Sambuc 	ATF_CHECK(isnan(cos(x)) != 0);
10411be35a1SLionel Sambuc }
10511be35a1SLionel Sambuc 
10611be35a1SLionel Sambuc ATF_TC(cos_inf_pos);
ATF_TC_HEAD(cos_inf_pos,tc)10711be35a1SLionel Sambuc ATF_TC_HEAD(cos_inf_pos, tc)
10811be35a1SLionel Sambuc {
10911be35a1SLionel Sambuc 	atf_tc_set_md_var(tc, "descr", "Test cos(+Inf) == NaN");
11011be35a1SLionel Sambuc }
11111be35a1SLionel Sambuc 
ATF_TC_BODY(cos_inf_pos,tc)11211be35a1SLionel Sambuc ATF_TC_BODY(cos_inf_pos, tc)
11311be35a1SLionel Sambuc {
11411be35a1SLionel Sambuc 	const double x = 1.0L / 0.0L;
11511be35a1SLionel Sambuc 
11611be35a1SLionel Sambuc 	ATF_CHECK(isnan(cos(x)) != 0);
11711be35a1SLionel Sambuc }
11811be35a1SLionel Sambuc 
11911be35a1SLionel Sambuc 
12011be35a1SLionel Sambuc ATF_TC(cos_zero_neg);
ATF_TC_HEAD(cos_zero_neg,tc)12111be35a1SLionel Sambuc ATF_TC_HEAD(cos_zero_neg, tc)
12211be35a1SLionel Sambuc {
12311be35a1SLionel Sambuc 	atf_tc_set_md_var(tc, "descr", "Test cos(-0.0) == 1.0");
12411be35a1SLionel Sambuc }
12511be35a1SLionel Sambuc 
ATF_TC_BODY(cos_zero_neg,tc)12611be35a1SLionel Sambuc ATF_TC_BODY(cos_zero_neg, tc)
12711be35a1SLionel Sambuc {
12811be35a1SLionel Sambuc 	const double x = -0.0L;
12911be35a1SLionel Sambuc 
13011be35a1SLionel Sambuc 	ATF_CHECK(cos(x) == 1.0);
13111be35a1SLionel Sambuc }
13211be35a1SLionel Sambuc 
13311be35a1SLionel Sambuc ATF_TC(cos_zero_pos);
ATF_TC_HEAD(cos_zero_pos,tc)13411be35a1SLionel Sambuc ATF_TC_HEAD(cos_zero_pos, tc)
13511be35a1SLionel Sambuc {
13611be35a1SLionel Sambuc 	atf_tc_set_md_var(tc, "descr", "Test cos(+0.0) == 1.0");
13711be35a1SLionel Sambuc }
13811be35a1SLionel Sambuc 
ATF_TC_BODY(cos_zero_pos,tc)13911be35a1SLionel Sambuc ATF_TC_BODY(cos_zero_pos, tc)
14011be35a1SLionel Sambuc {
14111be35a1SLionel Sambuc 	const double x = 0.0L;
14211be35a1SLionel Sambuc 
14311be35a1SLionel Sambuc 	ATF_CHECK(cos(x) == 1.0);
14411be35a1SLionel Sambuc }
14511be35a1SLionel Sambuc 
14611be35a1SLionel Sambuc /*
14711be35a1SLionel Sambuc  * cosf(3)
14811be35a1SLionel Sambuc  */
14911be35a1SLionel Sambuc ATF_TC(cosf_angles);
ATF_TC_HEAD(cosf_angles,tc)15011be35a1SLionel Sambuc ATF_TC_HEAD(cosf_angles, tc)
15111be35a1SLionel Sambuc {
15211be35a1SLionel Sambuc 	atf_tc_set_md_var(tc, "descr", "Test some selected angles");
15311be35a1SLionel Sambuc }
15411be35a1SLionel Sambuc 
ATF_TC_BODY(cosf_angles,tc)15511be35a1SLionel Sambuc ATF_TC_BODY(cosf_angles, tc)
15611be35a1SLionel Sambuc {
15711be35a1SLionel Sambuc 	const float eps = 1.0e-7;
15811be35a1SLionel Sambuc 	float x, y;
15911be35a1SLionel Sambuc 	size_t i;
16011be35a1SLionel Sambuc 
16111be35a1SLionel Sambuc 	for (i = 0; i < __arraycount(angles); i++) {
16211be35a1SLionel Sambuc 
16311be35a1SLionel Sambuc 		x = angles[i].x;
16411be35a1SLionel Sambuc 		y = angles[i].y;
16511be35a1SLionel Sambuc 
16611be35a1SLionel Sambuc 		if (fabsf(cosf(x) - y) > eps)
16711be35a1SLionel Sambuc 			atf_tc_fail_nonfatal("cosf(%d deg) != %0.01f",
16811be35a1SLionel Sambuc 			    angles[i].angle, angles[i].y);
16911be35a1SLionel Sambuc 	}
17011be35a1SLionel Sambuc }
17111be35a1SLionel Sambuc 
17211be35a1SLionel Sambuc ATF_TC(cosf_nan);
ATF_TC_HEAD(cosf_nan,tc)17311be35a1SLionel Sambuc ATF_TC_HEAD(cosf_nan, tc)
17411be35a1SLionel Sambuc {
17511be35a1SLionel Sambuc 	atf_tc_set_md_var(tc, "descr", "Test cosf(NaN) == NaN");
17611be35a1SLionel Sambuc }
17711be35a1SLionel Sambuc 
ATF_TC_BODY(cosf_nan,tc)17811be35a1SLionel Sambuc ATF_TC_BODY(cosf_nan, tc)
17911be35a1SLionel Sambuc {
18011be35a1SLionel Sambuc 	const float x = 0.0L / 0.0L;
18111be35a1SLionel Sambuc 
18211be35a1SLionel Sambuc 	ATF_CHECK(isnan(x) != 0);
18311be35a1SLionel Sambuc 	ATF_CHECK(isnan(cosf(x)) != 0);
18411be35a1SLionel Sambuc }
18511be35a1SLionel Sambuc 
18611be35a1SLionel Sambuc ATF_TC(cosf_inf_neg);
ATF_TC_HEAD(cosf_inf_neg,tc)18711be35a1SLionel Sambuc ATF_TC_HEAD(cosf_inf_neg, tc)
18811be35a1SLionel Sambuc {
18911be35a1SLionel Sambuc 	atf_tc_set_md_var(tc, "descr", "Test cosf(-Inf) == NaN");
19011be35a1SLionel Sambuc }
19111be35a1SLionel Sambuc 
ATF_TC_BODY(cosf_inf_neg,tc)19211be35a1SLionel Sambuc ATF_TC_BODY(cosf_inf_neg, tc)
19311be35a1SLionel Sambuc {
19411be35a1SLionel Sambuc 	const float x = -1.0L / 0.0L;
19511be35a1SLionel Sambuc 
19611be35a1SLionel Sambuc 	if (isnan(cosf(x)) == 0) {
19711be35a1SLionel Sambuc 		atf_tc_expect_fail("PR lib/45362");
19811be35a1SLionel Sambuc 		atf_tc_fail("cosf(-Inf) != NaN");
19911be35a1SLionel Sambuc 	}
20011be35a1SLionel Sambuc }
20111be35a1SLionel Sambuc 
20211be35a1SLionel Sambuc ATF_TC(cosf_inf_pos);
ATF_TC_HEAD(cosf_inf_pos,tc)20311be35a1SLionel Sambuc ATF_TC_HEAD(cosf_inf_pos, tc)
20411be35a1SLionel Sambuc {
20511be35a1SLionel Sambuc 	atf_tc_set_md_var(tc, "descr", "Test cosf(+Inf) == NaN");
20611be35a1SLionel Sambuc }
20711be35a1SLionel Sambuc 
ATF_TC_BODY(cosf_inf_pos,tc)20811be35a1SLionel Sambuc ATF_TC_BODY(cosf_inf_pos, tc)
20911be35a1SLionel Sambuc {
21011be35a1SLionel Sambuc 	const float x = 1.0L / 0.0L;
21111be35a1SLionel Sambuc 
21211be35a1SLionel Sambuc 	if (isnan(cosf(x)) == 0) {
21311be35a1SLionel Sambuc 		atf_tc_expect_fail("PR lib/45362");
21411be35a1SLionel Sambuc 		atf_tc_fail("cosf(+Inf) != NaN");
21511be35a1SLionel Sambuc 	}
21611be35a1SLionel Sambuc }
21711be35a1SLionel Sambuc 
21811be35a1SLionel Sambuc 
21911be35a1SLionel Sambuc ATF_TC(cosf_zero_neg);
ATF_TC_HEAD(cosf_zero_neg,tc)22011be35a1SLionel Sambuc ATF_TC_HEAD(cosf_zero_neg, tc)
22111be35a1SLionel Sambuc {
22211be35a1SLionel Sambuc 	atf_tc_set_md_var(tc, "descr", "Test cosf(-0.0) == 1.0");
22311be35a1SLionel Sambuc }
22411be35a1SLionel Sambuc 
ATF_TC_BODY(cosf_zero_neg,tc)22511be35a1SLionel Sambuc ATF_TC_BODY(cosf_zero_neg, tc)
22611be35a1SLionel Sambuc {
22711be35a1SLionel Sambuc 	const float x = -0.0L;
22811be35a1SLionel Sambuc 
22911be35a1SLionel Sambuc 	ATF_CHECK(cosf(x) == 1.0);
23011be35a1SLionel Sambuc }
23111be35a1SLionel Sambuc 
23211be35a1SLionel Sambuc ATF_TC(cosf_zero_pos);
ATF_TC_HEAD(cosf_zero_pos,tc)23311be35a1SLionel Sambuc ATF_TC_HEAD(cosf_zero_pos, tc)
23411be35a1SLionel Sambuc {
23511be35a1SLionel Sambuc 	atf_tc_set_md_var(tc, "descr", "Test cosf(+0.0) == 1.0");
23611be35a1SLionel Sambuc }
23711be35a1SLionel Sambuc 
ATF_TC_BODY(cosf_zero_pos,tc)23811be35a1SLionel Sambuc ATF_TC_BODY(cosf_zero_pos, tc)
23911be35a1SLionel Sambuc {
24011be35a1SLionel Sambuc 	const float x = 0.0L;
24111be35a1SLionel Sambuc 
24211be35a1SLionel Sambuc 	ATF_CHECK(cosf(x) == 1.0);
24311be35a1SLionel Sambuc }
24411be35a1SLionel Sambuc 
ATF_TP_ADD_TCS(tp)24511be35a1SLionel Sambuc ATF_TP_ADD_TCS(tp)
24611be35a1SLionel Sambuc {
24711be35a1SLionel Sambuc 
24811be35a1SLionel Sambuc 	ATF_TP_ADD_TC(tp, cos_angles);
24911be35a1SLionel Sambuc 	ATF_TP_ADD_TC(tp, cos_nan);
25011be35a1SLionel Sambuc 	ATF_TP_ADD_TC(tp, cos_inf_neg);
25111be35a1SLionel Sambuc 	ATF_TP_ADD_TC(tp, cos_inf_pos);
25211be35a1SLionel Sambuc 	ATF_TP_ADD_TC(tp, cos_zero_neg);
25311be35a1SLionel Sambuc 	ATF_TP_ADD_TC(tp, cos_zero_pos);
25411be35a1SLionel Sambuc 
25511be35a1SLionel Sambuc 	ATF_TP_ADD_TC(tp, cosf_angles);
25611be35a1SLionel Sambuc 	ATF_TP_ADD_TC(tp, cosf_nan);
25711be35a1SLionel Sambuc 	ATF_TP_ADD_TC(tp, cosf_inf_neg);
25811be35a1SLionel Sambuc 	ATF_TP_ADD_TC(tp, cosf_inf_pos);
25911be35a1SLionel Sambuc 	ATF_TP_ADD_TC(tp, cosf_zero_neg);
26011be35a1SLionel Sambuc 	ATF_TP_ADD_TC(tp, cosf_zero_pos);
26111be35a1SLionel Sambuc 
26211be35a1SLionel Sambuc 	return atf_no_error();
26311be35a1SLionel Sambuc }
264