Gmpfr: fixed const-ref passing for constructor

This commit is contained in:
Luis Peñaranda 2012-06-07 19:51:12 +00:00
parent 1e0e574d6b
commit 7f08b8bd7a
3 changed files with 25 additions and 11 deletions

View File

@ -73,7 +73,7 @@ This type is \ccc{ImplicitInteroperable} with \ccc{Gmpz}, \verb-long-,
\ccConstructor{Gmpfr(const Gmpzf &zf);} \ccConstructor{Gmpfr(const Gmpzf &zf);}
{Creates a \ccc{Gmpfr}, initialized with the value of \ccc{zf}.} {Creates a \ccc{Gmpfr}, initialized with the value of \ccc{zf}.}
\ccConstructor{Gmpfr(std::pair<Gmpz,long> ie);} \ccConstructor{Gmpfr(const std::pair<Gmpz,long> &ie);}
{Creates a \ccc{Gmpfr}, initialized with the value of {Creates a \ccc{Gmpfr}, initialized with the value of
\( ie.first \times 2^{ie.second} \) .} \( ie.first \times 2^{ie.second} \) .}
@ -331,8 +331,8 @@ the compared numbers is \ccc{NaN}, the \ccc{erange} flag is set.
\ccMethod{bool is_square(const Gmpfr &y);} \ccMethod{bool is_square(const Gmpfr &y);}
{Returns \ccc{true} iff \ccVar~is the square of a number {Returns \ccc{true} iff \ccVar~is the square of a number
representable by an object of this type, calculating it and storing representable by an object of this type, computing and storing it
it in \ccc{y}.} in \ccc{y}.}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
@ -348,7 +348,7 @@ the compared numbers is \ccc{NaN}, the \ccc{erange} flag is set.
\ccFunction{std::ostream& operator<<(std::ostream& out, const Gmpfr& f);} \ccFunction{std::ostream& operator<<(std::ostream& out, const Gmpfr& f);}
{If the ostream \ccc{out} is in pretty-print mode, writes a decimal {If the ostream \ccc{out} is in pretty-print mode, writes a decimal
approximatin of \ccc{f} to \ccc{out}. Otherwise, writes \ccc{f} to approximation of \ccc{f} to \ccc{out}. Otherwise, writes \ccc{f} to
\ccc{out} in the form \(MeE\), where \(M\) is its mantissa and \ccc{out} in the form \(MeE\), where \(M\) is its mantissa and
\(E\) is its exponent, both in base 10.} \(E\) is its exponent, both in base 10.}

View File

@ -255,7 +255,7 @@ class Gmpfr:
} }
} }
Gmpfr(Gmpzf f, Gmpfr(const Gmpzf &f,
std::float_round_style r, std::float_round_style r,
Gmpfr::Precision_type p=Gmpfr::get_default_precision()){ Gmpfr::Precision_type p=Gmpfr::get_default_precision()){
CGAL_assertion(p>=MPFR_PREC_MIN&&p<=MPFR_PREC_MAX); CGAL_assertion(p>=MPFR_PREC_MIN&&p<=MPFR_PREC_MAX);
@ -264,7 +264,7 @@ class Gmpfr:
mpfr_mul_2si(fr(),fr(),f.exp(),_gmp_rnd(r)); mpfr_mul_2si(fr(),fr(),f.exp(),_gmp_rnd(r));
} }
Gmpfr(Gmpzf f,Gmpfr::Precision_type p){ Gmpfr(const Gmpzf &f,Gmpfr::Precision_type p){
CGAL_assertion(p>=MPFR_PREC_MIN&&p<=MPFR_PREC_MAX); CGAL_assertion(p>=MPFR_PREC_MIN&&p<=MPFR_PREC_MAX);
mpfr_init2(fr(),p); mpfr_init2(fr(),p);
mpfr_set_z(fr(),f.man(),mpfr_get_default_rounding_mode()); mpfr_set_z(fr(),f.man(),mpfr_get_default_rounding_mode());
@ -274,7 +274,7 @@ class Gmpfr:
mpfr_get_default_rounding_mode()); mpfr_get_default_rounding_mode());
} }
Gmpfr(Gmpzf f){ Gmpfr(const Gmpzf &f){
mpfr_init2(fr(), mpfr_init2(fr(),
static_cast<Gmpfr::Precision_type>( static_cast<Gmpfr::Precision_type>(
mpz_sizeinbase(f.man(),2)<MPFR_PREC_MIN? mpz_sizeinbase(f.man(),2)<MPFR_PREC_MIN?
@ -288,7 +288,7 @@ class Gmpfr:
CGAL_assertion_msg(inexact==0,"inexact conversion from Gmpzf"); CGAL_assertion_msg(inexact==0,"inexact conversion from Gmpzf");
} }
Gmpfr(std::pair<Gmpz,long> intexp, Gmpfr(const std::pair<Gmpz,long> &intexp,
std::float_round_style r=Gmpfr::get_default_rndmode(), std::float_round_style r=Gmpfr::get_default_rndmode(),
Gmpfr::Precision_type p=Gmpfr::get_default_precision()){ Gmpfr::Precision_type p=Gmpfr::get_default_precision()){
CGAL_assertion(p>=MPFR_PREC_MIN&&p<=MPFR_PREC_MAX); CGAL_assertion(p>=MPFR_PREC_MIN&&p<=MPFR_PREC_MAX);
@ -297,7 +297,7 @@ class Gmpfr:
mpfr_mul_2si(fr(),fr(),intexp.second,_gmp_rnd(r)); mpfr_mul_2si(fr(),fr(),intexp.second,_gmp_rnd(r));
} }
Gmpfr(std::pair<Gmpz,long> intexp,Gmpfr::Precision_type p){ Gmpfr(const std::pair<Gmpz,long> &intexp,Gmpfr::Precision_type p){
CGAL_assertion(p>=MPFR_PREC_MIN&&p<=MPFR_PREC_MAX); CGAL_assertion(p>=MPFR_PREC_MIN&&p<=MPFR_PREC_MAX);
mpfr_init2(fr(),p); mpfr_init2(fr(),p);
mpfr_set_z(fr(), mpfr_set_z(fr(),

View File

@ -116,15 +116,26 @@ int test_to_integer_exp(CGAL::Gmpfr f){
} }
} }
// This function checks equality between an _NT x and a Gmpfr y.
template<class _NT> template<class _NT>
int test_constructors(_NT x){ int are_different(const _NT &x,const CGAL::Gmpfr &y){
return x!=y;
}
template<>
int are_different(const std::pair<CGAL::Gmpz,long> &x,const CGAL::Gmpfr &y){
return(mpfr_cmp_si_2exp(y.fr(),mpz_get_si(x.first.mpz()),x.second));
}
template<class _NT>
int test_constructors(const _NT &x){
typedef CGAL::Gmpfr Gmpfr; typedef CGAL::Gmpfr Gmpfr;
typedef _NT NT; typedef _NT NT;
bool fail=false; bool fail=false;
Gmpfr::set_default_precision(70); Gmpfr::set_default_precision(70);
Gmpfr f(x); Gmpfr f(x);
// this conversion should be exact // this conversion should be exact
if(f!=x){ if(are_different(x,f)){
std::cerr<<"failed default construction! (inexact)"<<std::endl; std::cerr<<"failed default construction! (inexact)"<<std::endl;
fail=true; fail=true;
} }
@ -173,6 +184,9 @@ int main(){
_TEST("constructors Gmpz", _TEST("constructors Gmpz",
test_constructors<CGAL::Gmpz>((CGAL::Gmpz(1)<<1000)+CGAL::Gmpz(1));) test_constructors<CGAL::Gmpz>((CGAL::Gmpz(1)<<1000)+CGAL::Gmpz(1));)
_TEST("constructors Gmpzf",test_constructors<CGAL::Gmpzf>(1025);) _TEST("constructors Gmpzf",test_constructors<CGAL::Gmpzf>(1025);)
typedef std::pair<CGAL::Gmpz,long> MantExp;
_TEST("constructors pair<Gmpz,long>",
test_constructors<MantExp>(std::make_pair(CGAL::Gmpz(4096),35));)
_TEST("operators Gmpfr",test_operators<NT>();) _TEST("operators Gmpfr",test_operators<NT>();)
_TEST("operators Gmpzf",test_operators<CGAL::Gmpzf>();) _TEST("operators Gmpzf",test_operators<CGAL::Gmpzf>();)