2 CLAW - a C++ Library Absolutely Wonderful
4 CLAW is a free library without any particular aim but being useful to
7 Copyright (C) 2005-2011 Julien Jorge
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.
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.
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
23 contact: julien.jorge@gamned.org
26 * \file claw/impl/real_number.tpp
27 * \brief Implementation of the claw::real_number class.
28 * \author Julien Jorge
32/*----------------------------------------------------------------------------*/
34 * \brief Constructuor.
37claw::real_number<T>::real_number()
38 : m_value(0), m_epsilon( make_epsilon<value_type>::value(m_value) )
41} // real_number::real_number()
43/*----------------------------------------------------------------------------*/
46 * \param v The value of the number.
49claw::real_number<T>::real_number( const value_type& v )
50 : m_value(v), m_epsilon( make_epsilon<T>::value(m_value) )
53} // real_number::real_number()
55/*----------------------------------------------------------------------------*/
57 * \brief Copy constructor.
58 * \param that The instance to copy from.
61claw::real_number<T>::real_number( const self_type& that )
62 : m_value(that.m_value), m_epsilon(that.m_epsilon)
65} // real_number::real_number()
67/*----------------------------------------------------------------------------*/
69 * \brief Get the absolute value of the number.
72typename claw::real_number<T>::self_type claw::real_number<T>::abs() const
74 return self_type( std::abs(m_value) );
75} // real_number::abs()
77/*----------------------------------------------------------------------------*/
79 * \brief Tell if this number is stricty lower than an other number.
80 * \param that The other number.
83bool claw::real_number<T>::operator<( const self_type& that ) const
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() )
89 else if ( m_value == std::numeric_limits<value_type>::infinity() )
91 else if ( m_value == -std::numeric_limits<value_type>::infinity() )
92 return that.m_value != -std::numeric_limits<value_type>::infinity();
94 return m_value < (that.m_value - std::max(m_epsilon, that.m_epsilon));
95} // real_number::operator<()
97/*----------------------------------------------------------------------------*/
99 * \brief Tell if this number is lower or equal to an other number.
100 * \param that The other number.
103bool claw::real_number<T>::operator<=( const self_type& that ) const
105 return !(that < *this);
106} // real_number::operator<=()
108/*----------------------------------------------------------------------------*/
110 * \brief Tell if this number is stricty greater than an other number.
111 * \param that The other number.
114bool claw::real_number<T>::operator>( const self_type& that ) const
117} // real_number::operator>()
119/*----------------------------------------------------------------------------*/
121 * \brief Tell if this number is greater or equal to an other number.
122 * \param that The other number.
125bool claw::real_number<T>::operator>=( const self_type& that ) const
127 return that <= *this;
128} // real_number::operator>=()
130/*----------------------------------------------------------------------------*/
132 * \brief Tell if this number is equal to an other number.
133 * \param that The other number.
136bool claw::real_number<T>::operator==( const self_type& that ) const
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 )
145 return std::abs(m_value - that.m_value)
146 <= std::max(m_epsilon, that.m_epsilon);
147} // real_number::operator==()
149/*----------------------------------------------------------------------------*/
151 * \brief Tell if this number is not equal to an other number.
152 * \param that The other number.
155bool claw::real_number<T>::operator!=( const self_type& that ) const
157 return !((*this) == that);
158} // real_number::operator!=()
160/*----------------------------------------------------------------------------*/
162 * \brief Sum two numbers.
163 * \param that The other number.
166typename claw::real_number<T>::self_type
167claw::real_number<T>::operator+( const self_type& that ) const
169 return self_type(m_value + that.m_value);
170} // real_number::operator+()
172/*----------------------------------------------------------------------------*/
174 * \brief Get the difference of two numbers.
175 * \param that The other number.
178typename claw::real_number<T>::self_type
179claw::real_number<T>::operator-( const self_type& that ) const
181 return self_type(m_value - that.m_value);
182} // real_number::operator-()
184/*----------------------------------------------------------------------------*/
186 * \brief Multiply two numbers.
187 * \param that The other number.
190typename claw::real_number<T>::self_type
191claw::real_number<T>::operator*( const self_type& that ) const
193 return self_type(m_value * that.m_value);
194} // real_number::operator*()
196/*----------------------------------------------------------------------------*/
198 * \brief Divide by an other number.
199 * \param that The other number.
202typename claw::real_number<T>::self_type
203claw::real_number<T>::operator/( const self_type& that ) const
205 return self_type(m_value / that.m_value);
206} // real_number::operator/()
208/*----------------------------------------------------------------------------*/
210 * \brief Add an other number.
211 * \param that The other number.
214typename claw::real_number<T>::self_type&
215claw::real_number<T>::operator+=( const self_type& that )
217 m_value += that.m_value;
218 m_epsilon = make_epsilon<value_type>::value(m_value);
220} // real_number::operator+=()
222/*----------------------------------------------------------------------------*/
224 * \brief Subtract an other number.
225 * \param that The other number.
228typename claw::real_number<T>::self_type&
229claw::real_number<T>::operator-=( const self_type& that )
231 m_value -= that.m_value;
232 m_epsilon = make_epsilon<value_type>::value(m_value);
234} // real_number::operator-=()
236/*----------------------------------------------------------------------------*/
238 * \brief Multiply by an other number.
239 * \param that The other number.
242typename claw::real_number<T>::self_type&
243claw::real_number<T>::operator*=( const self_type& that )
245 m_value *= that.m_value;
246 m_epsilon = make_epsilon<value_type>::value(m_value);
248} // real_number::operator*=()
250/*----------------------------------------------------------------------------*/
252 * \brief Divide by an other number.
253 * \param that The other number.
256typename claw::real_number<T>::self_type&
257claw::real_number<T>::operator/=( const self_type& that )
259 m_value /= that.m_value;
260 m_epsilon = make_epsilon<value_type>::value(m_value);
262} // real_number::operator/=()
264/*----------------------------------------------------------------------------*/
266 * \brief Output the value in a stream.
267 * \param os The stream in which the value is written.
270std::ostream& claw::real_number<T>::output( std::ostream& os ) const
272 return os << m_value;
273} // real_number::output()
275/*----------------------------------------------------------------------------*/
277 * \brief Cast the value.
281claw::real_number<T>::operator U() const
284} // real_number::operator U()
286/*----------------------------------------------------------------------------*/
288 * \brief Get the absolute value of a number.
289 * \param v The number.
292claw::real_number<T> std::abs( const claw::real_number<T>& v )
297/*----------------------------------------------------------------------------*/
299 * \brief Get the opposite value of a number.
300 * \param self The number.
303claw::real_number<T> operator-( const claw::real_number<T>& self )
305 return claw::real_number<T>(0) - self;
308/*----------------------------------------------------------------------------*/
310 * \brief Subtract a number to an other value.
311 * \param v The other value.
312 * \param self The number.
315claw::real_number<T> operator-( T v, const claw::real_number<T>& self )
317 return claw::real_number<T>(v) - self;
320/*----------------------------------------------------------------------------*/
322 * \brief Output a number in a stream.
323 * \param os The stream in which the value is written.
324 * \param self The number.
327std::ostream& operator<<( std::ostream& os, const claw::real_number<T>& self )
329 return self.output(os);
332/*----------------------------------------------------------------------------*/
334 * \brief Read a number from a stream.
335 * \param is The stream from which the value is read.
336 * \param self The number.
339std::istream& operator>>( std::istream& is, claw::real_number<T>& self )
341 return is >> self.m_value;