CXXR (C++ R) API
Arith.h
1 /*CXXR $Id: Arith.h 1348 2013-02-25 17:49:03Z arr $
2  *CXXR
3  *CXXR This file is part of CXXR, a project to refactor the R interpreter
4  *CXXR into C++. It may consist in whole or in part of program code and
5  *CXXR documentation taken from the R project itself, incorporated into
6  *CXXR CXXR (and possibly MODIFIED) under the terms of the GNU General Public
7  *CXXR Licence.
8  *CXXR
9  *CXXR CXXR is Copyright (C) 2008-13 Andrew R. Runnalls, subject to such other
10  *CXXR copyrights and copyright restrictions as may be stated below.
11  *CXXR
12  *CXXR CXXR is not part of the R project, and bugs and other issues should
13  *CXXR not be reported via r-bugs or other R project channels; instead refer
14  *CXXR to the CXXR website.
15  *CXXR */
16 
17 /*
18  * R : A Computer Language for Statistical Data Analysis
19  * Copyright (C) 1995, 1996 Robert Gentleman and Ross Ihaka
20  * Copyright (C) 1998--2007 The R Core Team.
21  *
22  * This program is free software; you can redistribute it and/or modify
23  * it under the terms of the GNU Lesser General Public License as published by
24  * the Free Software Foundation; either version 2.1 of the License, or
25  * (at your option) any later version.
26  *
27  * This program is distributed in the hope that it will be useful,
28  * but WITHOUT ANY WARRANTY; without even the implied warranty of
29  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30  * GNU Lesser General Public License for more details.
31  *
32  * You should have received a copy of the GNU Lesser General Public License
33  * along with this program; if not, a copy is available at
34  * http://www.r-project.org/Licenses/
35  */
36 
37 #ifndef R_ARITH_H_
38 #define R_ARITH_H_
39 
40 /* Only for use where config.h has not already been included */
41 #if defined(HAVE_GLIBC2) && !defined(_BSD_SOURCE)
42 /* ensure that finite and isnan are declared */
43 # define _BSD_SOURCE 1
44 #endif
45 
46 #include <R_ext/Boolean.h>
47 #include <R_ext/libextern.h>
48 
49 #ifdef __cplusplus
50 #include <cmath>
51 
52 extern "C" {
53 #elif !defined(NO_C_HEADERS)
54 /* needed for isnan and isfinite, neither of which are used under C++ */
55 # include <math.h>
56 #endif
57 
58 /* implementation of these : ../../main/arithmetic.c */
59 LibExtern double R_NaN; /* IEEE NaN */
60 LibExtern double R_PosInf; /* IEEE Inf */
61 LibExtern double R_NegInf; /* IEEE -Inf */
62 LibExtern double R_NaReal; /* NA_REAL: IEEE */
63 LibExtern int R_NaInt; /* NA_INTEGER:= INT_MIN currently */
64 #ifdef __MAIN__
65 //#undef extern /* 2007/06/03 arr */
66 #undef LibExtern
67 #endif
68 
69 #define NA_LOGICAL R_NaInt
70 #define NA_INTEGER R_NaInt
71 /* #define NA_FACTOR R_NaInt unused */
72 #define NA_REAL R_NaReal
73 /* NA_STRING is a SEXP, so defined in Rinternals.h */
74 
75 int R_IsNA(double); /* True for R's NA only */
76 Rboolean R_IsNaN(double); /* True for special NaN, *not* for NA */
77 Rboolean R_finite(double); /* True if none of NA, NaN, +/-Inf */
78 #define ISNA(x) R_IsNA(x)
79 
80 /* ISNAN(): True for *both* NA and NaN.
81  NOTE: some systems do not return 1 for TRUE.
82  Also note that C++ math headers specifically undefine
83  isnan if it is a macro (it is on OS X and in C99),
84  hence the workaround. This code also appears in Rmath.h
85 */
86 #ifdef __cplusplus
87  /* CXXR Notes:
88  *
89  * isnan() was introduced by C99, which prescribes that it shall
90  * be a macro. ISO14882, even in its 2003 issue, requires only
91  * that <cmath> offer the facilities of math.h as defined in
92  * ISO/IEC 9899:1990. TR1 proposed that <cmath> make isnan()
93  * available to C++ programs as a templated function within the
94  * std namespace. On Linux, gcc appears to make the ordinary
95  * isnan() macro available to C++ programs. However, on Mac OS X
96  * (at least as of 10.5.6) gcc appears to make isnan() available
97  * to C++ programs only as std::isnan().
98  *
99  * The following covers these two cases. Another possibility that
100  * may need to be addressed is that C++'s isnan() is to be found
101  * in namespace std::tr1.
102  */
103  inline Rboolean R_isnancpp(double x)
104  {
105 #ifdef isnan
106  return Rboolean(isnan(x)!=0);
107 #else
108  return Rboolean(std::isnan(x)!=0);
109 #endif
110  }
111 
112  /* This anticipates C++ 0x. */
113  inline Rboolean R_finite(double x)
114  {
115  return Rboolean(std::isfinite(x) != 0);
116  }
117 
118 # define ISNAN(x) R_isnancpp(x)
119 #else
120 # define ISNAN(x) ((Rboolean)(isnan(x)!=0))
121 #endif
122 
123 /* The following is only defined inside R */
124 #ifdef HAVE_WORKING_ISFINITE
125 /* isfinite is defined in <math.h> according to C99 */
126 # define R_FINITE(x) isfinite(x)
127 #else
128 
129 # define R_FINITE(x) R_finite(x)
130 #endif
131 
132 #ifdef __cplusplus
133 }
134 #endif
135 
136 #endif /* R_ARITH_H_ */