1*0a6a1f1dSLionel Sambuc /* $NetBSD: t_acos.c,v 1.10 2014/03/05 20:14:46 dsl 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>
34*0a6a1f1dSLionel Sambuc #include "t_libm.h"
3511be35a1SLionel Sambuc
36*0a6a1f1dSLionel Sambuc /*
37*0a6a1f1dSLionel Sambuc * acos(3) and acosf(3)
38*0a6a1f1dSLionel Sambuc */
39*0a6a1f1dSLionel Sambuc
40*0a6a1f1dSLionel Sambuc ATF_LIBM_TEST(acos_is_nan, "Test acos/acosf(x) == NaN, x = NaN, +/-Inf, ![-1..1]")
41*0a6a1f1dSLionel Sambuc {
42*0a6a1f1dSLionel Sambuc static const double x[] = {
43*0a6a1f1dSLionel Sambuc -1.000000001, 1.000000001,
44*0a6a1f1dSLionel Sambuc -1.0000001, 1.0000001,
45*0a6a1f1dSLionel Sambuc -1.1, 1.1,
46*0a6a1f1dSLionel Sambuc #ifndef __vax__
47*0a6a1f1dSLionel Sambuc T_LIBM_NAN,
48*0a6a1f1dSLionel Sambuc T_LIBM_MINUS_INF, T_LIBM_PLUS_INF,
49*0a6a1f1dSLionel Sambuc #endif
50*0a6a1f1dSLionel Sambuc };
51*0a6a1f1dSLionel Sambuc unsigned int i;
52*0a6a1f1dSLionel Sambuc
53*0a6a1f1dSLionel Sambuc for (i = 0; i < __arraycount(x); i++) {
54*0a6a1f1dSLionel Sambuc T_LIBM_CHECK_NAN(i, acos, x[i]);
55*0a6a1f1dSLionel Sambuc if (i < 2)
56*0a6a1f1dSLionel Sambuc /* Values are too small for float */
57*0a6a1f1dSLionel Sambuc continue;
58*0a6a1f1dSLionel Sambuc T_LIBM_CHECK_NAN(i, acosf, x[i]);
59*0a6a1f1dSLionel Sambuc }
60*0a6a1f1dSLionel Sambuc }
61*0a6a1f1dSLionel Sambuc
62*0a6a1f1dSLionel Sambuc ATF_LIBM_TEST(acos_inrange, "Test acos/acosf(x) for some valid values")
63*0a6a1f1dSLionel Sambuc {
6411be35a1SLionel Sambuc static const struct {
6511be35a1SLionel Sambuc double x;
6611be35a1SLionel Sambuc double y;
6711be35a1SLionel Sambuc } values[] = {
6811be35a1SLionel Sambuc { -1, M_PI, },
6911be35a1SLionel Sambuc { -0.99, 3.000053180265366, },
7011be35a1SLionel Sambuc { -0.5, 2.094395102393195, },
7111be35a1SLionel Sambuc { -0.1, 1.670963747956456, },
7211be35a1SLionel Sambuc { 0, M_PI / 2, },
7311be35a1SLionel Sambuc { 0.1, 1.470628905633337, },
7411be35a1SLionel Sambuc { 0.5, 1.047197551196598, },
7511be35a1SLionel Sambuc { 0.99, 0.141539473324427, },
7611be35a1SLionel Sambuc };
77*0a6a1f1dSLionel Sambuc unsigned int i;
7811be35a1SLionel Sambuc
7911be35a1SLionel Sambuc /*
80*0a6a1f1dSLionel Sambuc * Note that acos(x) might be calculated as atan2(sqrt(1-x*x),x).
81*0a6a1f1dSLionel Sambuc * This means that acos(-1) is atan2(+0,-1), if the sign is wrong
82*0a6a1f1dSLionel Sambuc * the value will be -M_PI (atan2(-0,-1)) not M_PI.
8311be35a1SLionel Sambuc */
8411be35a1SLionel Sambuc
8511be35a1SLionel Sambuc for (i = 0; i < __arraycount(values); i++) {
86*0a6a1f1dSLionel Sambuc T_LIBM_CHECK(i, acos, values[i].x, values[i].y, 1.0e-15);
87*0a6a1f1dSLionel Sambuc T_LIBM_CHECK(i, acosf, values[i].x, values[i].y, 1.0e-5);
8811be35a1SLionel Sambuc }
8911be35a1SLionel Sambuc }
9011be35a1SLionel Sambuc
91*0a6a1f1dSLionel Sambuc ATF_LIBM_TEST(acos_is_plus_zero, "Test acosf(1.0) == +0.0")
9211be35a1SLionel Sambuc {
93*0a6a1f1dSLionel Sambuc T_LIBM_CHECK_PLUS_ZERO(0, acos, 1.0);
94*0a6a1f1dSLionel Sambuc T_LIBM_CHECK_PLUS_ZERO(0, acosf, 1.0);
9511be35a1SLionel Sambuc }
9611be35a1SLionel Sambuc
ATF_TP_ADD_TCS(tp)9711be35a1SLionel Sambuc ATF_TP_ADD_TCS(tp)
9811be35a1SLionel Sambuc {
9911be35a1SLionel Sambuc
10011be35a1SLionel Sambuc ATF_TP_ADD_TC(tp, acos_inrange);
101*0a6a1f1dSLionel Sambuc ATF_TP_ADD_TC(tp, acos_is_nan);
102*0a6a1f1dSLionel Sambuc ATF_TP_ADD_TC(tp, acos_is_plus_zero);
10311be35a1SLionel Sambuc
10411be35a1SLionel Sambuc return atf_no_error();
10511be35a1SLionel Sambuc }
106