Diff of /src/ov-mp.cc [a5e8fd] .. [6164d0]  Maximize  Restore

  Switch to side-by-side view

--- a/src/ov-mp.cc
+++ b/src/ov-mp.cc
@@ -18,19 +18,21 @@
 */
 
 #include <octave/config.h>
+//#include <octave/gripes.h>
 #include <octave/oct.h>
 #include <octave/ops.h>
 
 #include "ov-mp.h"
 
-/* initialize the static member */
+/* initialize the static members */
 mpfr_prec_t octave_mp::prec = mpfr_get_default_prec ();
+std::map<char, int> octave_mp::rndmap = initmap ();
 
 octave_mp::octave_mp(void)
   :octave_base_value() /*, rnd(mpfr_get_default_rounding_mode ()) */
 {
   /* Bare minimum constructor -- avoid SEGV on exit */
-  (scalar)->_mpfr_d = 0;
+  scalar->_mpfr_d = 0;
   /*
     mpfr_init2 (scalar, prec);
     mpfr_set_si (scalar, 0, rnd);
@@ -44,12 +46,25 @@
   mpfr_set_si (scalar, si, rnd);
  }
 
+octave_mp::octave_mp (signed long si, mpfr_prec_t sprec,  const std::string srnd)
+  :octave_base_value(), rnd(convert_rnd_mode (srnd))
+{ 
+  (sprec == prec) ? mpfr_init (scalar) : mpfr_init2 (scalar, sprec);
+  mpfr_set_si (scalar, si, rnd);
+ }
+
 octave_mp::octave_mp (unsigned long ui, mpfr_prec_t sprec,  mp_rnd_t srnd)
   :octave_base_value(), rnd(srnd)
 { 
   (sprec == prec) ? mpfr_init (scalar) : mpfr_init2 (scalar, sprec);
   mpfr_set_ui (scalar, ui, rnd);
  }
+octave_mp::octave_mp (unsigned long ui, mpfr_prec_t sprec,  const std::string srnd)
+  :octave_base_value(), rnd(convert_rnd_mode (srnd))
+{ 
+  (sprec == prec) ? mpfr_init (scalar) : mpfr_init2 (scalar, sprec);
+  mpfr_set_ui (scalar, ui, rnd);
+ }
 
 octave_mp::octave_mp (const octave_mp& s)
   :octave_base_value(), rnd(s.rnd)
@@ -61,48 +76,160 @@
   :octave_base_value(), rnd(srnd)
 { 
   (sprec == prec) ? mpfr_init (scalar) : mpfr_init2 (scalar, sprec);
-  if (0 == s.compare("pi"))
+  commun_string_parser (s, rnd); 
+} 
+
+octave_mp::octave_mp (const std::string& s, mpfr_prec_t sprec,  const std::string srnd)
+  :octave_base_value(), rnd(convert_rnd_mode (srnd))
+{ 
+  (sprec == prec) ? mpfr_init (scalar) : mpfr_init2 (scalar, sprec);
+  commun_string_parser (s, rnd); 
+} 
+
+/* 
+   Commun code to parse a string, recognizing "pi", "log2", "euler", 
+   and "catalan". Otherwise, just parse it. 
+*/
+void
+octave_mp::commun_string_parser (const std::string src, mp_rnd_t srnd) 
+{
+  if (0 == src.compare("pi"))
     {
       (void) mpfr_const_pi (scalar, rnd);
-    }
-  else
-    {
-      if (0 == s.compare("log2"))
-        (void)  mpfr_const_log2 (scalar, rnd);
-      else
-        (void) mpfr_set_str (scalar, s.c_str(), 0, rnd);
-    }
-} 
+      return;
+    }
+  
+  if (0 == src.compare("log2"))
+    {  
+      (void)  mpfr_const_log2 (scalar, rnd);
+      return;
+    }
+  
+  if (0 == src.compare("euler"))
+    {
+      (void) mpfr_const_euler (scalar, rnd);
+      return;
+    }
+  
+  if (0 == src.compare("catalan"))
+    {  
+      (void)  mpfr_const_catalan (scalar, rnd);
+      return;
+    }
+  
+  (void) mpfr_set_str (scalar, src.c_str(), 0, rnd);
+}
 
 octave_mp::octave_mp (const float& x, mpfr_prec_t sprec,  mp_rnd_t srnd)
   :octave_base_value(), rnd(srnd)
 { 
   (sprec == prec) ? mpfr_init (scalar) : mpfr_init2 (scalar, sprec);
-  double val = x;
-  mpfr_set_d(scalar, val, rnd);
+  mpfr_set_flt (scalar, x, rnd);
+}
+
+octave_mp::octave_mp (const float& x, mpfr_prec_t sprec,  const std::string srnd)
+  :octave_base_value(), rnd(convert_rnd_mode (srnd))
+{ 
+  (sprec == prec) ? mpfr_init (scalar) : mpfr_init2 (scalar, sprec);
+  mpfr_set_flt (scalar, x, rnd);
 }
 
 octave_mp::octave_mp (const double& x, mpfr_prec_t sprec,  mp_rnd_t srnd)
   :octave_base_value(), rnd(srnd)
 { 
   (sprec == prec) ? mpfr_init (scalar) : mpfr_init2 (scalar, sprec);
-  mpfr_set_d(scalar, x, rnd);
+  mpfr_set_d (scalar, x, rnd);
+}
+
+octave_mp::octave_mp (const double& x, mpfr_prec_t sprec, const std::string srnd) 
+  :octave_base_value(), rnd(convert_rnd_mode (srnd))
+{ 
+  (sprec == prec) ? mpfr_init (scalar) : mpfr_init2 (scalar, sprec);
+  mpfr_set_d (scalar, x, rnd);
 }
 
 octave_mp::octave_mp (const octave_value& x, mpfr_prec_t sprec,  mp_rnd_t srnd)
   :octave_base_value(), rnd(srnd)
 { 
   (sprec == prec) ? mpfr_init (scalar) : mpfr_init2 (scalar, sprec);
-  double val = 0;
-  mpfr_set_d(scalar, val, rnd);
-}
-
-octave_mp::octave_mp (const octave_base_value& x, mpfr_prec_t sprec,  mp_rnd_t srnd)
-  :octave_base_value(), rnd(srnd)
-{ 
-  (sprec == prec) ? mpfr_init (scalar) : mpfr_init2 (scalar, sprec);
-  double val = 0;
-  mpfr_set_d(scalar, val, rnd);
+  commun_ov_parser (x, rnd); 
+}
+
+octave_mp::octave_mp (const octave_value& x, mpfr_prec_t sprec,  const std::string srnd)
+  :octave_base_value(),  rnd(convert_rnd_mode (srnd))
+{ 
+  (sprec == prec) ? mpfr_init (scalar) : mpfr_init2 (scalar, sprec);
+  commun_ov_parser (x, rnd); 
+}
+
+void
+octave_mp::commun_ov_parser (const octave_value& x, mp_rnd_t srnd) 
+{
+  if (x.is_string ()) 
+    {
+      std::string s = x.string_value ();
+      commun_string_parser (s, rnd); 
+      return;
+    }
+  if (x.is_real_scalar ())
+    {
+      /*
+        if (x.is_integer_type ())
+        {
+        if (x.is_int8_type ())
+        {
+        long int val = x.int8_scalar_value ();
+        mpfr_set_si (scalar, val, rnd);
+        return;
+        }
+        if (x.is_int16_type ())
+        {
+        long int val = x.int16_scalar_value ();
+        mpfr_set_si (scalar, val, rnd);
+        return;
+        }
+        if (x.is_int32_type ())
+        {
+        long int val = x.int32_scalar_value ();
+        mpfr_set_si (scalar, val, rnd);
+        return;
+        }
+        if (x.is_int64_type ())
+        {
+        long int val = x.int64_scalar_value ();
+        mpfr_set_si (scalar, val, rnd);
+        return;
+        }
+        if (x.is_uint8_type ())
+        {
+        unsigned long int val = x.uint8_scalar_value ();
+        mpfr_set_ui (scalar, val, rnd);
+        return;
+        }
+        if (x.is_uint16_type ())
+        {
+        unsigned long int val = x.uint16_scalar_value ();
+        mpfr_set_ui (scalar, val, rnd);
+        return;
+        }
+        if (x.is_uint32_type ())
+        {
+        unsigned long int val = x.uint32_scalar_value ();
+        mpfr_set_ui (scalar, val, rnd);
+        return;
+        }
+        if (x.is_uint64_type ())
+        {
+        unsigned long int val = x.uint64_scalar_value ();
+        mpfr_set_si (scalar, val, rnd);
+        return;
+        }
+        gripe_unrecognized_data_fmt("Integer");
+        }
+      */
+      double val = x.double_value ();
+      mpfr_set_d(scalar, val, rnd);
+    }
 }
 
 void
@@ -118,9 +245,25 @@
   
   if  (!(mpfr_asprintf (&s,format, scalar) < 0))
     {
-      os << std::string(s) << " with prec " << digits;
+      os << std::string (s) << " with prec " << digits;
+      os << " rounding " <<  std::string (mpfr_print_rnd_mode (rnd));
       mpfr_free_str(s);
     }
+}
+mpfr_rnd_t 
+octave_mp::convert_rnd_mode (const std::string mode)
+{
+  if (mode.substr (0, mode.length () - 1).compare ("MPFR_RND")) 
+    {
+      //gripe_string_invalid (void);
+    }
+  
+  char lastchar = mode[mode.length () - 1];
+  std::map<char, int>::iterator it = rndmap.find (lastchar);
+  
+  if (rndmap.end () == it)
+    return MPFR_RNDN;
+  return (mpfr_rnd_t) it->second;
 }
 
 /* proxy for equal_p, regular_p, ... */
@@ -307,9 +450,9 @@
       SCALAR_MAPPER3 (expm1, expm1);
     case umap_fix:
       {
-        octave_mp* retval = new octave_mp(static_cast<long unsigned int>(0), prec, rnd);
+        octave_mp* retval = new octave_mp (static_cast<long unsigned int>(0), prec, rnd);
         /* explicitelly asks to round towards zero */
-        (void) mpfr_rint(retval->scalar, scalar, MPFR_RNDZ);
+        (void) mpfr_rint (retval->scalar, scalar, MPFR_RNDZ);
         return octave_value (retval);
       }
 /*
@@ -341,9 +484,9 @@
 */
     case umap_roundb:
       {
-        octave_mp* retval = new octave_mp(static_cast<long unsigned int>(0), prec, rnd);
+        octave_mp* retval = new octave_mp (static_cast<long unsigned int>(0), prec, rnd);
         /* explicitelly asks to round towards nearest, prefering even */
-        (void) mpfr_rint(retval->scalar, scalar, MPFR_RNDN);
+        (void) mpfr_rint (retval->scalar, scalar, MPFR_RNDN);
         return octave_value (retval);
       }
 /*

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:





No, thanks