00001 /// 00002 /// \file ratio.cpp 00003 /// \brief Provides a simple ratio class. 00004 /// 00005 /// \author Kent Holsinger 00006 /// \date 2005-05-18 00007 /// 00008 00009 // This file is part of MCMC++, a library for constructing C++ programs 00010 // that implement MCMC analyses of Bayesian statistical models. 00011 // Copyright (c) 2004-2006 Kent E. Holsinger 00012 // 00013 // MCMC++ is free software; you can redistribute it and/or modify 00014 // it under the terms of the GNU General Public License as published by 00015 // the Free Software Foundation; either version 2 of the License, or 00016 // (at your option) any later version. 00017 // 00018 // MCMC++ is distributed in the hope that it will be useful, 00019 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00020 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00021 // GNU General Public License for more details. 00022 // 00023 // You should have received a copy of the GNU General Public License 00024 // along with MCMC++; if not, write to the Free Software 00025 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00026 // 00027 00028 // local includes 00029 #include "mcmc++/ratio.h" 00030 00031 /// \class ratio 00032 /// \brief Provides a ratio class 00033 /// 00034 /// The ratio class allows numerators and denominators to be summed separately. 00035 /// Access to each is provided. 00036 /// 00037 00038 /// Default constructor. 00039 /// 00040 /// Initializes private variables 00041 /// 00042 ratio::ratio(void) : 00043 top_(0.0), bottom_(0.0) 00044 {} 00045 00046 /// Copy constructor. 00047 /// 00048 /// Initializes private variables from existing ratio 00049 /// 00050 ratio::ratio(const ratio& r) 00051 : top_(r.top_), bottom_(r.bottom_) 00052 {} 00053 00054 /// Add ratio to current ratio. 00055 /// 00056 /// \param r The ratio to add 00057 /// 00058 ratio& 00059 ratio::operator +=(const ratio& r) { 00060 top_ += r.top_; 00061 bottom_ += r.bottom_; 00062 return *this; 00063 } 00064 00065 /// Add double to current ratio. 00066 /// 00067 /// Adds the d to both numerator and denominator 00068 /// 00069 /// \param d The double value to add 00070 /// 00071 ratio& 00072 ratio::operator +=(const double d) { 00073 top_ += d; 00074 bottom_ += d; 00075 return *this; 00076 } 00077 00078 /// Divide one ratio by another. 00079 /// 00080 /// Numerator divided by numerator. Denominator divided by denominator. 00081 /// 00082 /// \param r The ratio to be used in the "denominator" of the division 00083 /// 00084 ratio& 00085 ratio::operator /=(const ratio& r) { 00086 top_ /= r.top_; 00087 bottom_ /= r.bottom_; 00088 return *this; 00089 } 00090 00091 /// Divide a ratio by a double. 00092 /// 00093 /// Numerator and denominator both divided by d. 00094 /// 00095 /// \param d 00096 /// 00097 ratio& 00098 ratio::operator /=(const double d) { 00099 top_ /= d; 00100 bottom_ /= d; 00101 return *this; 00102 } 00103 00104 /// Assignment operator. 00105 /// 00106 /// \param r The value being assigned 00107 /// 00108 ratio& 00109 ratio::operator =(const ratio& r) { 00110 top_ = r.top_; 00111 bottom_ = r.bottom_; 00112 return *this; 00113 } 00114 00115 /// Equality test. 00116 /// 00117 /// Equal if and only if top_ and bottom_ of both ratios are equal. 00118 /// 00119 /// \param r The value being compared 00120 /// 00121 bool 00122 ratio::operator ==(const ratio& r) const { 00123 return ( top_ == r.top_ && bottom_ == r.bottom_ ); 00124 } 00125 00126 /// Inequality test. 00127 /// 00128 /// Not equal if top_s or bottom_s are unequal 00129 /// 00130 /// \param r The value being compared 00131 /// 00132 bool 00133 ratio::operator !=(const ratio& r) const { 00134 return ( top_ != r.top_ || bottom_ != r.bottom_ ); 00135 } 00136 00137 /// Value of ratio 00138 /// 00139 double 00140 ratio::make_double(void) const { 00141 double retval; 00142 if ( bottom_ != 0.0 ) { 00143 retval = top_/bottom_; 00144 } else if ( top_ < 0.0 ) { 00145 retval = Util::long_min; 00146 } else if ( top_ > 0.0 ) { 00147 retval = Util::long_max; 00148 } 00149 return retval; 00150 } 00151 00152 /// Numerator of ratio 00153 /// 00154 double 00155 ratio::Top(void) const { 00156 return top_; 00157 } 00158 00159 /// Denominator of ratio 00160 /// 00161 double 00162 ratio::Bottom(void) const { 00163 return bottom_; 00164 }
1.5.1