BWAPI
|
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/Gmpq.h $ 00016 // $Id: Gmpq.h 47264 2008-12-08 06:25:14Z hemmer $ 00017 // 00018 // 00019 // Author(s) : Michael Hemmer <hemmer@mpi-inf.mpg.de> 00020 00021 #ifndef CGAL_GMPQ_H 00022 #define CGAL_GMPQ_H 00023 00024 #include <CGAL/number_type_basic.h> 00025 #include <CGAL/Gmpz.h> 00026 #include <CGAL/Gmp_coercion_traits.h> 00027 00028 CGAL_BEGIN_NAMESPACE 00029 00030 // AST for Gmpq-class 00031 template <> class Algebraic_structure_traits< Gmpq > 00032 : public Algebraic_structure_traits_base< Gmpq, Field_tag > { 00033 public: 00034 typedef Tag_true Is_exact; 00035 typedef Tag_false Is_numerical_sensitive; 00036 00037 class Is_square 00038 : public std::binary_function< Type, Type&, 00039 bool > { 00040 public: 00041 bool operator()( const Type& x_, Type& y ) const { 00042 Gmpq x( x_ ); 00043 mpq_canonicalize( x.mpq() ); 00044 Algebraic_structure_traits< Gmpz >::Sqrt sqrt; 00045 y = Gmpq( sqrt( x.numerator() ), sqrt( x.denominator() ) ); 00046 return y*y == x; 00047 } 00048 bool operator()( const Type& x) const { 00049 Type y; 00050 return operator()(x,y); 00051 } 00052 00053 }; 00054 00055 class Simplify 00056 : public std::unary_function< Type&, void > { 00057 public: 00058 void operator()( Type& x) const { 00059 mpq_canonicalize( x.mpq() ); 00060 } 00061 }; 00062 00063 }; 00064 00065 // RET for Gmpq-class 00066 00067 template <> class Real_embeddable_traits< Gmpq > 00068 : public INTERN_RET::Real_embeddable_traits_base< Gmpq , CGAL::Tag_true > { 00069 public: 00070 00071 class Sgn 00072 : public std::unary_function< Type, ::CGAL::Sign > { 00073 public: 00074 ::CGAL::Sign operator()( const Type& x ) const { 00075 return x.sign(); 00076 } 00077 }; 00078 00079 class To_double 00080 : public std::unary_function< Type, double > { 00081 public: 00082 double operator()( const Type& x ) const { 00083 return x.to_double(); 00084 } 00085 }; 00086 00087 class To_interval 00088 : public std::unary_function< Type, std::pair< double, double > > { 00089 public: 00090 std::pair<double, double> operator()( const Type& x ) const { 00091 mpfr_t y; 00092 mpfr_init2 (y, 53); /* Assume IEEE-754 */ 00093 mpfr_set_q (y, x.mpq(), GMP_RNDD); 00094 double i = mpfr_get_d (y, GMP_RNDD); /* EXACT but can overflow */ 00095 mpfr_set_q (y, x.mpq(), GMP_RNDU); 00096 double s = mpfr_get_d (y, GMP_RNDU); /* EXACT but can overflow */ 00097 mpfr_clear (y); 00098 return std::pair<double, double>(i, s); 00099 } 00100 }; 00101 }; 00102 00106 template <> 00107 class Fraction_traits< Gmpq > { 00108 public: 00109 typedef Gmpq Type; 00110 typedef ::CGAL::Tag_true Is_fraction; 00111 typedef Gmpz Numerator_type; 00112 typedef Gmpz Denominator_type; 00113 typedef Algebraic_structure_traits< Gmpz >::Gcd Common_factor; 00114 class Decompose { 00115 public: 00116 typedef Gmpq first_argument_type; 00117 typedef Gmpz& second_argument_type; 00118 typedef Gmpz& third_argument_type; 00119 void operator () (const Gmpq& rat, Gmpz& num,Gmpz& den) { 00120 num = rat.numerator(); 00121 den = rat.denominator(); 00122 } 00123 }; 00124 class Compose { 00125 public: 00126 typedef Gmpz first_argument_type; 00127 typedef Gmpz second_argument_type; 00128 typedef Gmpq result_type; 00129 Gmpq operator () (const Gmpz& num,const Gmpz& den) { 00130 return Gmpq(num, den); 00131 } 00132 }; 00133 }; 00134 00135 CGAL_END_NAMESPACE 00136 00137 //since types are included by Gmp_coercion_traits.h: 00138 #include <CGAL/Gmpz.h> 00139 #include <CGAL/Gmpq.h> 00140 #include <CGAL/Gmpzf.h> 00141 00142 #endif // CGAL_GMPQ_H