The R Project SVN R

Rev

Rev 77685 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 77685 Rev 81865
Line 1... Line 1...
1
/*
1
/*
2
 *  Mathlib : A C Library of Special Functions
2
 *  Mathlib : A C Library of Special Functions
3
 *  Copyright (C) 2013-2016 The R Core Team
3
 *  Copyright (C) 2013-2022 The R Core Team
4
 *
4
 *
5
 *  This program is free software; you can redistribute it and/or modify
5
 *  This program is free software; you can redistribute it and/or modify
6
 *  it under the terms of the GNU General Public License as published by
6
 *  it under the terms of the GNU General Public License as published by
7
 *  the Free Software Foundation; either version 2 of the License, or
7
 *  the Free Software Foundation; either version 2 of the License, or
8
 *  (at your option) any later version.
8
 *  (at your option) any later version.
Line 66... Line 66...
66
    // otherwise
66
    // otherwise
67
    return sin(M_PI * x);
67
    return sin(M_PI * x);
68
}
68
}
69
#endif
69
#endif
70
 
70
 
71
// tan(pi * x)  -- exact when x = k/2  for all integer k
71
// tan(pi * x)  -- exact when x = k/4  for all integer k and half-values give NaN
72
#if defined(HAVE_TANPI) || defined(HAVE___TANPI)
-
 
73
// for use in arithmetic.c, half-values documented to give NaN
72
// ----------- e.g. used in ../main/arithmetic.c : 
74
double Rtanpi(double x)
73
double Rtanpi(double x)
75
#else
-
 
76
double tanpi(double x)
-
 
77
#endif
-
 
78
{
74
{
79
#ifdef IEEE_754
75
#ifdef IEEE_754
80
    if (ISNAN(x)) return x;
76
    if (ISNAN(x)) return x;
81
#endif
77
#endif
82
    if(!R_FINITE(x)) ML_WARN_return_NAN;
78
    if(!R_FINITE(x)) ML_WARN_return_NAN;
83
 
79
 
84
    x = fmod(x, 1.); // tan(pi(x + k)) == tan(pi x)  for all integer k
80
    x = fmod(x, 1.); // tan(pi(x + k)) == tan(pi x)  for all integer k
85
    // map (-1,1) --> (-1/2, 1/2] :
81
    // map (-1,1] --> (-1/2, 1/2] :
86
    if(x <= -0.5) x++; else if(x > 0.5) x--;
82
    if(x <= -0.5) x++; else if(x > 0.5) x--;
87
    return (x == 0.) ? 0. : ((x == 0.5) ? ML_NAN : tan(M_PI * x));
83
    return (x == 0.) ? 0. :
-
 
84
	((x ==  0.5 ) ? ML_NAN :
-
 
85
	((x ==  0.25) ?  1. :
-
 
86
	((x == -0.25) ? -1. :
-
 
87
			tan(M_PI * x)
-
 
88
	    )));
88
}
89
}
89
 
90
 
-
 
91
#if defined(HAVE_TANPI) || defined(HAVE___TANPI)
-
 
92
#else
-
 
93
double tanpi(double x) {
-
 
94
    return Rtanpi(x);
-
 
95
}
-
 
96
#endif
-
 
97
 
90
#if !defined(HAVE_TANPI) && defined(HAVE___TANPI)
98
#if !defined(HAVE_TANPI) && defined(HAVE___TANPI)
91
double tanpi(double x) {
99
double tanpi(double x) {
92
    return __tanpi(x);
100
    return __tanpi(x);
93
}
101
}
-
 
102
/* #else tanpi() defined from C standard math lib */
94
#endif
103
#endif