/* $Id$ */ /* * Copyright (C) 2005-2006 Richard Braun * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ #ifndef _MTXRATIONAL_H #define _MTXRATIONAL_H #include #include #include "mtx.h" #include "mtxvalue.h" #include "mtxinteger.h" /** * A rational. * * This class merely allows to create rationals which consist of a numerator * and a denominator which are both MtxInteger objects. */ class MtxRational: public MtxValue { private: /** * Numerator. */ MtxInteger numerator; /** * Denominator. */ MtxInteger denominator; /** * Precision used when computing floating point representation. * * @see getPrecision() * @see setPrecision() */ static unsigned int precision; public: /** * Default constructor. * * 0/1 is the default value. */ MtxRational(); /** * Constructor usable for "rational = 123" cases. */ MtxRational(int integer); /** * Constructor usable for "rational = 1.23" cases. * * If 1.23 is given to this constructor, numerator is 123 and * denominator is 100. */ MtxRational(double rational); /** * Constructor usable for "rational = "1.23"" cases. * * Same as MtxRational(double rational), with a character string for * larger values. * * @see MtxRational(double rational) */ MtxRational(const char *rational); /** * Main constructor. */ MtxRational(const MtxInteger &numerator, const MtxInteger &denominator = 1); virtual ~MtxRational(); /** * Get the numerator. */ MtxInteger getNumerator() const; /** * Set the numerator. */ void setNumerator(const MtxInteger &numerator); /** * Get the denominator. */ MtxInteger getDenominator() const; /** * Set the denominator. An exception is raised if the given denominator * is 0. */ void setDenominator(const MtxInteger &denominator); /* * Some operators are available for easiness. */ MtxRational operator+() const; MtxRational operator-() const; bool operator==(const MtxRational &rational) const; bool operator!=(const MtxRational &rational) const; bool operator>(const MtxRational &rational) const; bool operator>=(const MtxRational &rational) const; bool operator<(const MtxRational &rational) const; bool operator<=(const MtxRational &rational) const; MtxRational operator+(const MtxRational &rational) const; MtxMatrix operator+(const MtxMatrix &matrix) const; MtxRational & operator++(); MtxRational & operator++(int a); MtxRational & operator+=(const MtxRational &rational); MtxRational operator-(const MtxRational &rational) const; MtxMatrix operator-(const MtxMatrix &matrix) const; MtxRational & operator--(); MtxRational & operator--(int a); MtxRational & operator-=(const MtxRational &rational); MtxRational operator*(const MtxRational &rational) const; MtxMatrix operator*(const MtxMatrix &matrix) const; MtxRational & operator*=(const MtxRational &rational); MtxRational operator/(const MtxRational &rational) const; MtxMatrix operator/(const MtxMatrix &matrix) const; MtxRational & operator/=(const MtxRational &rational); friend std::ostream & operator<<(std::ostream &cout, const MtxRational &rational); /** * Simplify the numerator and denominator if possible. * * E.g. 5/10 becomes 1/2. */ void simplify(); /** * Normalize the rational. * * If it's negative, the sign is set on the numerator. It is then * simplified. The object is returned so that this function can be * used inline. */ MtxRational & normalize(); /** * Return the numerator[/denominator] string representation of * this rational. If denominator is 1, it is ommited. */ virtual std::string toString() const; /** * Return the floating point representation of this rational. */ std::string toFloatString() const; /* * See mtxvalue.h. */ MTXVALUE_CLONE_METHOD_DECLARATION(MtxRational); virtual MtxValue * add(const MtxValue *value) const; virtual MtxValue * subtract(const MtxValue *value) const; virtual MtxValue * multiply(const MtxValue *value) const; virtual MtxValue * divide(const MtxValue *value) const; virtual MtxValue * opposite() const; /** * Return the reciprocal of this rational. */ MtxRational reciprocal() const; /** * Get precision. * * @see precision * @see setPrecision() */ static unsigned int getPrecision(); /** * Set precision. * * @see precision * @see getPrecision() */ static void setPrecision(unsigned int new_precision); }; #endif /* _MTXRATIONAL_H */