xref: /freebsd/contrib/netbsd-tests/lib/libm/t_acos.c (revision 1ec3feb64826d2a43d41e74684690985bf20e71c)
1*1ec3feb6SAlex Richardson /* $NetBSD: t_acos.c,v 1.11 2018/11/07 03:59:36 riastradh Exp $ */
257718be8SEnji Cooper 
357718be8SEnji Cooper /*-
457718be8SEnji Cooper  * Copyright (c) 2011 The NetBSD Foundation, Inc.
557718be8SEnji Cooper  * All rights reserved.
657718be8SEnji Cooper  *
757718be8SEnji Cooper  * This code is derived from software contributed to The NetBSD Foundation
857718be8SEnji Cooper  * by Jukka Ruohonen.
957718be8SEnji Cooper  *
1057718be8SEnji Cooper  * Redistribution and use in source and binary forms, with or without
1157718be8SEnji Cooper  * modification, are permitted provided that the following conditions
1257718be8SEnji Cooper  * are met:
1357718be8SEnji Cooper  * 1. Redistributions of source code must retain the above copyright
1457718be8SEnji Cooper  *    notice, this list of conditions and the following disclaimer.
1557718be8SEnji Cooper  * 2. Redistributions in binary form must reproduce the above copyright
1657718be8SEnji Cooper  *    notice, this list of conditions and the following disclaimer in the
1757718be8SEnji Cooper  *    documentation and/or other materials provided with the distribution.
1857718be8SEnji Cooper  *
1957718be8SEnji Cooper  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
2057718be8SEnji Cooper  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
2157718be8SEnji Cooper  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
2257718be8SEnji Cooper  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
2357718be8SEnji Cooper  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2457718be8SEnji Cooper  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2557718be8SEnji Cooper  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2657718be8SEnji Cooper  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
2757718be8SEnji Cooper  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
2857718be8SEnji Cooper  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
2957718be8SEnji Cooper  * POSSIBILITY OF SUCH DAMAGE.
3057718be8SEnji Cooper  */
3157718be8SEnji Cooper 
3257718be8SEnji Cooper #include <atf-c.h>
3357718be8SEnji Cooper #include <math.h>
3457718be8SEnji Cooper #include "t_libm.h"
3557718be8SEnji Cooper 
3657718be8SEnji Cooper /*
3757718be8SEnji Cooper  * acos(3) and acosf(3)
3857718be8SEnji Cooper  */
3957718be8SEnji Cooper 
4057718be8SEnji Cooper ATF_LIBM_TEST(acos_is_nan, "Test acos/acosf(x) == NaN, x = NaN, +/-Inf, ![-1..1]")
4157718be8SEnji Cooper {
4257718be8SEnji Cooper 	static const double x[] = {
4357718be8SEnji Cooper 	    -1.000000001, 1.000000001,
4457718be8SEnji Cooper 	    -1.0000001, 1.0000001,
4557718be8SEnji Cooper 	    -1.1, 1.1,
4657718be8SEnji Cooper #ifndef __vax__
4757718be8SEnji Cooper 	    T_LIBM_NAN,
4857718be8SEnji Cooper 	    T_LIBM_MINUS_INF, T_LIBM_PLUS_INF,
4957718be8SEnji Cooper #endif
5057718be8SEnji Cooper 	};
5157718be8SEnji Cooper 	unsigned int i;
5257718be8SEnji Cooper 
5357718be8SEnji Cooper 	for (i = 0; i < __arraycount(x); i++) {
5457718be8SEnji Cooper 		T_LIBM_CHECK_NAN(i, acos, x[i]);
5557718be8SEnji Cooper 		if (i < 2)
5657718be8SEnji Cooper 			/* Values are too small for float */
5757718be8SEnji Cooper 			continue;
5857718be8SEnji Cooper 		T_LIBM_CHECK_NAN(i, acosf, x[i]);
5957718be8SEnji Cooper 	}
6057718be8SEnji Cooper }
6157718be8SEnji Cooper 
6257718be8SEnji Cooper ATF_LIBM_TEST(acos_inrange, "Test acos/acosf(x) for some valid values")
6357718be8SEnji Cooper {
6457718be8SEnji Cooper 	static const struct {
6557718be8SEnji Cooper 		double x;
6657718be8SEnji Cooper 		double y;
6757718be8SEnji Cooper 	} values[] = {
6857718be8SEnji Cooper 		{ -1,    M_PI,              },
6957718be8SEnji Cooper 		{ -0.99, 3.000053180265366, },
7057718be8SEnji Cooper 		{ -0.5,  2.094395102393195, },
7157718be8SEnji Cooper 		{ -0.1,  1.670963747956456, },
7257718be8SEnji Cooper 		{  0,    M_PI / 2,          },
7357718be8SEnji Cooper 		{  0.1,  1.470628905633337, },
7457718be8SEnji Cooper 		{  0.5,  1.047197551196598, },
75*1ec3feb6SAlex Richardson 		{  0.99, 0.1415394733244273, },
7657718be8SEnji Cooper 	};
7757718be8SEnji Cooper 	unsigned int i;
7857718be8SEnji Cooper 
7957718be8SEnji Cooper 	/*
8057718be8SEnji Cooper 	 * Note that acos(x) might be calculated as atan2(sqrt(1-x*x),x).
8157718be8SEnji Cooper 	 * This means that acos(-1) is atan2(+0,-1), if the sign is wrong
8257718be8SEnji Cooper 	 * the value will be -M_PI (atan2(-0,-1)) not M_PI.
8357718be8SEnji Cooper 	 */
8457718be8SEnji Cooper 
8557718be8SEnji Cooper 	for (i = 0; i < __arraycount(values); i++) {
8657718be8SEnji Cooper 		T_LIBM_CHECK(i, acos, values[i].x, values[i].y, 1.0e-15);
8757718be8SEnji Cooper 		T_LIBM_CHECK(i, acosf, values[i].x, values[i].y, 1.0e-5);
8857718be8SEnji Cooper 	}
8957718be8SEnji Cooper }
9057718be8SEnji Cooper 
9157718be8SEnji Cooper ATF_LIBM_TEST(acos_is_plus_zero, "Test acosf(1.0) == +0.0")
9257718be8SEnji Cooper {
9357718be8SEnji Cooper 	T_LIBM_CHECK_PLUS_ZERO(0, acos, 1.0);
9457718be8SEnji Cooper 	T_LIBM_CHECK_PLUS_ZERO(0, acosf, 1.0);
9557718be8SEnji Cooper }
9657718be8SEnji Cooper 
ATF_TP_ADD_TCS(tp)9757718be8SEnji Cooper ATF_TP_ADD_TCS(tp)
9857718be8SEnji Cooper {
9957718be8SEnji Cooper 
10057718be8SEnji Cooper 	ATF_TP_ADD_TC(tp, acos_inrange);
10157718be8SEnji Cooper 	ATF_TP_ADD_TC(tp, acos_is_nan);
10257718be8SEnji Cooper 	ATF_TP_ADD_TC(tp, acos_is_plus_zero);
10357718be8SEnji Cooper 
10457718be8SEnji Cooper 	return atf_no_error();
10557718be8SEnji Cooper }
106