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 |
T | 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) |
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 and
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 and
, then
.
As a particular example, if ,
,
, and an absolute tolerance
is used, it turns out that
and
, but
!!!.
This is an example of how to use the BasicScalar class:
|
inline |
Assignment operator.
s | The value to assign to this object. |
|
inline |
Assignment operator.
x | The value to assign to this object. |