#ifndef __JMATH__JNUMBER__ #define __JMATH__JNUMBER__ #include #include #include "JLang/JClass.hh" #include "JLang/JComparable.hh" #include "JMath/JZero.hh" #include "JMath/JMath.hh" /** * \file * * Definition of number value for any class. * \author mdejong */ namespace JMATH {} namespace JPP { using namespace JMATH; } namespace JMATH { using JLANG::JClass; using JLANG::JComparable; /** * Simple wrapper around template data type to ensure that zero is the default value. */ template struct JNumber : public JMath < JNumber >, public JComparable< JNumber > { /** * Default constructor. */ JNumber() : value(getZero()) {} /** * Constructor. * * \param value value */ JNumber(typename JClass::argument_type value) : value(value) {} /** * Type conversion. * * \return value */ inline operator const T&() const { return value; } /** * Type conversion. * * \return value */ inline operator T&() { return value; } /** * Negate number. * * \return this number */ JNumber& negate() { value = -value; return *this; } /** * Add number. * * \param number number * \return this number */ JNumber& add(const JNumber& number) { value += number.value; return *this; } /** * Subtract number. * * \param number number * \return this number */ JNumber& sub(const JNumber& number) { value -= number.value; return *this; } /** * Scale number. * * \param factor multiplication factor * \return this number */ JNumber& mul(const double factor) { value *= factor; return *this; } /** * Scale number. * * \param factor division factor * \return this number */ JNumber& div(const double factor) { value /= factor; return *this; } /** * Less than. * * \param number number * \return true if number is less than given number; else false */ bool less(const JNumber& number) const { return value < number.value; } /** * Read number from input stream. * * \param in input stream * \param number number * \return input stream */ friend inline std::ostream& operator>>(std::ostream& in, JNumber& number) { return in >> number.value; } /** * Write number to output stream. * * \param out output stream * \param number number * \return output stream */ friend inline std::ostream& operator<<(std::ostream& out, const JNumber& number) { return out << number.value; } T value; }; } #endif