ReactOS 0.4.17-dev-1321-g5c122a9
tan.c
Go to the documentation of this file.
1/*
2 * PROJECT: ReactOS CRT
3 * LICENSE: MIT (https://spdx.org/licenses/MIT)
4 * PURPOSE: Implementation of tan.
5 * COPYRIGHT: Imported from musl libc
6 * https://git.musl-libc.org/cgit/musl/tree/src/math/tan.c
7 * blob: 9c724a45af51e09f9da9069ba7ab1073e8fee05c
8 * See https://git.musl-libc.org/cgit/musl/tree/COPYRIGHT
9 */
10
11/* origin: FreeBSD /usr/src/lib/msun/src/s_tan.c */
12/*
13 * ====================================================
14 * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
15 *
16 * Developed at SunPro, a Sun Microsystems, Inc. business.
17 * Permission to use, copy, modify, and distribute this
18 * software is freely granted, provided that this notice
19 * is preserved.
20 * ====================================================
21 */
22/* tan(x)
23 * Return tangent function of x.
24 *
25 * kernel function:
26 * __tan ... tangent function on [-pi/4,pi/4]
27 * __rem_pio2 ... argument reduction routine
28 *
29 * Method.
30 * Let S,C and T denote the sin, cos and tan respectively on
31 * [-PI/4, +PI/4]. Reduce the argument x to y1+y2 = x-k*pi/2
32 * in [-pi/4 , +pi/4], and let n = k mod 4.
33 * We have
34 *
35 * n sin(x) cos(x) tan(x)
36 * ----------------------------------------------------------
37 * 0 S C T
38 * 1 C -S -1/T
39 * 2 -S -C T
40 * 3 -C S -1/T
41 * ----------------------------------------------------------
42 *
43 * Special cases:
44 * Let trig be any of sin, cos, or tan.
45 * trig(+-INF) is NaN, with signals;
46 * trig(NaN) is that NaN;
47 *
48 * Accuracy:
49 * TRIG(x) returns trig(x) nearly rounded
50 */
51
52#include "libm.h"
53
54#ifdef _MSC_VER
55#pragma function(tan)
56#endif
57
58double tan(double x)
59{
60 double y[2];
61 uint32_t ix;
62 unsigned n;
63
64 GET_HIGH_WORD(ix, x);
65 ix &= 0x7fffffff;
66
67 /* |x| ~< pi/4 */
68 if (ix <= 0x3fe921fb) {
69 if (ix < 0x3e400000) { /* |x| < 2**-27 */
70 /* raise inexact if x!=0 and underflow if subnormal */
71 FORCE_EVAL(ix < 0x00100000 ? x/0x1p120f : x+0x1p120f);
72 return x;
73 }
74 return __tan(x, 0.0, 0);
75 }
76
77 /* tan(Inf or NaN) is NaN */
78 if (ix >= 0x7ff00000)
79 return x - x;
80
81 /* argument reduction */
82 n = __rem_pio2(x, y);
83 return __tan(y[0], y[1], n&1);
84}
int __rem_pio2(double x, double *y)
Definition: __rem_pio2.c:59
double __tan(double x, double y, int odd)
Definition: __tan.c:76
UINT32 uint32_t
Definition: types.h:75
_ACRTIMP double __cdecl tan(double)
Definition: tan.c:122
GLint GLint GLint GLint GLint x
Definition: gl.h:1548
GLint GLint GLint GLint GLint GLint y
Definition: gl.h:1548
GLdouble n
Definition: glext.h:7729
#define GET_HIGH_WORD(i, d)
Definition: ieee754.h:26
#define FORCE_EVAL(x)
Definition: libm.h:64