00001 /* 00002 * Copyright 2007 Martin von Gagern 00003 * 00004 * 00005 * This file is part of mqn2mps. 00006 * 00007 * mqn2mps is free software; you can redistribute it and/or modify 00008 * it under the terms of the GNU General Public License as published by 00009 * the Free Software Foundation; either version 3 of the License, or 00010 * (at your option) any later version. 00011 * 00012 * mqn2mps is distributed in the hope that it will be useful, 00013 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00015 * GNU General Public License for more details. 00016 * 00017 * You should have received a copy of the GNU General Public License 00018 * along with this program. If not, see <http://www.gnu.org/licenses/>. 00019 */ 00020 00021 00022 #ifndef RATIONAL_HH 00023 #define RATIONAL_HH 00024 00025 #include <iostream> 00026 00027 /** 00028 * @file 00029 * Interface for class rational. 00030 */ 00031 00032 /** 00033 * The class rational represents rational number. 00034 * A rational number is represented by giving a nominator and a 00035 * denominator. Common factors need not necessarily be canceled from 00036 * the fraction. 00037 */ 00038 class rational { 00039 00040 public: 00041 00042 /** 00043 * Nomionator. 00044 */ 00045 int nom; 00046 00047 /** 00048 * Denominator. 00049 */ 00050 int denom; 00051 00052 /** 00053 * Constructor. 00054 * Depending on the number of arguments, this constructor can 00055 * represent the default value of zero, an integer with denominator 00056 * one, or a full fraction with nominator and denominator. 00057 * 00058 * @param n the nominator of the %rational number 00059 * @param d the denominator of the %rational number 00060 */ 00061 rational(int n = 0, int d = 1) : nom(n), denom(d) { } 00062 00063 /** 00064 * Expand fraction to given denominator. 00065 * 00066 * The new denominator must be a multiple of the current 00067 * denominator. The intended use is to calculate the common 00068 * denominator of a set of rationals and then expand each member of 00069 * the set to this denominator. 00070 * 00071 * @param d the new denominator, multiple of current denominator 00072 */ 00073 void set_denom(int d) { nom *= d / denom; denom = d; } 00074 00075 rational& cancel(); 00076 rational& operator*= (const rational& other); 00077 rational operator- (const rational& other) const; 00078 00079 /** 00080 * Implicit conversion to bool and negation. 00081 * @return @c true if the nominator is zero. 00082 */ 00083 bool operator! () const { return !nom; } 00084 00085 /** 00086 * Common pattern for rational comparisons. 00087 * Each comparison operator is implemented by a comparison of the 00088 * crosswise multiplication of both sides, as given by the expansion 00089 * of this macro. 00090 * @param OP the comparison operator to be implemented. 00091 */ 00092 #define RCMP(OP) bool operator OP (const rational& other) const \ 00093 { return nom * other.denom OP denom * other.nom; } 00094 RCMP(==) 00095 RCMP(<) 00096 RCMP(<=) 00097 RCMP(!=) 00098 RCMP(>=) 00099 RCMP(>) 00100 #undef RCMP 00101 00102 }; 00103 00104 std::ostream& operator<<(std::ostream& out, const rational& rat); 00105 00106 #endif // ifndef RATIONAL_HH
1.6.0