// Rascal, the Advanced Scientific CALculator
// Copyright (C) 2001, Sebastian Ritterbusch (Rascal@Ritterbusch.de)
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more detauls.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
//

using cxsc::stagprec;

string output(const cxscl_real & a)
{
   if(rinterval_precision2>=0)
   {
      string s;
      s << a;
      return "LReal("+s+")";
   } else
   {
      int p2=rinterval_precision2;
      int p1=rinterval_precision1;
      rinterval_precision(p1,0);
      string b;
      b << a;
      string beststring=b;
      l_real bestdist=abs(stringl_real(b)-a);
      int p3;
      for(p3=1;p3<=-p2;p3++)
      {
         rinterval_precision(p1,p3);
         b="";
         b << a;
         l_real now=abs(stringl_real(b)-a);

         if(now<bestdist)
         {
            beststring=b;
            bestdist=now;
         }
         if(now==0)
            break;
      }
      rinterval_precision(p1,p2);
      return "LReal("+beststring+")";
   }
}

cxscl_real stringl_real(const string & a)
{
   cxscl_real b;
   a>>b;
   return b;
}

string   setStagprec(int newValue)
{
   stagprec=newValue;
   return "Staggered arithmetics precision changed";
}


syntax highlighted by Code2HTML, v. 0.9.1