Skip to content

Commit

Permalink
Number_types: Add Filtered_rational
Browse files Browse the repository at this point in the history
  • Loading branch information
afabri committed Mar 28, 2024
1 parent e4c9cfd commit 25a35e2
Show file tree
Hide file tree
Showing 2 changed files with 170 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@

namespace CGAL {

class Filtered_rational;

template < typename ET >
class Lazy_exact_nt;

Expand Down Expand Up @@ -233,6 +235,7 @@ inline bool fit_in_double(const int& i, double& r) { r = i; return true; }
template < typename ET >
inline bool fit_in_double(const Lazy_exact_nt<ET>&, double&);

inline bool fit_in_double(const Filtered_rational&, double&);

// Auxiliary functor, to get the approximation of a kernel object:
// - for a Point_3 of the Lazy_kernel<...>, one needs to call approx(),
Expand Down
167 changes: 167 additions & 0 deletions Number_types/include/CGAL/Filtered_rational.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
#ifndef CGAL_FILTERED_RATIONAL_H

#include <CGAL/Exact_rational.h>
#include <CGAL/Interval_nt.h>

namespace CGAL {

struct Filtered_rational : boost::totally_ordered1<Filtered_rational
//#ifdef _MSC_VER
, boost::ordered_ring_operators2<Filtered_rational, int
, boost::ordered_ring_operators2<Filtered_rational, double
> >
//#endif
>
{
Interval_nt<> i;
Exact_rational rat;

Filtered_rational()
{}

Filtered_rational(int d)
: i(d), rat(d)
{}

Filtered_rational(double d)
: i(d), rat(d)
{}


Filtered_rational(const Interval_nt<>& i, const Exact_rational& rat)
: i(i), rat(rat)
{}

Filtered_rational operator-() const
{
return Filtered_rational(-i, -rat);
}

Filtered_rational operator+(const Filtered_rational& b) const
{
return Filtered_rational(i + b.i, rat + b.rat);
}


Filtered_rational operator-(const Filtered_rational& b) const
{
return Filtered_rational(i - b.i, rat - b.rat);
}

Filtered_rational operator*(const Filtered_rational& b) const
{
return Filtered_rational(i * b.i, rat * b.rat);
}

Filtered_rational operator/(const Filtered_rational& b) const
{
return Filtered_rational(i / b.i, rat / b.rat);
}



Filtered_rational& operator+=(const Filtered_rational &a) { return *this = *this + a; }
Filtered_rational& operator-=(const Filtered_rational &a) { return *this = *this - a; }
Filtered_rational& operator*=(const Filtered_rational &a) { return *this = *this * a; }
Filtered_rational& operator/=(const Filtered_rational &a) { return *this = *this / a; }

};


template <> class Real_embeddable_traits< Filtered_rational >
: public INTERN_RET::Real_embeddable_traits_base< Filtered_rational, CGAL::Tag_true > {
public:

class Sgn
: public CGAL::cpp98::unary_function< Type, ::CGAL::Sign > {
public:
::CGAL::Sign operator()( const Type& x ) const {
return sign(x);
}
};
};



bool operator==(const Filtered_rational& a,
const Filtered_rational& b)
{
Uncertain<bool> u = a.i == b.i;
if(is_indeterminate(u)){
return a.rat == b.rat;
}
return make_certain(u);
}



bool operator<(const Filtered_rational& a,
const Filtered_rational& b)
{
Uncertain<bool> u = a.i < b.i;
if(is_indeterminate(u)){
return a.rat < b.rat;
}
return make_certain(u);
}

Sign sign(const Filtered_rational& a)
{
Uncertain<Sign> u = sign(a.i);
if(is_indeterminate(u)){
return CGAL::sign(a.rat);
}
return make_certain(u);
}

Sign compare(const Filtered_rational& a, const Filtered_rational& b){
Uncertain<Sign> u = compare(a.i, b.i);
if(is_indeterminate(u)){
return compare(a.rat, b.rat);
}
return make_certain(u);
}

Filtered_rational square(const Filtered_rational& a)
{
return Filtered_rational(square(a.i), square(a.rat));
}

std::pair<double,double> to_interval(const Filtered_rational& b)
{
return std::make_pair(b.i.inf(), b.i.sup());
}

double to_double(const Filtered_rational& b)
{
return to_double(b.i);
}


std::ostream& operator<<(std::ostream& os, const Filtered_rational& )
{
return os;
}

std::istream& operator>>(std::istream& is, const Filtered_rational& )
{
return is;
}

namespace internal {

inline bool fit_in_double(const Filtered_rational& fr, double& d)
{
return fit_in_double(fr.i, d);
}
}
CGAL_DEFINE_COERCION_TRAITS_FOR_SELF(Filtered_rational)
CGAL_DEFINE_COERCION_TRAITS_FROM_TO(short ,Filtered_rational)
CGAL_DEFINE_COERCION_TRAITS_FROM_TO(int ,Filtered_rational)
CGAL_DEFINE_COERCION_TRAITS_FROM_TO(long ,Filtered_rational)
CGAL_DEFINE_COERCION_TRAITS_FROM_TO(float ,Filtered_rational)
CGAL_DEFINE_COERCION_TRAITS_FROM_TO(double ,Filtered_rational)

} // namespace CGAL

#endif

0 comments on commit 25a35e2

Please sign in to comment.