STK++ 0.9.13
STK_Real.h
Go to the documentation of this file.
1/*--------------------------------------------------------------------*/
2/* Copyright (C) 2004-2016 Serge Iovleff, Université Lille 1, Inria
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU Lesser General Public License as
6 published by the Free Software Foundation; either version 2 of the
7 License, or (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU Lesser General Public License for more details.
13
14 You should have received a copy of the GNU Lesser General Public
15 License along with this program; if not, write to the
16 Free Software Foundation, Inc.,
17 59 Temple Place,
18 Suite 330,
19 Boston, MA 02111-1307
20 USA
21
22 Contact : S..._Dot_I..._At_stkpp_Dot_org (see copyright for ...)
23*/
24
25/*
26 * Project: Base
27 * Purpose: Define the fundamental type Real.
28 * Author: Serge Iovleff, S..._Dot_I..._At_stkpp_Dot_org (see copyright for ...)
29 *
30 **/
31
36#ifndef STK_REAL_H
37#define STK_REAL_H
38
39#include <map>
40
41#ifdef IS_RTKPP_LIB
42#include "Rtkpp/include/STK_RcppTraits.h"
43#endif
44
45namespace STK
46{
47// forward declarations
48template<typename Type> struct Arithmetic;
49template<typename Type> struct IdTypeImpl;
50
51
58template<>
59struct Arithmetic<Real> : public std::numeric_limits<Real>
60{
61#ifdef IS_RTKPP_LIB
62 enum
63 {
64 Rtype_ = hidden::RcppTraits<Real>::Rtype_
65 };
66#endif
68 static const bool hasNA = true;
70 static inline Real NA() throw() { return std::numeric_limits<Real>::quiet_NaN();}
72 static const bool has_quiet_NaN = false;
76 static inline bool isNA(Real const& x) throw() { return !(x==x);}
80 static inline bool isInfinite(Real const& x) throw()
81 { return ( (x < -std::numeric_limits<Real>::max())||(x > std::numeric_limits<Real>::max()));}
85 static inline bool isFinite(Real const& x) throw() { return (!isNA(x) && !isInfinite(x));}
86};
87
93template<>
95{
97 static Base::IdType returnType() { return(Base::real_);}
98};
99
100#ifdef IS_RTKPP_LIB
107template<>
108struct Arithmetic<const Real>
109{
110 enum
111 {
112 Rtype_ = hidden::RcppTraits<Real>::Rtype_
113 };
115 static const bool hasNA = true;
117 static inline Real NA() throw() { return std::numeric_limits<Real>::quiet_NaN();}
119 static const bool has_quiet_NaN = false;
123 static inline bool isNA(Real const& x) throw() { return !(x==x);}
127 static inline bool isInfinite(Real const& x) throw()
128 { return ( (x < -std::numeric_limits<Real>::max())||(x > std::numeric_limits<Real>::max()));}
132 static inline bool isFinite(Real const& x) throw() { return (!isNA(x) && !isInfinite(x));}
133};
134
140template<>
141struct IdTypeImpl<const Real>
142{
144 static Base::IdType returnType() { return(Base::real_);}
145};
146
147#endif // IS_RTKPP_LIB
148
155{
157 Real x;
158 is >> Proxy<Real>(x);
159 return x;
160}
161
169inline Real stringToReal( String const& str, std::map<String, Real> const& mapping)
170{
171 std::map<String, Real>::const_iterator it=mapping.find(str);
172 return (it == mapping.end()) ? Arithmetic<Real>::NA() : it->second;
173}
174
181inline String realToString( Real const& value, std::ios_base& (*f)(std::ios_base&) = std::dec)
182{
183 if (isNA(value)) return stringNa;
185 os << f << value;
186 return os.str();
187}
188
195inline String realToString( Real const& type, std::map<Real, String> const& mapping)
196{
197 std::map<Real, String>::const_iterator it=mapping.find(type);
198 if (it == mapping.end()) return stringNa;
199 return it->second;
200}
201
207template<>
209{ return stringToReal(str);}
210
216template<>
217inline String typeToString<Real>( Real const& value, std::ios_base& (*f)(std::ios_base&))
218{ return realToString(value, f);}
219
220} // namespace STK
221
222#endif /*STK_REAL_H*/
The MultidimRegression class allows to regress a multidimensional output variable among a multivariat...
bool isNA(Type const &x)
utility method allowing to know if a value is a NA (Not Available) value
String typeToString< Real >(Real const &value, std::ios_base &(*f)(std::ios_base &))
specialization for Real
Definition STK_Real.h:217
String realToString(Real const &value, std::ios_base &(*f)(std::ios_base &)=std::dec)
Convert a Real to a String.
Definition STK_Real.h:181
Real stringToReal(String const &str)
Convert a String to a Real.
Definition STK_Real.h:154
String stringNa
Representation of a Not Available value.
Real stringToType< Real >(String const &str)
specialization for Real
Definition STK_Real.h:208
IdType
Id for the Type of a variable.
@ real_
Quantitative type.
std::basic_string< Char > String
STK fundamental type of a String.
double Real
STK fundamental type of Real values.
std::basic_ostringstream< Char > ostringstream
ostringstream (ostringstream) for Char
Definition STK_Stream.h:65
std::basic_istringstream< Char > istringstream
istringstream for Char
Definition STK_Stream.h:63
The namespace STK is the main domain space of the Statistical ToolKit project.
static bool isNA(Real const &x)
Test if x is a Non Available (NA) special number.
Definition STK_Real.h:76
static bool isInfinite(Real const &x)
Definition STK_Real.h:80
static bool isFinite(Real const &x)
Definition STK_Real.h:85
static Real NA()
Definition STK_Real.h:70
Arithmetic properties of STK fundamental types.
static bool isFinite(Type const &x)
static bool isInfinite(Type const &x)
static const bool hasNA
True if the type has a representation for a "Not Available."
static Type NA()
Adding a Non Available (NA) special number.
static bool isNA(Type const &x)
static Base::IdType returnType()
Give the IdType of the type Real.
Definition STK_Real.h:97
Implementation of the return type.
static Base::IdType returnType()