BWAPI
SPAR/AIModule/BWTA/vendors/CGAL/CGAL/leda_real.h
Go to the documentation of this file.
00001 // Copyright (c) 1999,2007  Utrecht University (The Netherlands),
00002 // ETH Zurich (Switzerland), Freie Universitaet Berlin (Germany),
00003 // INRIA Sophia-Antipolis (France), Martin-Luther-University Halle-Wittenberg
00004 // (Germany), Max-Planck-Institute Saarbruecken (Germany), RISC Linz (Austria),
00005 // and Tel-Aviv University (Israel).  All rights reserved.
00006 //
00007 // This file is part of CGAL (www.cgal.org); you can redistribute it and/or
00008 // modify it under the terms of the GNU Lesser General Public License as
00009 // published by the Free Software Foundation; version 2.1 of the License.
00010 // See the file LICENSE.LGPL distributed with CGAL.
00011 //
00012 // Licensees holding a valid commercial license may use this file in
00013 // accordance with the commercial license agreement provided with the software.
00014 //
00015 // This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
00016 // WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
00017 //
00018 // $URL: svn+ssh://scm.gforge.inria.fr/svn/cgal/branches/CGAL-3.5-branch/Number_types/include/CGAL/leda_real.h $
00019 // $Id: leda_real.h 45636 2008-09-18 15:35:55Z hemmer $
00020 //
00021 //
00022 // Author(s)     : Stefan Schirra, Michael Hemmer
00023 
00024 #ifndef CGAL_LEDA_REAL_H
00025 #define CGAL_LEDA_REAL_H
00026 
00027 #include <CGAL/number_type_basic.h>
00028 
00029 #ifdef CGAL_USE_LEDA
00030 
00031 #include <CGAL/leda_coercion_traits.h>
00032 
00033 #include <CGAL/utils.h>
00034 #include <CGAL/Interval_nt.h>
00035 
00036 #include <utility>
00037 
00038 #include <CGAL/LEDA_basic.h>
00039 #if CGAL_LEDA_VERSION < 500
00040 #include <LEDA/real.h>
00041 #include <LEDA/interval.h>
00042 #else
00043 #include <LEDA/numbers/real.h>
00044 #endif
00045 
00046 
00047 CGAL_BEGIN_NAMESPACE
00048 
00049 template <> class Algebraic_structure_traits< leda_real >
00050 
00051 #if CGAL_LEDA_VERSION >= 500
00052   : public Algebraic_structure_traits_base< leda_real,
00053                                             Field_with_root_of_tag >  {
00054 #else
00055   : public Algebraic_structure_traits_base< leda_real,
00056                                             Field_with_kth_root_tag >  {
00057 #endif
00058 
00059   public:
00060     typedef Tag_true           Is_exact;
00061     typedef Tag_true           Is_numerical_sensitive;
00062 
00063     class Sqrt
00064       : public std::unary_function< Type, Type > {
00065       public:
00066         Type operator()( const Type& x ) const {
00067           return CGAL_LEDA_SCOPE::sqrt( x );
00068         }
00069     };
00070 
00071     class Kth_root
00072       : public std::binary_function<int, Type, Type> {
00073       public:
00074         Type operator()( int k,
00075                                         const Type& x) const {
00076             CGAL_precondition_msg(k > 0, "'k' must be positive for k-th roots");
00077             return CGAL_LEDA_SCOPE::root( x, k);
00078         }
00079     };
00080 
00081 // Root_of is only available for LEDA versions >= 5.0
00082 #if CGAL_LEDA_VERSION >= 500
00083     class Root_of {
00084       public:
00085         typedef Type result_type;
00086 
00087 //        typedef leda_rational Boundary;
00088       private:
00089         template< class ForwardIterator >
00090         inline
00091         CGAL_LEDA_SCOPE::polynomial<Type>
00092         make_polynomial(ForwardIterator begin,
00093                         ForwardIterator end) const {
00094           CGAL_LEDA_SCOPE::growing_array<Type> coeffs;
00095           for(ForwardIterator it = begin; it < end; it++)
00096               coeffs.push_back(*it);
00097           return CGAL_LEDA_SCOPE::polynomial<Type>(coeffs);
00098         }
00099       public:
00100         template <class ForwardIterator>
00101         Type operator()( int k,
00102                        ForwardIterator begin,
00103                        ForwardIterator end) const {
00104             return CGAL_LEDA_SCOPE::diamond(k,make_polynomial(begin,end));
00105         };
00106 /*        template <class ForwardIterator>
00107         Type operator()( leda_rational lower,
00108                                         leda_rational upper,
00109                                         ForwardIterator begin,
00110                                         ForwardIterator end) const {
00111             return CGAL_LEDA_SCOPE::diamond(lower,upper,
00112                                              make_polynomial(begin,end));
00113         };*/
00114     };
00115 
00116 #endif
00117 
00118 
00119 };
00120 
00121 template <> class Real_embeddable_traits< leda_real >
00122   : public INTERN_RET::Real_embeddable_traits_base< leda_real , CGAL::Tag_true > {
00123   public:
00124     class Abs
00125       : public std::unary_function< Type, Type > {
00126       public:
00127         Type operator()( const Type& x ) const {
00128             return CGAL_LEDA_SCOPE::abs( x );
00129         }
00130     };
00131 
00132     class Sgn
00133       : public std::unary_function< Type, ::CGAL::Sign > {
00134       public:
00135         ::CGAL::Sign operator()( const Type& x ) const {
00136           return (::CGAL::Sign) CGAL_LEDA_SCOPE::sign( x );
00137         }
00138     };
00139 
00140     class Compare
00141       : public std::binary_function< Type, Type,
00142                                 Comparison_result > {
00143       public:
00144         Comparison_result operator()( const Type& x,
00145                                             const Type& y ) const {
00146           return (Comparison_result) CGAL_LEDA_SCOPE::compare( x, y );
00147         }
00148 
00149         CGAL_IMPLICIT_INTEROPERABLE_BINARY_OPERATOR_WITH_RT( Type,
00150                                                       Comparison_result )
00151 
00152     };
00153 
00154     class To_double
00155       : public std::unary_function< Type, double > {
00156       public:
00157         double operator()( const Type& x ) const {
00158           // this call is required to get reasonable values for the double
00159           // approximation (as of LEDA-4.3.1)
00160           x.improve_approximation_to(53);
00161           return x.to_double();
00162         }
00163     };
00164 
00165     class To_interval
00166       : public std::unary_function< Type, std::pair< double, double > > {
00167       public:
00168         std::pair<double, double> operator()( const Type& x ) const {
00169 
00170 #if CGAL_LEDA_VERSION >= 501
00171             leda_bigfloat bnum = x.to_bigfloat();
00172             leda_bigfloat berr = x.get_bigfloat_error();
00173 
00174             double dummy;
00175             double low = CGAL_LEDA_SCOPE::sub(bnum, berr, 53, CGAL_LEDA_SCOPE::TO_N_INF).to_double(dummy,
00176                                                      CGAL_LEDA_SCOPE::TO_N_INF);
00177             double upp = CGAL_LEDA_SCOPE::add(bnum, berr, 53, CGAL_LEDA_SCOPE::TO_P_INF).to_double(dummy,
00178                                                      CGAL_LEDA_SCOPE::TO_P_INF);
00179 
00180             std::pair<double, double> result(low, upp);
00181             CGAL_postcondition(Type(result.first)<=x);
00182             CGAL_postcondition(Type(result.second)>=x);
00183             return result;
00184 #else
00185             CGAL_LEDA_SCOPE::interval temp(x); //bug in leda
00186             std::pair<double, double> result(temp.lower_bound(),temp.upper_bound());
00187             CGAL_postcondition_msg(Type(result.first)<=x,
00188                                                     "Known bug in LEDA <=5.0");
00189             CGAL_postcondition_msg(Type(result.first)>=x,
00190                                                     "Known bug in LEDA <=5.0");
00191             return result;
00192             // If x is very small and we look closer at x
00193             // (i.e. comparison or to_double() or to_bigfloat())
00194             // then x gets 0, which is really bad.
00195             // Therefore we do not touch x.
00196             // The LEDA interval above returns (-inf, inf) for
00197             // very small x, which is also bad and leads to
00198             // problems lateron. The postcondition fails in this
00199             // situation.
00200 #endif
00201               // Original CGAL to_interval:
00202             //  Protect_FPU_rounding<true> P (CGAL_FE_TONEAREST);
00203             //  double approx = z.to_double();
00204             //  double rel_error = z.get_double_error();
00205             //  FPU_set_cw(CGAL_FE_UPWARD);
00206             //  Interval_nt_advanced ina(-rel_error,rel_error);
00207             //  ina += 1;
00208             //  ina *= approx;
00209             //  return ina.pair();
00210         }
00211     };
00212 };
00213 
00214 
00215 template <>
00216 class Output_rep< ::leda::real > {
00217     const ::leda::real& t;
00218 public:
00220     Output_rep( const ::leda::real& tt) : t(tt) {}
00222     std::ostream& operator()( std::ostream& out) const {
00223         out << CGAL_NTS to_double(t);
00224         return out;
00225     }
00226 
00227 };
00228 
00229 template <>
00230 class Output_rep< ::leda::real, CGAL::Parens_as_product_tag > {
00231     const ::leda::real& t;
00232 public:
00234     Output_rep( const ::leda::real& tt) : t(tt) {}
00236     std::ostream& operator()( std::ostream& out) const {
00237         if (t<0) out << "(" << ::CGAL::oformat(t)<<")";
00238         else out << ::CGAL::oformat(t);
00239         return out;
00240     }
00241 };
00242 
00243 
00244 
00245 CGAL_END_NAMESPACE
00246 
00247 // Unary + is missing for leda::real
00248 
00249 namespace leda {
00250     inline real operator+( const real& i) { return i; }
00251 } // namespace leda
00252 
00253 //since types are included by leda_coercion_traits.h:
00254 #include <CGAL/leda_integer.h>
00255 #include <CGAL/leda_rational.h>
00256 #include <CGAL/leda_bigfloat.h>
00257 #include <CGAL/leda_real.h>
00258 
00259 #endif // CGAL_USE_LEDA
00260 
00261 #endif // CGAL_LEDA_REAL_H
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines