BWAPI
SPAR/AIModule/BWTA/vendors/CGAL/CGAL/CORE_BigRat.h
Go to the documentation of this file.
00001 // Copyright (c) 2006-2008 Max-Planck-Institute Saarbruecken (Germany).
00002 // All rights reserved.
00003 //
00004 // This file is part of CGAL (www.cgal.org); you can redistribute it and/or
00005 // modify it under the terms of the GNU Lesser General Public License as
00006 // published by the Free Software Foundation; version 2.1 of the License.
00007 // See the file LICENSE.LGPL distributed with CGAL.
00008 //
00009 // Licensees holding a valid commercial license may use this file in
00010 // accordance with the commercial license agreement provided with the software.
00011 //
00012 // This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
00013 // WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
00014 //
00015 // $URL: svn+ssh://scm.gforge.inria.fr/svn/cgal/branches/CGAL-3.5-branch/Number_types/include/CGAL/CORE_BigRat.h $
00016 // $Id: CORE_BigRat.h 47264 2008-12-08 06:25:14Z hemmer $
00017 //
00018 //
00019 // Author(s)     : Michael Hemmer   <hemmer@mpi-inf.mpg.de>
00020 
00021 
00022 #ifndef CGAL_CORE_BIGRAT_H
00023 #define CGAL_CORE_BIGRAT_H
00024 
00025 #include <CGAL/number_type_basic.h>
00026 #include <CGAL/CORE_coercion_traits.h>
00027 #include <CGAL/CORE_Expr.h> // used for To_interval-functor
00028 
00029 //#if defined(CGAL_CORE_BIGRAT_NUMER_DENOM_ARE_MEMBERS)
00030 //  #define CGAL_CORE_NUMERATOR(X) ((X).numerator())
00031 //  #define CGAL_CORE_DENOMINATOR(X) ((X).denominator())
00032 //#elif defined(CGAL_CORE_BIGRAT_NUMER_DENOM_ARE_NONMEMBERS)
00033   #define CGAL_CORE_NUMERATOR(X) (numerator((X)))
00034   #define CGAL_CORE_DENOMINATOR(X) (denominator((X)))
00035 //#else
00036 
00037 CGAL_BEGIN_NAMESPACE
00038 
00039 //
00040 // Algebraic structure traits
00041 //
00042 template <> class Algebraic_structure_traits< CORE::BigRat >
00043   : public Algebraic_structure_traits_base< CORE::BigRat,
00044                                             Field_tag >  {
00045   public:
00046     typedef Tag_true            Is_exact;
00047     typedef Tag_false           Is_numerical_sensitive;
00048 
00049     // BigRat are always normalized, so no special simplify-functor is needed
00050 
00051     // Nothing new...
00052 };
00053 
00054 
00055 
00056 
00057 //
00058 // Real embeddable traits
00059 //
00060 template <> class Real_embeddable_traits< CORE::BigRat >
00061   : public INTERN_RET::Real_embeddable_traits_base< CORE::BigRat , CGAL::Tag_true > {
00062   public:
00063 
00064     class Abs
00065       : public std::unary_function< Type, Type > {
00066       public:
00067         Type operator()( const Type& x ) const {
00068           return CORE::abs( x );
00069         }
00070     };
00071 
00072     class Sgn
00073       : public std::unary_function< Type, ::CGAL::Sign > {
00074       public:
00075         ::CGAL::Sign operator()( const Type& x ) const {
00076           return (::CGAL::Sign) CORE::sign( x );
00077         }
00078     };
00079 
00080     class Compare
00081       : public std::binary_function< Type, Type,
00082                                 Comparison_result > {
00083       public:
00084         Comparison_result operator()( const Type& x,
00085                                             const Type& y ) const {
00086           return CGAL::sign( ::CORE::cmp(x,y));
00087         }
00088     };
00089 
00090     class To_double
00091       : public std::unary_function< Type, double > {
00092       public:
00093         double operator()( const Type& x ) const {
00094           // this call is required to get reasonable values for the double
00095           // approximation
00096           return x.doubleValue();
00097         }
00098     };
00099 
00100     class To_interval
00101       : public std::unary_function< Type, std::pair< double, double > > {
00102       public:
00103         std::pair<double, double> operator()( const Type& x_ ) const {
00104             CORE::Expr x(x_);
00105             std::pair<double,double> result;
00106             x.doubleInterval(result.first, result.second);
00107             CGAL_expensive_assertion(result.first  <= x);
00108             CGAL_expensive_assertion(result.second >= x);
00109             return result;
00110         }
00111     };
00112 };
00113 
00117 template <>
00118 class Fraction_traits< CORE::BigRat > {
00119 public:
00120     typedef CORE::BigRat Type;
00121     typedef ::CGAL::Tag_true Is_fraction;
00122     typedef CORE::BigInt Numerator_type;
00123     typedef Numerator_type Denominator_type;
00124 
00125     typedef Algebraic_structure_traits< Numerator_type >::Gcd Common_factor;
00126 
00127     class Decompose {
00128     public:
00129         typedef Type first_argument_type;
00130         typedef Numerator_type& second_argument_type;
00131         typedef Numerator_type& third_argument_type;
00132         void operator () (
00133                 const Type& rat,
00134                 Numerator_type& num,
00135                 Numerator_type& den) {
00136             num = CGAL_CORE_NUMERATOR(rat);
00137             den = CGAL_CORE_DENOMINATOR(rat);
00138         }
00139     };
00140 
00141     class Compose {
00142     public:
00143         typedef Numerator_type first_argument_type;
00144         typedef Numerator_type second_argument_type;
00145         typedef Type result_type;
00146         Type operator ()(
00147                 const Numerator_type& num ,
00148                 const Numerator_type& den ) {
00149             return Type(num, den);
00150         }
00151     };
00152 };
00153 
00154 template <class F>
00155 class Output_rep< ::CORE::BigRat, F> {
00156     const ::CORE::BigRat& t;
00157 public:
00159     Output_rep( const ::CORE::BigRat& tt) : t(tt) {}
00161     std::ostream& operator()( std::ostream& out) const {
00162         switch (get_mode(out)) {
00163         case IO::PRETTY:{
00164             if(CGAL_CORE_DENOMINATOR(t) == ::CORE::BigRat(1))
00165                 return out <<CGAL_CORE_NUMERATOR(t);
00166             else
00167                 return out << CGAL_CORE_NUMERATOR(t)
00168                            << "/"
00169                            << CGAL_CORE_DENOMINATOR(t);
00170             //break; // unreachable
00171         }
00172 
00173         default:
00174             return out << CGAL_CORE_NUMERATOR(t)
00175                        << "/"
00176                        << CGAL_CORE_DENOMINATOR(t);
00177         }
00178     }
00179 };
00180 
00181 template <>
00182 struct Needs_parens_as_product< ::CORE::BigRat >{
00183     bool operator()( ::CORE::BigRat t){
00184         if (CGAL_CORE_DENOMINATOR(t) != 1 )
00185             return true;
00186         else
00187             return needs_parens_as_product(CGAL_CORE_NUMERATOR(t)) ;
00188     }
00189 };
00190 
00191 template <>
00192 class Output_rep< ::CORE::BigRat, Parens_as_product_tag > {
00193     const ::CORE::BigRat& t;
00194 public:
00195     // Constructor
00196     Output_rep( const ::CORE::BigRat& tt) : t(tt) {}
00197     // operator
00198     std::ostream& operator()( std::ostream& out) const {
00199         Needs_parens_as_product< ::CORE::BigRat > needs_parens_as_product;
00200         if (needs_parens_as_product(t))
00201             return out <<"("<< oformat(t) <<")";
00202         else
00203             return out << oformat(t);
00204     }
00205 };
00206 
00207 // Benchmark_rep specialization 
00208 template<>
00209 class Benchmark_rep< CORE::BigRat > {
00210     const CORE::BigRat& t;
00211 public:
00213     Benchmark_rep( const CORE::BigRat& tt) : t(tt) {}
00215     std::ostream& operator()( std::ostream& out) const { 
00216             out << "Rational(" << numerator(t) << "," << denominator(t) << ")";
00217             return out;
00218     }
00219     
00220     static std::string get_benchmark_name() {
00221         return "Rational";
00222     }
00223 };
00224 
00225 CGAL_END_NAMESPACE
00226 
00227 //since types are included by CORE_coercion_traits.h:
00228 #include <CGAL/CORE_Expr.h>
00229 #include <CGAL/CORE_BigInt.h>
00230 #include <CGAL/CORE_BigRat.h>
00231 #include <CGAL/CORE_BigFloat.h>
00232 
00233 #endif // CGAL_CORE_BIGRAT_H
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines