CLAW Library (a C++ Library Absolutely Wonderful) 1.5.5
Public Types | Public Member Functions | Private Attributes | Friends
claw::real_number< T > Class Template Reference

Custom precision real numbers. More...

#include <real_number.hpp>

List of all members.

Public Types

typedef T value_type
typedef real_number< T > self_type

Public Member Functions

 real_number ()
 Constructuor.
 real_number (const value_type &v)
 Constructor.
 real_number (const self_type &that)
 Copy constructor.
self_type abs () const
 Get the absolute value of the number.
bool operator< (const self_type &that) const
 Tell if this number is stricty lower than an other number.
bool operator<= (const self_type &that) const
 Tell if this number is lower or equal to an other number.
bool operator> (const self_type &that) const
 Tell if this number is stricty greater than an other number.
bool operator>= (const self_type &that) const
 Tell if this number is greater or equal to an other number.
bool operator== (const self_type &that) const
 Tell if this number is equal to an other number.
bool operator!= (const self_type &that) const
 Tell if this number is not equal to an other number.
self_type operator+ (const self_type &that) const
 Sum two numbers.
self_type operator- (const self_type &that) const
 Get the difference of two numbers.
self_type operator* (const self_type &that) const
 Multiply two numbers.
self_type operator/ (const self_type &that) const
 Divide by an other number.
self_typeoperator+= (const self_type &that)
 Add an other number.
self_typeoperator-= (const self_type &that)
 Subtract an other number.
self_typeoperator*= (const self_type &that)
 Multiply by an other number.
self_typeoperator/= (const self_type &that)
 Divide by an other number.
std::ostream & output (std::ostream &os) const
 Output the value in a stream.
template<typename U >
 operator U () const
 Cast the value.

Private Attributes

value_type m_value
 The value of the number.
value_type m_epsilon
 Precision applied to operators.

Friends

std::istream & operator>> (std::istream &is, real_number< T > &self)
 Read a number from a stream.

Detailed Description

template<typename T>
class claw::real_number< T >

Custom precision real numbers.

Author:
Julien Jorge

Definition at line 67 of file real_number.hpp.


Member Typedef Documentation

template<typename T>
typedef real_number<T> claw::real_number< T >::self_type

Definition at line 74 of file real_number.hpp.

template<typename T>
typedef T claw::real_number< T >::value_type

Definition at line 73 of file real_number.hpp.


Constructor & Destructor Documentation

template<typename T >
claw::real_number< T >::real_number ( )

Constructuor.

Definition at line 37 of file real_number.tpp.

  : m_value(0), m_epsilon( make_epsilon<value_type>::value(m_value) )
{

} // real_number::real_number()
template<typename T >
claw::real_number< T >::real_number ( const value_type v)

Constructor.

Parameters:
vThe value of the number.

Definition at line 49 of file real_number.tpp.

  : m_value(v), m_epsilon( make_epsilon<T>::value(m_value) )
{

} // real_number::real_number()
template<typename T >
claw::real_number< T >::real_number ( const self_type that)

Copy constructor.

Parameters:
thatThe instance to copy from.

Definition at line 61 of file real_number.tpp.

  : m_value(that.m_value), m_epsilon(that.m_epsilon)
{

} // real_number::real_number()

Member Function Documentation

template<typename T >
claw::real_number< T >::self_type claw::real_number< T >::abs ( ) const

Get the absolute value of the number.

Definition at line 72 of file real_number.tpp.

References std::abs().

Referenced by std::abs().

{
  return self_type( std::abs(m_value) );
} // real_number::abs()
template<typename T >
template<typename U >
claw::real_number< T >::operator U ( ) const

Cast the value.

Definition at line 281 of file real_number.tpp.

{
  return (U)m_value;
} // real_number::operator U()
template<typename T >
bool claw::real_number< T >::operator!= ( const self_type that) const

Tell if this number is not equal to an other number.

Parameters:
thatThe other number.

Definition at line 155 of file real_number.tpp.

{
  return !((*this) == that);
} // real_number::operator!=()
template<typename T >
claw::real_number< T >::self_type claw::real_number< T >::operator* ( const self_type that) const

Multiply two numbers.

Parameters:
thatThe other number.

Definition at line 191 of file real_number.tpp.

References claw::real_number< T >::m_value.

{
  return self_type(m_value * that.m_value);
} // real_number::operator*()
template<typename T >
claw::real_number< T >::self_type & claw::real_number< T >::operator*= ( const self_type that)

Multiply by an other number.

Parameters:
thatThe other number.

Definition at line 243 of file real_number.tpp.

References claw::real_number< T >::m_value, and claw::make_epsilon< T >::value().

{
  m_value *= that.m_value;
  m_epsilon = make_epsilon<value_type>::value(m_value);
  return *this;
} // real_number::operator*=()
template<typename T >
claw::real_number< T >::self_type claw::real_number< T >::operator+ ( const self_type that) const

Sum two numbers.

Parameters:
thatThe other number.

Definition at line 167 of file real_number.tpp.

References claw::real_number< T >::m_value.

{
  return self_type(m_value + that.m_value);
} // real_number::operator+()
template<typename T >
claw::real_number< T >::self_type & claw::real_number< T >::operator+= ( const self_type that)

Add an other number.

Parameters:
thatThe other number.

Definition at line 215 of file real_number.tpp.

References claw::real_number< T >::m_value, and claw::make_epsilon< T >::value().

{
  m_value += that.m_value;
  m_epsilon = make_epsilon<value_type>::value(m_value);
  return *this;
} // real_number::operator+=()
template<typename T >
claw::real_number< T >::self_type claw::real_number< T >::operator- ( const self_type that) const

Get the difference of two numbers.

Parameters:
thatThe other number.

Definition at line 179 of file real_number.tpp.

References claw::real_number< T >::m_value.

{
  return self_type(m_value - that.m_value);
} // real_number::operator-()
template<typename T >
claw::real_number< T >::self_type & claw::real_number< T >::operator-= ( const self_type that)

Subtract an other number.

Parameters:
thatThe other number.

Definition at line 229 of file real_number.tpp.

References claw::real_number< T >::m_value, and claw::make_epsilon< T >::value().

{
  m_value -= that.m_value;
  m_epsilon = make_epsilon<value_type>::value(m_value);
  return *this;
} // real_number::operator-=()
template<typename T >
claw::real_number< T >::self_type claw::real_number< T >::operator/ ( const self_type that) const

Divide by an other number.

Parameters:
thatThe other number.

Definition at line 203 of file real_number.tpp.

References claw::real_number< T >::m_value.

{
  return self_type(m_value / that.m_value);
} // real_number::operator/()
template<typename T >
claw::real_number< T >::self_type & claw::real_number< T >::operator/= ( const self_type that)

Divide by an other number.

Parameters:
thatThe other number.

Definition at line 257 of file real_number.tpp.

References claw::real_number< T >::m_value, and claw::make_epsilon< T >::value().

{
  m_value /= that.m_value;
  m_epsilon = make_epsilon<value_type>::value(m_value);
  return *this;
} // real_number::operator/=()
template<typename T >
bool claw::real_number< T >::operator< ( const self_type that) const

Tell if this number is stricty lower than an other number.

Parameters:
thatThe other number.

Definition at line 83 of file real_number.tpp.

References claw::real_number< T >::m_epsilon, and claw::real_number< T >::m_value.

{
  if ( that.m_value == std::numeric_limits<value_type>::infinity() )
    return m_value != std::numeric_limits<value_type>::infinity();
  else if ( that.m_value == -std::numeric_limits<value_type>::infinity() )
    return false;
  else if ( m_value == std::numeric_limits<value_type>::infinity() )
    return false;
  else if ( m_value == -std::numeric_limits<value_type>::infinity() )
    return that.m_value != -std::numeric_limits<value_type>::infinity();
  else
    return m_value < (that.m_value - std::max(m_epsilon, that.m_epsilon));
} // real_number::operator<()
template<typename T >
bool claw::real_number< T >::operator<= ( const self_type that) const

Tell if this number is lower or equal to an other number.

Parameters:
thatThe other number.

Definition at line 103 of file real_number.tpp.

{
  return !(that < *this);
} // real_number::operator<=()
template<typename T >
bool claw::real_number< T >::operator== ( const self_type that) const

Tell if this number is equal to an other number.

Parameters:
thatThe other number.

Definition at line 136 of file real_number.tpp.

References std::abs(), claw::real_number< T >::m_epsilon, and claw::real_number< T >::m_value.

{
  if ( that.m_value == std::numeric_limits<value_type>::infinity() )
    return m_value == std::numeric_limits<value_type>::infinity();
  else if ( that.m_value == -std::numeric_limits<value_type>::infinity() )
    return m_value == -std::numeric_limits<value_type>::infinity();
  else if ( m_value == that.m_value )
    return true;
  else
    return std::abs(m_value - that.m_value)
      <= std::max(m_epsilon, that.m_epsilon);
} // real_number::operator==()
template<typename T >
bool claw::real_number< T >::operator> ( const self_type that) const

Tell if this number is stricty greater than an other number.

Parameters:
thatThe other number.

Definition at line 114 of file real_number.tpp.

{
  return that < *this;
} // real_number::operator>()
template<typename T >
bool claw::real_number< T >::operator>= ( const self_type that) const

Tell if this number is greater or equal to an other number.

Parameters:
thatThe other number.

Definition at line 125 of file real_number.tpp.

{
  return that <= *this;
} // real_number::operator>=()
template<typename T >
std::ostream & claw::real_number< T >::output ( std::ostream &  os) const

Output the value in a stream.

Parameters:
osThe stream in which the value is written.

Definition at line 270 of file real_number.tpp.

Referenced by operator<<().

{
  return os << m_value;
} // real_number::output()

Friends And Related Function Documentation

template<typename T>
std::istream& operator>> ( std::istream &  is,
real_number< T > &  self 
) [friend]

Read a number from a stream.

Parameters:
isThe stream from which the value is read.
selfThe number.

Definition at line 339 of file real_number.tpp.

{
  return is >> self.m_value;
} // operator>>()

Member Data Documentation

template<typename T>
value_type claw::real_number< T >::m_epsilon [private]

Precision applied to operators.

Definition at line 110 of file real_number.hpp.

Referenced by claw::real_number< T >::operator<(), and claw::real_number< T >::operator==().

template<typename T>
value_type claw::real_number< T >::m_value [private]

The documentation for this class was generated from the following files: