commit-gnuradio
[Top][All Lists]
Advanced

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

[Commit-gnuradio] [gnuradio] 01/16: pmt: Added float conversion


From: git
Subject: [Commit-gnuradio] [gnuradio] 01/16: pmt: Added float conversion
Date: Fri, 19 Sep 2014 19:07:58 +0000 (UTC)

This is an automated email from the git hooks/post-receive script.

jcorgan pushed a commit to branch master
in repository gnuradio.

commit 0cbebe1b8f1fb23fd14630deddc9b0e08ec493b3
Author: Martin Braun <address@hidden>
Date:   Thu Sep 4 11:32:16 2014 +0200

    pmt: Added float conversion
    
    Added to_float() and from_float(). These are basically aliases
    for *_double() with a type cast for when strict typing is necessary
    (e.g. SWIG won't accept an f32 value when using from_double()).
---
 gnuradio-runtime/include/pmt/pmt.h       | 10 ++++++++++
 gnuradio-runtime/lib/pmt/pmt.cc          | 12 ++++++++++++
 gnuradio-runtime/lib/pmt/qa_pmt_prims.cc |  9 +++++++++
 gnuradio-runtime/python/pmt/qa_pmt.py    |  2 ++
 gnuradio-runtime/swig/pmt_swig.i         |  2 ++
 5 files changed, 35 insertions(+)

diff --git a/gnuradio-runtime/include/pmt/pmt.h 
b/gnuradio-runtime/include/pmt/pmt.h
index 3e17571..cb6fdf4 100644
--- a/gnuradio-runtime/include/pmt/pmt.h
+++ b/gnuradio-runtime/include/pmt/pmt.h
@@ -201,6 +201,7 @@ PMT_API bool is_real(pmt_t obj);
 
 //! Return the pmt value that represents double \p x.
 PMT_API pmt_t from_double(double x);
+PMT_API pmt_t from_float(float x);
 
 /*!
  * \brief Convert pmt to double if possible.
@@ -211,6 +212,15 @@ PMT_API pmt_t from_double(double x);
  */
 PMT_API double to_double(pmt_t x);
 
+/*!
+ * \brief Convert pmt to float if possible.
+ *
+ * This basically is to_double() with a type-cast; the PMT stores
+ * the value as a double in any case. Use this when strict typing
+ * is required.
+ */
+PMT_API float to_float(pmt_t x);
+
 /*
  * ------------------------------------------------------------------------
  *                            Complex
diff --git a/gnuradio-runtime/lib/pmt/pmt.cc b/gnuradio-runtime/lib/pmt/pmt.cc
index 082b98a..da830e1 100644
--- a/gnuradio-runtime/lib/pmt/pmt.cc
+++ b/gnuradio-runtime/lib/pmt/pmt.cc
@@ -368,6 +368,12 @@ from_double(double x)
   return pmt_t(new pmt_real(x));
 }
 
+pmt_t
+from_float(float x)
+{
+  return pmt_t(new pmt_real(x));
+}
+
 double
 to_double(pmt_t x)
 {
@@ -379,6 +385,12 @@ to_double(pmt_t x)
   throw wrong_type("pmt_to_double", x);
 }
 
+float
+to_float(pmt_t x)
+{
+  return float(to_double(x));
+}
+
 ////////////////////////////////////////////////////////////////////////////
 //                              Complex
 ////////////////////////////////////////////////////////////////////////////
diff --git a/gnuradio-runtime/lib/pmt/qa_pmt_prims.cc 
b/gnuradio-runtime/lib/pmt/qa_pmt_prims.cc
index 2f46b01..2b3ca32 100644
--- a/gnuradio-runtime/lib/pmt/qa_pmt_prims.cc
+++ b/gnuradio-runtime/lib/pmt/qa_pmt_prims.cc
@@ -125,6 +125,15 @@ qa_pmt_prims::test_reals()
   CPPUNIT_ASSERT_EQUAL(-1.0, pmt::to_double(m1));
   CPPUNIT_ASSERT_EQUAL(1.0, pmt::to_double(p1));
   CPPUNIT_ASSERT_EQUAL(1.0, pmt::to_double(pmt::from_long(1)));
+
+  pmt::pmt_t p2 = pmt::from_float(1);
+  pmt::pmt_t m2 = pmt::from_float(-1);
+  CPPUNIT_ASSERT(pmt::is_real(p2));
+  CPPUNIT_ASSERT(pmt::is_real(m2));
+  CPPUNIT_ASSERT_THROW(pmt::to_float(pmt::PMT_T), pmt::wrong_type);
+  CPPUNIT_ASSERT_EQUAL(float(-1.0), pmt::to_float(m2));
+  CPPUNIT_ASSERT_EQUAL(float(1.0), pmt::to_float(p2));
+  CPPUNIT_ASSERT_EQUAL(float(1.0), pmt::to_float(pmt::from_long(1)));
 }
 
 void
diff --git a/gnuradio-runtime/python/pmt/qa_pmt.py 
b/gnuradio-runtime/python/pmt/qa_pmt.py
index 5c1af2c..32cff62 100755
--- a/gnuradio-runtime/python/pmt/qa_pmt.py
+++ b/gnuradio-runtime/python/pmt/qa_pmt.py
@@ -36,7 +36,9 @@ class test_pmt(unittest.TestCase):
         const = 123765
         x_pmt = pmt.from_double(const)
         x_int = pmt.to_double(x_pmt)
+        x_float = pmt.to_float(x_pmt)
         self.assertEqual(x_int, const)
+        self.assertEqual(x_float, const)
 
     def test03(self):
         v = pmt.init_f32vector(3, [11, -22, 33])
diff --git a/gnuradio-runtime/swig/pmt_swig.i b/gnuradio-runtime/swig/pmt_swig.i
index e54b544..c4b6782 100644
--- a/gnuradio-runtime/swig/pmt_swig.i
+++ b/gnuradio-runtime/swig/pmt_swig.i
@@ -111,6 +111,8 @@ namespace pmt{
   bool is_real(pmt_t obj);
   pmt_t from_double(double x);
   double to_double(pmt_t x);
+  pmt_t from_float(double x);
+  double to_float(pmt_t x);
 
   bool is_complex(pmt_t obj);
   pmt_t make_rectangular(double re, double im);



reply via email to

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