Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r77199 - in sandbox/fixed_point: boost/fixed_point libs/fixed_point/example
From: vicente.botet_at_[hidden]
Date: 2012-03-04 04:23:24


Author: viboes
Date: 2012-03-04 04:23:19 EST (Sun, 04 Mar 2012)
New Revision: 77199
URL: http://svn.boost.org/trac/boost/changeset/77199

Log:
FixedPoint: Added scaling functions
Text files modified:
   sandbox/fixed_point/boost/fixed_point/number.hpp | 74 ++++++++++++++++++++++++++++++++++++---
   sandbox/fixed_point/libs/fixed_point/example/ex_xx.cpp | 59 +++++++++++++++++++++++++++++++
   2 files changed, 127 insertions(+), 6 deletions(-)

Modified: sandbox/fixed_point/boost/fixed_point/number.hpp
==============================================================================
--- sandbox/fixed_point/boost/fixed_point/number.hpp (original)
+++ sandbox/fixed_point/boost/fixed_point/number.hpp 2012-03-04 04:23:19 EST (Sun, 04 Mar 2012)
@@ -1432,6 +1432,37 @@
         value_ = tmp.count();
         return *this;
       }
+
+ // Scaling
+ template <std::size_t N>
+ signed_number<Range+N, Resolution+N, Rounding, Overflow, Optimization>
+ virtual_scale() const
+ {
+ return signed_number<Range+N, Resolution+N, Rounding, Overflow, Optimization>(index(count()));
+ }
+
+ template <std::size_t N>
+ void scale_up()
+ {
+ value_ <<= N;
+ }
+
+ template <int N, typename RP>
+ void scale()
+ {
+ if (N>=0)
+ {
+ value_ <<= N;
+ }
+ else
+ {
+ signed_number tmp=
+ divide<signed_number<Range, Resolution, RP, Overflow, Optimization>>(*this,
+ signed_number<-N+1, -N, Rounding, Overflow, Optimization>(index(1)));
+ value_ = tmp.count();
+ }
+ }
+
     protected:
       underlying_type value_;
     };
@@ -1691,6 +1722,37 @@
         value_ += tmp.count();
         return *this;
       }
+
+ // Scaling
+ template <std::size_t N>
+ unsigned_number<Range+N, Resolution+N, Rounding, Overflow, Optimization>
+ virtual_scale() const
+ {
+ return unsigned_number<Range+N, Resolution+N, Rounding, Overflow, Optimization>(index(count()));
+ }
+
+ template <std::size_t N>
+ void scale_up()
+ {
+ value_ <<= N;
+ }
+
+ template <int N, typename RP>
+ void scale()
+ {
+ if (N>=0)
+ {
+ value_ <<= N;
+ }
+ else
+ {
+ unsigned_number tmp=
+ divide<unsigned_number<Range, Resolution, RP, Overflow, Optimization>>(*this,
+ unsigned_number<-N+1, -N, Rounding, Overflow, Optimization>(index(1)));
+ value_ = tmp.count();
+ }
+ }
+
     protected:
       underlying_type value_;
     };
@@ -2044,9 +2106,9 @@
       typedef Res result_type;
       typedef typename result_type::underlying_type underlying_type;
       typedef typename common_type<unsigned_number<R1,P1,RP1,OP1,Opt1>, unsigned_number<R2,P2,RP2,OP2,Opt2> >::type CT;
- BOOST_STATIC_CONSTEXPR int P = Res::resolution_exp;
+ //BOOST_STATIC_CONSTEXPR int P = Res::resolution_exp;
 
- BOOST_STATIC_ASSERT((Res::digits>=(CT::digits-P)));
+ //BOOST_STATIC_ASSERT((Res::digits>=(CT::digits-P)));
       BOOST_STATIC_ASSERT((Res::is_signed==CT::is_signed));
       BOOST_ASSERT_MSG(CT(rhs).count()!=0, "Division by 0");
 
@@ -2067,9 +2129,9 @@
       typedef Res result_type;
       typedef typename result_type::underlying_type underlying_type;
       typedef typename common_type<signed_number<R1,P1,RP1,OP1,Opt1>, unsigned_number<R2,P2,RP2,OP2,Opt2> >::type CT;
- BOOST_STATIC_CONSTEXPR int P = Res::resolution_exp;
+ //BOOST_STATIC_CONSTEXPR int P = Res::resolution_exp;
 
- BOOST_STATIC_ASSERT((Res::digits>=(CT::digits-P)));
+ //BOOST_STATIC_ASSERT((Res::digits>=(CT::digits-P)));
       BOOST_STATIC_ASSERT((Res::is_signed==CT::is_signed));
       BOOST_ASSERT_MSG(CT(rhs).count()!=0, "Division by 0");
 
@@ -2090,9 +2152,9 @@
       typedef Res result_type;
       typedef typename result_type::underlying_type underlying_type;
       typedef typename common_type<unsigned_number<R1,P1,RP1,OP1,Opt1>, signed_number<R2,P2,RP2,OP2,Opt2> >::type CT;
- BOOST_STATIC_CONSTEXPR int P = Res::resolution_exp;
+ //BOOST_STATIC_CONSTEXPR int P = Res::resolution_exp;
 
- BOOST_STATIC_ASSERT((Res::digits>=(CT::digits-P)));
+ //BOOST_STATIC_ASSERT((Res::digits>=(CT::digits-P)));
       BOOST_STATIC_ASSERT((Res::is_signed==CT::is_signed));
       BOOST_ASSERT_MSG(CT(rhs).count()!=0, "Division by 0");
 

Modified: sandbox/fixed_point/libs/fixed_point/example/ex_xx.cpp
==============================================================================
--- sandbox/fixed_point/libs/fixed_point/example/ex_xx.cpp (original)
+++ sandbox/fixed_point/libs/fixed_point/example/ex_xx.cpp 2012-03-04 04:23:19 EST (Sun, 04 Mar 2012)
@@ -395,6 +395,65 @@
     BOOST_TEST(n3.count()==49);
   }
 
+ // virtual_scale
+ {
+ std::cout << __FILE__ << "[" <<__LINE__<<"]"<<std::endl;
+ signed_number<6,-2> n1((index(7)));
+ signed_number<8,0> n2= n1.virtual_scale<2>();
+ std::cout << int(n2.count()) << std::endl;
+ BOOST_TEST(n1.count()==7);
+ }
+ {
+ std::cout << __FILE__ << "[" <<__LINE__<<"]"<<std::endl;
+ unsigned_number<6,-2> n1((index(7)));
+ unsigned_number<8,0> n2= n1.virtual_scale<2>();
+ std::cout << int(n2.count()) << std::endl;
+ BOOST_TEST(n1.count()==7);
+ }
+ // scale_up
+ {
+ std::cout << __FILE__ << "[" <<__LINE__<<"]"<<std::endl;
+ signed_number<6,-2> n1((index(7)));
+ n1.scale_up<2>();
+ std::cout << int(n1.count()) << std::endl;
+ BOOST_TEST(n1.count()==28);
+ }
+ {
+ std::cout << __FILE__ << "[" <<__LINE__<<"]"<<std::endl;
+ unsigned_number<6,-2> n1((index(7)));
+ n1.scale_up<2>();
+ std::cout << int(n1.count()) << std::endl;
+ BOOST_TEST(n1.count()==28);
+ }
+ // scale
+ {
+ std::cout << __FILE__ << "[" <<__LINE__<<"]"<<std::endl;
+ signed_number<6,-2> n1((index(7)));
+ n1.scale<2,round::truncated>();
+ std::cout << int(n1.count()) << std::endl;
+ BOOST_TEST(n1.count()==28);
+ }
+ {
+ std::cout << __FILE__ << "[" <<__LINE__<<"]"<<std::endl;
+ signed_number<6,-2> n1((index(4)));
+ n1.scale<-2,round::truncated>();
+ std::cout << int(n1.count()) << std::endl;
+ BOOST_TEST(n1.count()==1);
+ }
+ {
+ std::cout << __FILE__ << "[" <<__LINE__<<"]"<<std::endl;
+ unsigned_number<6,-2> n1((index(7)));
+ n1.scale<2,round::truncated>();
+ std::cout << int(n1.count()) << std::endl;
+ BOOST_TEST(n1.count()==28);
+ }
+ {
+ std::cout << __FILE__ << "[" <<__LINE__<<"]"<<std::endl;
+ unsigned_number<6,-2> n1((index(4)));
+ n1.scale<-2,round::truncated>();
+ std::cout << int(n1.count()) << std::endl;
+ BOOST_TEST(n1.count()==1);
+ }
   // *=
   {
     std::cout << __FILE__ << "[" <<__LINE__<<"]"<<std::endl;


Boost-Commit list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk