bug-gmp
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: bugs with C++ and gmp-4.0


From: Nathalie Revol
Subject: Re: bugs with C++ and gmp-4.0
Date: Sun, 16 Dec 2001 13:26:50 +0100 (MET)


> Strange.  Please post the test program that provokes this.

The original one was far too long. Here is a shorter one with the same problem:

pb-gmp4.0.H: def of the C++ corresponding to a MPFR object
pb-gmp4.0.C: minimal number of constructors, a destructor and the I/O
operators.

Compiled using:
g++ pb-gmp4.0.C -o pb-gmp4.0 -I../../GNUC/gmp-4.0 -I../../GNUC/mpfr \           
-L../../GNUC/gmp-4.0/.libs -L../../GNUC/mpfr -lmpfr -lm

Error message:
In file included from pb-gmp4.0.H:22,
                 from pb-gmp4.0.C:5:
../../GNUC/gmp-4.0/gmp.h:1961: declaration of C function `class ostream & 
operator <<(ostream &, const __mpq_struct *)' conflicts with
../../GNUC/gmp-4.0/gmp.h:1960: previous declaration `class ostream & operator 
<<(ostream &, const __mpz_struct *)' here
../../GNUC/gmp-4.0/gmp.h:1962: declaration of C function `class ostream & 
operator <<(ostream &, const __mpf_struct *)' conflicts with
../../GNUC/gmp-4.0/gmp.h:1961: previous declaration `class ostream & operator 
<<(ostream &, const __mpq_struct *)' here
../../GNUC/gmp-4.0/gmp.h:1964: declaration of C function `class istream & 
operator >>(istream &, __mpq_struct *)' conflicts with
../../GNUC/gmp-4.0/gmp.h:1963: previous declaration `class istream & operator 
>>(istream &, __mpz_struct *)' here
../../GNUC/gmp-4.0/gmp.h:1965: declaration of C function `class istream & 
operator >>(istream &, __mpf_struct *)' conflicts with
../../GNUC/gmp-4.0/gmp.h:1964: previous declaration `class istream & operator 
>>(istream &, __mpq_struct *)' here
make: *** [pb-gmp4.0] Error 1


Thanks for your help
        Nathalie

---------------------------------------------------------------------------

     Nathalie REVOL
     LIP - Projet INRIA Arenaire           tel : (33) 4-72-72-86-42
     Ecole Normale Superieure de Lyon      Fax : (33) 4-72-72-80-80
     69364 Lyon Cedex 07                   address@hidden
     FRANCE                                http://www.ens-lyon.fr/~nrevol

---------------------------------------------------------------------------

//
// File pb-gmp4.0.H
// NR 14-12-01
//

#ifndef _MPFR_REAL_H_
#define _MPFR_REAL_H_

//#include <stdio.h>

// To be able to use the C++ string
using namespace std;
#ifdef __GNUC__
#include <string>
#define String string
#else
//#include <string_iso_SUNWCC.h>
#endif

#include <iostream.h>
#include <math.h>

extern "C" {
#include "gmp.h"
#include "gmp-impl.h"
#include "mpfr.h"
#include "mpfr-impl.h"
}

#ifndef _GIVARO_REF_COUNTER_H_
#define _GIVARO_REF_COUNTER_H_
// ==================================================================== //
// Definition of the Counter class, Counter
// (c) copyright GIVARO 1994                                            
// author: Th. Gautier                                                
// This class definition objects to handle reference
// counter for memory allocation (eg array0). 
// ==================================================================== //
#include <stddef.h>

class RefCounter {
public:
   // Cstor and Dstor
inline RefCounter( long l = 0) : counter(l) {} 
//inline RefCounter( const RefCounter& ) : counter(C.counter) {} 
inline ~RefCounter() {}

  //  Return the value
inline long  getvalue() const { return counter ; } 
inline long  val() const { return counter ; }
  // Return a ref to the counter
inline long& refvalue() { return counter ; }
  // Increments the counter and returns the new value 
inline long  incr() { return ++counter ; }
  // Decrements the value and returns the new value 
inline long  decr() { return --counter ; }

protected:
  long counter ;
} ;

#endif



// Rounding modes
//typedef int RoundingModes;
typedef mp_rnd_t RoundingModes;
static const RoundingModes RoundUp=int(GMP_RNDU); 
static const RoundingModes RoundDown=int(GMP_RNDD); 
static const RoundingModes RoundNearest=int(GMP_RNDN); 
static const RoundingModes RoundToZero=int(GMP_RNDZ);


class MpfrReal {

  protected:
    mpfr_t mpfr_rep;    // representation of the real in a mpfr format
    int mpfr_flag;      // flag inexact or infinity or NaN...
    RefCounter *nbref;
    // Precision
    typedef mp_prec_t PrecisionType;
    //typedef long unsigned int PrecisionType;
    static PrecisionType &CurrPrecision;
    // Rounding mode
    static RoundingModes& CurrRndMode;

  public:
    // constructors and destructors
    MpfrReal ();
    MpfrReal (double d, 
              RoundingModes rnd = CurrRndMode, 
              PrecisionType prec = CurrPrecision);
    ~MpfrReal ();
 
    // Input/Output
    ostream& put(ostream& o,
                 RoundingModes rnd = CurrRndMode,
                 PrecisionType prec = CurrPrecision,
                 int base = 10,
                 int nb_digits = 0) const;
                 //PrecisionType prec = PrecisionType(0) ) const;
    friend istream& operator >> (istream &i, MpfrReal& r);
    friend ostream& operator << (ostream &o, const MpfrReal& r);

};


#endif          // _MPFR_REAL_H_ 

//
// End of file pb-gmp4.0.H
//

//
// File pb-gmp4.o.C
// 

#include "pb-gmp4.0.H"

MpfrReal::PrecisionType &MpfrReal::CurrPrecision = 
__mpfr_default_fp_bit_precision;
RoundingModes &MpfrReal::CurrRndMode = __gmp_default_rounding_mode;



//--------------------------------------------------------------
//
// Constructors and destructors
//
//--------------------------------------------------------------

MpfrReal::MpfrReal ()
  {
  mpfr_init2(mpfr_rep, CurrPrecision);
  nbref = new RefCounter(0);
  // mpfr_flag is not initialized
  }

MpfrReal::MpfrReal (double d, 
                    RoundingModes rnd, 
                    PrecisionType prec)
  {
  mpfr_init2(mpfr_rep, prec);
  mpfr_flag=mpfr_set_d(mpfr_rep, d, rnd);
  nbref=new RefCounter(1);
  }

  
MpfrReal::~MpfrReal ()
  {
  if (nbref->decr() <= 0)
    {
    mpfr_clear(mpfr_rep);
    //delete nbref;
    }
  }


//--------------------------------------------------------------
//
// Input Output
//
//--------------------------------------------------------------

// Outputs the value on the stream o in base 'base' with 'nb_digits'
// with rounding mode rnd
ostream& MpfrReal::put(ostream& o, 
        RoundingModes rnd, PrecisionType prec,
        int base, int nb_digits) const
  {
  int neg = (mpfr_sgn(mpfr_rep)<0) ? 1 : 0;

  if (mpfr_nan_p(mpfr_rep))
    { o << "NaN"; return o; }
  if (mpfr_inf_p(mpfr_rep)) {
    if ( neg )
      o << "-";
    o << "Inf";
    return o;
    }
  if ( MPFR_IS_ZERO(mpfr_rep) )
    { 
    if ( neg )
      o << "-";
    o << "0"; 
    return o; 
    }

  char *s, *fraction, first_digit;
  mp_exp_t e;
  int size_s;
  s = mpfr_get_str(NULL, &e, base, nb_digits, mpfr_rep, rnd);
  size_s = strlen(s);
  // to print the floating point
  if (s[0] == '-')
    {
    o << s[0];
    first_digit = s[1];
    fraction = &s[2];
    }
  else
    {
    first_digit = s[0];
    fraction = &s[1];
    }
  o << first_digit << "." << fraction;
  (*__gmp_free_func)(s, size_s);
  if (--e)
    {
    o << "E" << e ;
    }
  return o;
  }
  
ostream& operator << (ostream& o, const MpfrReal& r)
  {
  return r.put(o);
  }

istream& operator >> (istream& i, MpfrReal& r)
// Mimicking mpfr_set_str and Integer::>> of Givaro
  {
  if (!i.good())
    { 
    cerr << " Pb reading on the input stream " << endl;
    // maybe throw an exception...
    }
  if (!i)
    return i;

  // read white spaces
  i >> ws;

  // reading the string, the rounding is done by mpfr_set_str
  int noend = 1, nread=0;
  size_t alloc_size=100;
  char c, *str=(char *)(*__gmp_allocate_func)(alloc_size);

  // read the characters on i until a white space is read
  while (noend)
    {
    // read until alloc_size char are read, or a white space is encountered
    while ( (noend) && (nread < alloc_size) )
      {
      i.get(c);
      if (i.eof())
        { noend=0; }
      else if (isspace(c))
        { noend=0; i.putback(c); }
      else
        { str[nread++] = c; }
      }

    if (nread >= alloc_size)
      {
      size_t old_alloc_size = alloc_size;
      alloc_size = alloc_size * 3 / 2;
      str = (char *)(*__gmp_reallocate_func)(str, old_alloc_size, alloc_size);
      }
    }

  str[nread] = '\0';

  if ( mpfr_set_str(r.mpfr_rep, str, 10, MpfrReal::CurrRndMode) )
    {
    cerr << " Pb reading on the input stream " << endl;
    // maybe throw an exception...
    }

  return i;
  }


void main()
{
MpfrReal x = MpfrReal(1.2);

cout << x;
}

//
// End of file pb-gmp4.o.C
// 


reply via email to

[Prev in Thread] Current Thread [Next in Thread]