Claw  1.7.3
real_number.tpp
Go to the documentation of this file.
1 /*
2  CLAW - a C++ Library Absolutely Wonderful
3 
4  CLAW is a free library without any particular aim but being useful to
5  anyone.
6 
7  Copyright (C) 2005-2011 Julien Jorge
8 
9  This library is free software; you can redistribute it and/or
10  modify it under the terms of the GNU Lesser General Public
11  License as published by the Free Software Foundation; either
12  version 2.1 of the License, or (at your option) any later version.
13 
14  This library is distributed in the hope that it will be useful,
15  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  Lesser General Public License for more details.
18 
19  You should have received a copy of the GNU Lesser General Public
20  License along with this library; if not, write to the Free Software
21  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 
23  contact: julien.jorge@gamned.org
24 */
30 #include <cmath>
31 
32 /*----------------------------------------------------------------------------*/
36 template<typename T>
38  : m_value(0), m_epsilon( make_epsilon<value_type>::value(m_value) )
39 {
40 
41 } // real_number::real_number()
42 
43 /*----------------------------------------------------------------------------*/
48 template<typename T>
49 claw::real_number<T>::real_number( const value_type& v )
50  : m_value(v), m_epsilon( make_epsilon<T>::value(m_value) )
51 {
52 
53 } // real_number::real_number()
54 
55 /*----------------------------------------------------------------------------*/
60 template<typename T>
62  : m_value(that.m_value), m_epsilon(that.m_epsilon)
63 {
64 
65 } // real_number::real_number()
66 
67 /*----------------------------------------------------------------------------*/
71 template<typename T>
73 {
74  return self_type( std::abs(m_value) );
75 } // real_number::abs()
76 
77 /*----------------------------------------------------------------------------*/
82 template<typename T>
83 bool claw::real_number<T>::operator<( const self_type& that ) const
84 {
85  if ( that.m_value == std::numeric_limits<value_type>::infinity() )
86  return m_value != std::numeric_limits<value_type>::infinity();
87  else if ( that.m_value == -std::numeric_limits<value_type>::infinity() )
88  return false;
89  else if ( m_value == std::numeric_limits<value_type>::infinity() )
90  return false;
91  else if ( m_value == -std::numeric_limits<value_type>::infinity() )
92  return that.m_value != -std::numeric_limits<value_type>::infinity();
93  else
94  return m_value < (that.m_value - std::max(m_epsilon, that.m_epsilon));
95 } // real_number::operator<()
96 
97 /*----------------------------------------------------------------------------*/
102 template<typename T>
104 {
105  return !(that < *this);
106 } // real_number::operator<=()
107 
108 /*----------------------------------------------------------------------------*/
113 template<typename T>
115 {
116  return that < *this;
117 } // real_number::operator>()
118 
119 /*----------------------------------------------------------------------------*/
124 template<typename T>
126 {
127  return that <= *this;
128 } // real_number::operator>=()
129 
130 /*----------------------------------------------------------------------------*/
135 template<typename T>
137 {
138  if ( that.m_value == std::numeric_limits<value_type>::infinity() )
139  return m_value == std::numeric_limits<value_type>::infinity();
140  else if ( that.m_value == -std::numeric_limits<value_type>::infinity() )
141  return m_value == -std::numeric_limits<value_type>::infinity();
142  else if ( m_value == that.m_value )
143  return true;
144  else
145  return std::abs(m_value - that.m_value)
146  <= std::max(m_epsilon, that.m_epsilon);
147 } // real_number::operator==()
148 
149 /*----------------------------------------------------------------------------*/
154 template<typename T>
156 {
157  return !((*this) == that);
158 } // real_number::operator!=()
159 
160 /*----------------------------------------------------------------------------*/
165 template<typename T>
168 {
169  return self_type(m_value + that.m_value);
170 } // real_number::operator+()
171 
172 /*----------------------------------------------------------------------------*/
177 template<typename T>
180 {
181  return self_type(m_value - that.m_value);
182 } // real_number::operator-()
183 
184 /*----------------------------------------------------------------------------*/
189 template<typename T>
192 {
193  return self_type(m_value * that.m_value);
194 } // real_number::operator*()
195 
196 /*----------------------------------------------------------------------------*/
201 template<typename T>
204 {
205  return self_type(m_value / that.m_value);
206 } // real_number::operator/()
207 
208 /*----------------------------------------------------------------------------*/
213 template<typename T>
216 {
217  m_value += that.m_value;
218  m_epsilon = make_epsilon<value_type>::value(m_value);
219  return *this;
220 } // real_number::operator+=()
221 
222 /*----------------------------------------------------------------------------*/
227 template<typename T>
230 {
231  m_value -= that.m_value;
232  m_epsilon = make_epsilon<value_type>::value(m_value);
233  return *this;
234 } // real_number::operator-=()
235 
236 /*----------------------------------------------------------------------------*/
241 template<typename T>
244 {
245  m_value *= that.m_value;
246  m_epsilon = make_epsilon<value_type>::value(m_value);
247  return *this;
248 } // real_number::operator*=()
249 
250 /*----------------------------------------------------------------------------*/
255 template<typename T>
258 {
259  m_value /= that.m_value;
260  m_epsilon = make_epsilon<value_type>::value(m_value);
261  return *this;
262 } // real_number::operator/=()
263 
264 /*----------------------------------------------------------------------------*/
269 template<typename T>
270 std::ostream& claw::real_number<T>::output( std::ostream& os ) const
271 {
272  return os << m_value;
273 } // real_number::output()
274 
275 /*----------------------------------------------------------------------------*/
279 template<typename T>
280 template<typename U>
282 {
283  return (U)m_value;
284 } // real_number::operator U()
285 
286 /*----------------------------------------------------------------------------*/
291 template<typename T>
292 claw::real_number<T> std::abs( const claw::real_number<T>& v )
293 {
294  return v.abs();
295 } // abs()
296 
297 /*----------------------------------------------------------------------------*/
302 template<typename T>
304 {
305  return claw::real_number<T>(0) - self;
306 } // operator-()
307 
308 /*----------------------------------------------------------------------------*/
314 template<typename T>
315 claw::real_number<T> operator-( T v, const claw::real_number<T>& self )
316 {
317  return claw::real_number<T>(v) - self;
318 } // operator-()
319 
320 /*----------------------------------------------------------------------------*/
326 template<typename T>
327 std::ostream& operator<<( std::ostream& os, const claw::real_number<T>& self )
328 {
329  return self.output(os);
330 } // operator<<()
331 
332 /*----------------------------------------------------------------------------*/
338 template<typename T>
339 std::istream& operator>>( std::istream& is, claw::real_number<T>& self )
340 {
341  return is >> self.m_value;
342 } // operator>>()