BWAPI
SPAR/AIModule/BWTA/vendors/CGAL/CGAL/Gmpzf.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/Gmpzf.h $
00016 // $Id: Gmpzf.h 50238 2009-06-30 18:59:00Z hemmer $
00017 //
00018 //
00019 // Author(s)     : Michael Hemmer   <hemmer@mpi-inf.mpg.de>
00020 
00021 #ifndef CGAL_GMPZF_H
00022 #define CGAL_GMPZF_H
00023 
00024 // includes
00025 #include <CGAL/number_type_basic.h>
00026 #include <CGAL/Gmp_coercion_traits.h>
00027 #include <CGAL/Gmpz.h>
00028 #include <CGAL/Interval_nt.h>
00029 
00030 CGAL_BEGIN_NAMESPACE
00031 
00032 // Algebraic structure traits
00033 template <> class Algebraic_structure_traits< Gmpzf >
00034     : public Algebraic_structure_traits_base< Gmpzf, Euclidean_ring_tag >  {
00035 public:
00036     typedef Tag_true            Is_exact;
00037 
00038     struct Is_zero
00039         : public std::unary_function< Type, bool > {
00040     public:
00041         bool operator()( const Type& x ) const {
00042             return x.is_zero();
00043         }
00044     };
00045 
00046     struct Integral_division
00047         : public std::binary_function< Type,
00048                                 Type,
00049                                 Type > {
00050     public:
00051         Type operator()(
00052                 const Type& x,
00053                 const Type& y ) const {
00054             return x.integral_division(y);
00055         }
00056     };
00057 
00058     struct Gcd
00059         : public std::binary_function< Type,
00060                                 Type,
00061                                 Type > {
00062     public:
00063         Type operator()(
00064                 const Type& x,
00065                 const Type& y ) const {
00066             return x.gcd(y);
00067         }
00068         CGAL_IMPLICIT_INTEROPERABLE_BINARY_OPERATOR(int)
00069     };
00070 
00071     class Div
00072         : public std::binary_function< Type, Type, Type > {
00073     public:
00074         Type operator()( const Type& x, const Type& y ) const {
00075             return Type(x).div( y );
00076         }
00077     };
00078 
00079     typedef INTERN_AST::Mod_per_operator< Type > Mod;
00080   
00081   class Is_square
00082     : public std::binary_function< Type, Type&, bool > {
00083   public:      
00084     bool operator()( const Type& x, Type& y ) const {
00085       y = CGAL::approximate_sqrt(x);
00086       return y * y == x;
00087     }
00088     bool operator()( const Type& x) const {
00089       Type dummy;
00090       return operator()(x,dummy);
00091     }
00092   };
00093 };
00094 
00095 
00096 // Real embeddable traits
00097 template <>
00098 class Real_embeddable_traits< Gmpzf >
00099     : public INTERN_RET::Real_embeddable_traits_base< Gmpzf , CGAL::Tag_true > {
00100 
00101     typedef Algebraic_structure_traits<Gmpzf> AST;
00102 public:
00103   typedef AST::Is_zero Is_zero;
00104   
00105     struct Sgn
00106         : public std::unary_function< Type, ::CGAL::Sign > {
00107     public:
00108         ::CGAL::Sign operator()( const Type& x ) const {
00109             return x.sign();
00110         }
00111     };
00112 
00113     struct Compare
00114         : public std::binary_function< Type,
00115                                   Type,
00116                                   Comparison_result > {
00117     public:
00118         Comparison_result operator()(
00119                 const Type& x,
00120                 const Type& y ) const {
00121             return x.compare(y);
00122         }
00123     };
00124 
00125     struct To_double
00126         : public std::unary_function< Type, double > {
00127     public:
00128         double operator()( const Type& x ) const {
00129             return x.to_double();
00130         }
00131     };
00132 
00133     struct To_interval
00134         : public std::unary_function< Type, std::pair< double, double > > {
00135     public:
00136         std::pair<double, double> operator()( const Type& x ) const {
00137             return x.to_interval();
00138         }
00139     };
00140 };
00141 
00142 // specialization of to double functor
00143 template<>
00144 class Real_embeddable_traits< Quotient<Gmpzf> >
00145     : public
00146 INTERN_QUOTIENT::Real_embeddable_traits_quotient_base< Quotient<Gmpzf> >
00147 {
00148 public:
00149     struct To_double: public std::unary_function<Quotient<Gmpzf>, double>{
00150         inline
00151         double operator()(const Quotient<Gmpzf>& q) const {
00152           std::pair<double, long> n = q.numerator().to_double_exp();
00153           std::pair<double, long> d = q.denominator().to_double_exp();
00154           double scale = std::ldexp(1.0, n.second - d.second);
00155           return (n.first / d.first) * scale;
00156         }
00157     };
00158     struct To_interval
00159         : public std::unary_function<Quotient<Gmpzf>, std::pair<double,double> >{
00160         inline
00161         std::pair<double,double> operator()(const Quotient<Gmpzf>& q) const {
00162           // do here as MP_Float does
00163           std::pair<std::pair<double, double>, long> n =
00164             q.numerator().to_interval_exp();
00165           std::pair<std::pair<double, double>, long> d =
00166             q.denominator().to_interval_exp();
00167 
00168           CGAL_assertion_msg(CGAL::abs(1.0*n.second - d.second) < (1<<30)*2.0,
00169                      "Exponent overflow in Quotient<MP_Float> to_interval");
00170           return ldexp(Interval_nt<>(n.first) / Interval_nt<>(d.first),
00171                n.second - d.second).pair();
00172         }
00173     };
00174 };
00175 
00176 CGAL_END_NAMESPACE
00177 
00178 //since types are included by Gmp_coercion_traits.h:
00179 #include <CGAL/Gmpz.h>
00180 #include <CGAL/Gmpq.h>
00181 #include <CGAL/Gmpzf.h>
00182 
00183 #endif // CGAL_GMPZF_H
00184 
00185 // ===== EOF ==================================================================
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines