mt
 All Classes Files Functions Enumerations Groups Pages
Public Member Functions | Friends | List of all members
mt::BasicScalar< T > Class Template Reference

Floating point number with tolerance-based comparison operators. More...

#include <basic_scalar.h>

Public Member Functions

 BasicScalar (const T &x=0.0)
 Constructor for input value.
template<class T2 >
 BasicScalar (const BasicScalar< T2 > &s)
 Copy constructor.
template<class T2 >
BasicScalar< T > & operator= (const BasicScalar< T2 > &s)
BasicScalar< T > & operator= (const T &x)
BasicScalar< T > & operator+= (const BasicScalar< T > &s)
BasicScalar< T > & operator-= (const BasicScalar< T > &s)
BasicScalar< T > & operator*= (const BasicScalar< T > &s)
BasicScalar< T > & operator/= (const BasicScalar< T > &s)
bool operator== (const BasicScalar< T > &s) const
bool operator< (const BasicScalar< T > &s) const
bool operator!= (const BasicScalar< T > &s) const
bool operator> (const BasicScalar< T > &s) const
bool operator<= (const BasicScalar< T > &s) const
bool operator>= (const BasicScalar< T > &s) const
getValue () const
 Gets the scalar value.
T & getRef ()
 Gets a reference to the scalar value.
const T & getRef () const
 Gets a constant reference to the scalar value.
void setValue (const T &x)
 Sets the scalar's value.

Friends

template<class T1 >
std::ostream & operator<< (std::ostream &os, const BasicScalar< T1 > &s)
template<class T1 >
std::istream & operator>> (std::istream &is, const BasicScalar< T1 > &s)
template<class T1 >
BasicScalar< T1 > min (const BasicScalar< T1 > &s1, const BasicScalar< T1 > &s2)
template<class T1 >
BasicScalar< T1 > max (const BasicScalar< T1 > &s1, const BasicScalar< T1 > &s2)
template<class T1 >
BasicScalar< T1 > abs (const BasicScalar< T1 > &s)
template<class T1 >
BasicScalar< T1 > ceil (const BasicScalar< T1 > &s)
template<class T1 >
BasicScalar< T1 > floor (const BasicScalar< T1 > &s)
template<class T1 >
BasicScalar< T1 > sqrt (const BasicScalar< T1 > &s)
template<class T1 >
BasicScalar< T1 > exp (const BasicScalar< T1 > &s)
template<class T1 >
BasicScalar< T1 > log (const BasicScalar< T1 > &s)
template<class T1 >
BasicScalar< T1 > log10 (const BasicScalar< T1 > &s)
template<class T1 , class T2 >
BasicScalar< T1 > pow (const BasicScalar< T1 > &s1, const BasicScalar< T2 > &s2)
template<class T1 >
BasicScalar< T1 > sin (const BasicScalar< T1 > &s)
template<class T1 >
BasicScalar< T1 > cos (const BasicScalar< T1 > &s)
template<class T2 >
BasicScalar< T2 > tan (const BasicScalar< T2 > &s)
template<class T2 >
BasicScalar< T2 > sinh (const BasicScalar< T2 > &s)
template<class T2 >
BasicScalar< T2 > cosh (const BasicScalar< T2 > &s)
template<class T2 >
BasicScalar< T2 > tanh (const BasicScalar< T2 > &s)
template<class T2 >
BasicScalar< T2 > asin (const BasicScalar< T2 > &s)
template<class T2 >
BasicScalar< T2 > acos (const BasicScalar< T2 > &s)
template<class T2 >
BasicScalar< T2 > atan (const BasicScalar< T2 > &s)
template<class T1 , class T2 >
BasicScalar< T1 > atan2 (const BasicScalar< T1 > &s1, const BasicScalar< T2 > &s2)

Detailed Description

template<class T>
class mt::BasicScalar< T >

Floating point number with tolerance-based comparison operators.

The BasicScalar class is used when floating point numbers comparisons depend on a tolerance. The class wraps a floating point datatype -the template parameter- and provides an interface with the usual operations for the original datatype such as operators, mathematical functions, etc.

The equality and less-than operators -hence the other operators that derive from these two- take into account a tolerance while performing the respective tests. The user can specify whether an absolute or relative tolerance is to be used, as well as its value by means of the BasicScalarTraits traits class. It is important to notice that such information is stored in static variables, so a change in the tolerance type or value will affect all instances of the class (but not the results of previously performed tests).

The default user-defined tolerance is absolute and equal to 0.0001.

Even if the user-defined tolerance is set to zero, the epsilon associated to the current floating-point representation is taken into account in the tests. This is done to compensate for numbers that cannot be expressed exactly using the current representation, case in which the difference between the real value and its representation is upper bounded by epsilon.

The criteria used for comparing two scalars $ s_1 $ and $ s_2 $ is the following:

where

A very important remark is that tolerance-based tests such as the ones presented above are not transitive, that is, the following does not necessarily hold: if $ (a = b)$ and $ (b = c) $, then $ (a = c) $.

As a particular example, if $ a = 0.9 $, $ b = 1.0 $, $ c = 1.1 $, and an absolute tolerance $ tol = 0.1 $ is used, it turns out that $ (a = b) $ and $ (b = c) $, but $ (a \neq c) $!!!.

This is an example of how to use the BasicScalar class:

using namespace mt;
// Typedefs are a good idea to make code more readable
typedef BasicScalarTraits<float> ScalarTraitsF;
typedef BasicScalarTraits<double> ScalarTraitsD;
typedef BasicScalar<float> ScalarF;
typedef BasicScalar<double> ScalarD;
// Tests using floats and a relative tolerance of 1e-2
ScalarTraitsF::setTolType(ScalarTraitsF::RELATIVE);
ScalarTraitsF::setTol(1e-2);
ScalarF f1(100.0f);
ScalarF f2(99.0f);
bool test;
test = (f1 == f2); // test = true
test = (f1 < f2); // test = false
// Tests using doubles and an absolute tolerance of 1e-3
ScalarTraitsD::setTolType(ScalarTraitsD::ABSOLUTE);
ScalarTraitsD::setTol(1e-3);
ScalarD d1(1.000);
ScalarD d2(1.0011);
test = (d1 == d2); // test = false
test = (d1 < d2); // test = true

Member Function Documentation

template<class T1 >
template<class T2 >
BasicScalar< T1 > & mt::BasicScalar< T1 >::operator= ( const BasicScalar< T2 > &  s)
inline

Assignment operator.

Parameters
sThe value to assign to this object.
Returns
A reference to this object.
template<class T >
BasicScalar< T > & mt::BasicScalar< T >::operator= ( const T &  x)
inline

Assignment operator.

Parameters
xThe value to assign to this object.
Returns
A reference to this object.

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