https://svn.lrde.epita.fr/svn/oln/trunk/milena
Index: ChangeLog
from Thierry Geraud <thierry.geraud(a)lrde.epita.fr>
Clean-up value types.
* tests/value_float01_bis.cc: New.
* tests/value_float01_f.cc: Layout.
* tests/value_float01.hh: Remove.
* tests/value_float01.cc: Use mln_assertion.
* tests/value_int_u8.cc: Check size of float01_8.
* mln/accu/median.hh (init): Fix.
* mln/value/internal/value_like.hh (V): Rename as...
(C): ...this.
(V, N): New parameters.
(operator): Remove auto conversion; too risky.
(to_interop): New.
* mln/value/graylevel.hh,
* mln/value/float01_.hh,
* mln/value/int_s.hh,
* mln/value/int_u.hh,
* mln/value/int_u_sat.hh,
* mln/value/float01.hh,
* mln/value/float01_f.hh: Update.
mln/accu/median.hh | 3 -
mln/value/float01.hh | 63 ++++++++++++++----------
mln/value/float01_.hh | 100 ++++++++++++---------------------------
mln/value/float01_f.hh | 42 +++++++---------
mln/value/graylevel.hh | 63 ++++++++----------------
mln/value/int_s.hh | 39 ++++++++-------
mln/value/int_u.hh | 37 +++++++++-----
mln/value/int_u_sat.hh | 44 +++++++++--------
mln/value/internal/value_like.hh | 73 +++++++++++++---------------
tests/value_float01.cc | 2
tests/value_float01_f.cc | 5 -
tests/value_int_u8.cc | 1
12 files changed, 222 insertions(+), 250 deletions(-)
Index: tests/value_float01_f.cc
--- tests/value_float01_f.cc (revision 1271)
+++ tests/value_float01_f.cc (working copy)
@@ -30,10 +30,9 @@
int main()
{
- using typename mln::value::float01_f;
- float01_f x = 0.5;
+ using mln::value::float01_f;
+ float01_f x = 0.5;
std::cout << x + 21.25 << std::endl;
-
x = x + 34;
}
Index: tests/value_float01.cc
--- tests/value_float01.cc (revision 1271)
+++ tests/value_float01.cc (working copy)
@@ -137,7 +137,7 @@
io::pgm::save(lena, "out.pgm");
io::pgm::save(ref, "ref.pgm");
- assert(lena = ref);
+ mln_assertion(lena = ref);
//debug::println(out);
}
Index: tests/value_int_u8.cc
--- tests/value_int_u8.cc (revision 1271)
+++ tests/value_int_u8.cc (working copy)
@@ -40,6 +40,7 @@
using value::int_u8;
using value::float01_8;
+ std::cout << sizeof(float01_8) << std::endl;
{
int_u8 i = 3;
i = 2;
Index: mln/accu/median.hh
--- mln/accu/median.hh (revision 1271)
+++ mln/accu/median.hh (working copy)
@@ -217,7 +217,8 @@
h_.init();
sum_minus_ = 0;
sum_plus_ = 0;
- i_ = (mln_max(argument) - mln_min(argument)) / 2;
+ i_ = (s_.index_of(mln_max(argument))
+ - s_.index_of(mln_min(argument))) / 2;
t_ = s_[i_];
valid_ = true;
}
Index: mln/value/graylevel.hh
--- mln/value/graylevel.hh (revision 1271)
+++ mln/value/graylevel.hh (working copy)
@@ -34,9 +34,7 @@
# include <mln/metal/math.hh>
# include <mln/metal/bexpr.hh>
-# include <mln/value/internal/value_like.hh>
-# include <mln/value/concept/integer.hh>
-# include <mln/value/internal/encoding.hh>
+# include <mln/value/int_u.hh>
# include <mln/value/gray.hh>
# include <mln/value/props.hh>
@@ -53,36 +51,25 @@
/// General gray-level class on n bits.
template <unsigned n>
- class graylevel
- : public Integer< graylevel<n> >,
- public internal::value_like_< typename internal::encoding_unsigned_<n>::ret,
- graylevel<n> >
- {
- protected:
- typedef internal::value_like_< typename
internal::encoding_unsigned_<n>::ret,
- graylevel<n> > like;
-
- public:
-
- /// Encoding associated type.
- typedef typename like::enc enc;
-
+ struct graylevel
+ :
+ public Integer< graylevel<n> >,
+
+ public internal::value_like_< int_u<n>, // Equivalent.
+ mln_enc(int_u<n>), // Encoding.
+ gray, // Interoperation.
+ graylevel<n> > // Exact.
+ {
/// Ctor.
graylevel();
/// Ctor.
- explicit graylevel(const int val);
+ explicit graylevel(int val);
/// Access to std type.
- enc value() const;
-
- /// Op<.
- bool operator<(const graylevel<n>& rhs) const;
+ mln_enc(int_u<n>) value() const;
- graylevel<n>& operator=(const int val);
-
- protected:
- enc val_;
+ graylevel<n>& operator=(int val);
};
@@ -132,37 +119,31 @@
}
template <unsigned n>
- graylevel<n>::graylevel(const int val)
- : val_(val)
+ graylevel<n>::graylevel(int val)
{
mln_precondition(val >= 0);
- mln_precondition(unsigned(val) <= mln_max(enc));
+ mln_precondition(unsigned(val) <= mln_max(mln_enc(int_u<n>)));
+ this->v_ = val;
}
template <unsigned n>
- typename graylevel<n>::enc
+ mln_enc(int_u<n>)
graylevel<n>::value() const
{
- return val_;
+ return this->v_;
}
template <unsigned n>
graylevel<n>&
- graylevel<n>::operator=(const int val)
+ graylevel<n>::operator=(int val)
{
mln_precondition(val >= 0);
- mln_precondition(unsigned(val) <= mln_max(enc));
- this->val_ = val;
+ mln_precondition(unsigned(val) <= mln_max(mln_enc(int_u<n>)));
+ this->v_ = val;
return *this;
}
template <unsigned n>
- bool graylevel<n>::operator<(const graylevel<n>& rhs) const
- {
- return val_ < rhs.val_;
- }
-
- template <unsigned n>
std::ostream& operator<<(std::ostream& ostr, const
graylevel<n>& g)
{
return ostr << g.value();
@@ -174,7 +155,7 @@
return gray(lhs) = gray(rhs);
}
-# endif
+# endif // ! MLN_INCLUDE_ONLY
} // end of namespace mln::value
Index: mln/value/float01_.hh
--- mln/value/float01_.hh (revision 1271)
+++ mln/value/float01_.hh (working copy)
@@ -32,12 +32,14 @@
# include <mln/core/contract.hh>
# include <mln/metal/math.hh>
# include <mln/metal/bexpr.hh>
-# include <mln/value/internal/value_like.hh>
+
+# include <mln/value/int_u.hh>
# include <mln/value/concept/floating.hh>
-# include <mln/value/internal/encoding.hh>
# include <mln/value/float01.hh>
# include <mln/value/props.hh>
+
+
namespace mln
{
@@ -50,44 +52,29 @@
/// General float01-level class on n bits.
template <unsigned n>
- class float01_
- : public Floating< float01_<n> >,
- public internal::value_like_< float,
- float01_<n> >
- {
- public:
+ struct float01_
- /// Encoding associated type.
- typedef typename internal::encoding_unsigned_<n>::ret enc;
+ : public Floating< float01_<n> >,
+ public internal::value_like_< int_u<n>, // Equivalent. // FIXME: Why
not float01?
+ mln_enc(int_u<n>), // Encoding.
+ float, // Interoperation.
+ float01_<n> > // Exact.
+ {
/// Ctor.
float01_();
/// Ctor.
- float01_(const float val);
+ float01_(float val);
/// Access to std type.
float value() const;
void set_ind(unsigned long val);
- enc value_ind() const;
-
- /// Op encoding_t.
operator float() const;
- /// Op float01.
- operator float01() const;
-
- /// Op<.
- bool operator<(const float01_<n>& rhs) const;
-
- float01_<n>& operator=(const float val);
- /// Op=.
- // bool operator=(const float01_<n>& rhs) const;
-
- protected:
- enc val_;
+ float01_<n>& operator=(float val);
};
@@ -106,7 +93,7 @@
static std::size_t index_of_value(const float01_<n>& v)
{
- return v.value_ind();
+ return v.to_enc();
}
};
}
@@ -115,8 +102,8 @@
struct props< float01_<n> >
{
static const std::size_t card_ = metal::pow<2, n>::value;
- static const int min() { return 0; }
- static const int max() { return 1; }
+ static const float min() { return 0.f; }
+ static const float max() { return 1.f; }
static const unsigned nbits = n;
typedef trait::value::kind::data kind;
typedef float sum;
@@ -126,96 +113,71 @@
/// Op<<.
template <unsigned n>
- std::ostream& operator<<(std::ostream& ostr, const
float01_<n>& g);
+ std::ostream& operator<<(std::ostream& ostr, const
float01_<n>& f);
template <unsigned n, unsigned m>
bool approx_equal(const float01_<n>& lhs, const float01_<m>&
rhs);
template <unsigned n>
- bool operator=(const float01_<n>& lhs, const float01_<n>& rhs);
-
- template <unsigned n>
bool approx_equal(const float01_<n>& lhs, const float f);
+
+
# ifndef MLN_INCLUDE_ONLY
// Float01_<n>.
template <unsigned n>
float01_<n>::float01_()
- : val_(0)
{
}
template <unsigned n>
- float01_<n>::float01_(const float val)
- : val_( int(val * (mln_card_(float01_<n>) - 1)) )
+ float01_<n>::float01_(float val)
{
mln_precondition(val >= 0);
mln_precondition(val <= 1);
+ this->v_ = int(val * (mln_card_(float01_<n>) - 1)); // FIXME
}
template <unsigned n>
float
float01_<n>::value() const
{
- return (float(val_) / (mln_card_(float01_<n>) - 1));
+ return float(this->v_) / (mln_card_(float01_<n>) - 1); // FIXME
}
template <unsigned n>
void
float01_<n>::set_ind(unsigned long val)
{
- val_ = val;
- }
-
- template <unsigned n>
- typename float01_<n>::enc
- float01_<n>::value_ind() const
- {
- return (val_);
+ this->v_ = val;
}
template <unsigned n>
float01_<n>&
- float01_<n>::operator=(const float val)
+ float01_<n>::operator=(float val)
{
mln_precondition(val >= 0);
mln_precondition(val <= 1);
- this->val_ = long(val * (mln_card_(float01_<n>) - 1));
+ this->v_ = int(val * (mln_card_(float01_<n>) - 1)); // FIXME
return *this;
}
template <unsigned n>
- float01_<n>::operator float01() const
- {
- float01 tmp(n, val_);
- return tmp;
- }
-
- template <unsigned n>
float01_<n>::operator float() const
{
- return float(val_) / (mln_card_(float01_<n>) - 1);
+ return float(this->v_) / (mln_card_(float01_<n>) - 1);
}
- template <unsigned n>
- bool float01_<n>::operator<(const float01_<n>& rhs) const
- {
- return val_ < rhs.val_;
- }
- template <unsigned n>
- std::ostream& operator<<(std::ostream& ostr, const
float01_<n>& g)
- {
- return ostr << g.value();
- }
+ // Operators.
template <unsigned n>
- bool operator=(const float01_<n>& lhs, const float01_<n>& rhs)
+ std::ostream& operator<<(std::ostream& ostr, const
float01_<n>& f)
{
- return lhs.value_ind() = rhs.value_ind();
+ return ostr << f.value();
}
template <unsigned n, unsigned m>
@@ -225,13 +187,13 @@
}
template <unsigned n>
- bool approx_equal(const float01_<n>& lhs, const float f)
+ bool approx_equal(const float01_<n>& lhs, float f)
{
return float01(lhs) = float01_<n>(f);
}
-# endif
+# endif // ! MLN_INCLUDE_ONLY
} // end of namespace mln::value
Index: mln/value/int_s.hh
--- mln/value/int_s.hh (revision 1271)
+++ mln/value/int_s.hh (working copy)
@@ -96,25 +96,23 @@
*/
template <unsigned n>
struct int_s
- : public Integer< int_s<n> >,
- public internal::value_like_< typename internal::encoding_signed_<n>::ret,
- int_s<n> >
- {
- protected:
- typedef internal::value_like_< typename
internal::encoding_signed_<n>::ret,
- int_s<n> > super;
-
- public:
-
- /// Encoding associated type.
- typedef typename super::enc enc;
+ :
+ public Integer< int_s<n> >,
+ public internal::value_like_< int, // Equivalent.
+ typename internal::encoding_signed_<n>::ret, // Enc.
+ int, // Interoperation.
+ int_s<n> > // Exact.
+ {
/// Constructor without argument.
int_s();
/// Constructor from an integer.
int_s(int i);
+ /// Conversion to an integer.
+ operator int() const;
+
/// Assignment from an integer.
int_s<n>& operator=(int i);
@@ -166,6 +164,7 @@
std::ostream& operator<<(std::ostream& ostr, const int_s<n>&
i);
+
# ifndef MLN_INCLUDE_ONLY
template <unsigned n>
@@ -174,13 +173,19 @@
}
template <unsigned n>
+ int_s<n>::operator int() const
+ {
+ return this->v_;
+ }
+
+ template <unsigned n>
int_s<n>::int_s(int i)
{
static const int max = metal::pow<2, n-1>::value - 1;
static const int min = - max;
mln_precondition(i >= min);
mln_precondition(i <= max);
- this->v_ = enc(i);
+ this->v_ = i;
}
template <unsigned n>
@@ -206,8 +211,8 @@
int_s<n>&
int_s<n>::operator+=(int i)
{
- mln_precondition(long(this->v_) + i >= mln_min(enc));
- mln_precondition(long(this->v_) + i <= mln_max(enc));
+ mln_precondition(long(this->v_) + i >= mln_min(int_s<n>));
+ mln_precondition(long(this->v_) + i <= mln_max(int_s<n>));
this->v_ += i;
return *this;
}
@@ -216,8 +221,8 @@
int_s<n>&
int_s<n>::operator-=(int i)
{
- mln_precondition(long(this->v_) - i >= mln_min(enc));
- mln_precondition(long(this->v_) - i <= mln_max(enc));
+ mln_precondition(long(this->v_) - i >= mln_min(int_s<n>));
+ mln_precondition(long(this->v_) - i <= mln_max(int_s<n>));
this->v_ -= i;
return *this;
}
Index: mln/value/internal/value_like.hh
--- mln/value/internal/value_like.hh (revision 1271)
+++ mln/value/internal/value_like.hh (working copy)
@@ -36,6 +36,7 @@
*/
# include <mln/core/concept/value.hh>
+# include <mln/core/internal/force_exact.hh>
namespace mln
@@ -55,29 +56,32 @@
* Parameters are \c V the equivalent value type and \c E the
* exact value type.
*/
- template <typename V, typename E>
+ template < typename V, // Equivalent.
+ typename C, // Encoding.
+ typename N, // Interoperation.
+ typename E >
struct value_like_ // FIXME :Remove -> : public Value<E>
{
- /// Encoding associated type.
- typedef V enc;
-
/// Equivalent associated type.
typedef V equiv;
- /// Convertion towards equivalent type.
- operator V() const;
+ /// Encoding associated type.
+ typedef C enc;
+
+ /// Interoperation associated type.
+ typedef N interop;
/// Explicit convertion towards equivalent type.
V to_equiv() const;
/// Explicit convertion towards encoding type.
- V to_enc() const;
+ C to_enc() const;
- /// Assignment from equivalent type.
- E& operator=(const V& v);
+ /// Explicit convertion towards interoperation type.
+ N to_interop() const;
protected:
- enc v_; /// The actual value.
+ enc v_; /// The encoding value.
};
@@ -86,8 +90,8 @@
*
* \relates value_like_
*/
- template <typename V, typename E>
- bool operator=(const value_like_<V,E>& lhs, const
value_like_<V,E>& rhs);
+ template <typename V, typename C, typename N, typename E>
+ bool operator=(const value_like_<V,C,N,E>& lhs, const
value_like_<V,C,N,E>& rhs);
/*! \brief General definition of the "less than" operator
@@ -95,52 +99,45 @@
*
* \relates value_like_
*/
- template <typename V, typename E>
- bool operator<(const value_like_<V,E>& lhs, const
value_like_<V,E>& rhs);
+ template <typename V, typename C, typename N, typename E>
+ bool operator<(const value_like_<V,C,N,E>& lhs, const
value_like_<V,C,N,E>& rhs);
# ifndef MLN_INCLUDE_ONLY
- template <typename V, typename E>
- value_like_<V, E>::operator V() const
- {
- return v_;
- }
-
- template <typename V, typename E>
+ template <typename V, typename C, typename N, typename E>
V
- value_like_<V, E>::to_equiv() const
+ value_like_<V,C,N,E>::to_equiv() const
{
return v_;
}
- template <typename V, typename E>
- V
- value_like_<V, E>::to_enc() const
+ template <typename V, typename C, typename N, typename E>
+ C
+ value_like_<V,C,N,E>::to_enc() const
{
return v_;
}
- template <typename V, typename E>
- E&
- value_like_<V, E>::operator=(const V& v)
+ template <typename V, typename C, typename N, typename E>
+ N
+ value_like_<V,C,N,E>::to_interop() const
{
- v_ = v;
- return exact(*this);
+ return mln::internal::force_exact<E>(*this).operator N();
}
- template <typename V, typename E>
- bool operator=(const value_like_<V, E>& lhs,
- const value_like_<V, E>& rhs)
+ template <typename V, typename C, typename N, typename E>
+ bool operator=(const value_like_<V,C,N,E>& lhs,
+ const value_like_<V,C,N,E>& rhs)
{
- return V(lhs) = V(rhs);
+ return lhs.to_interop() = rhs.to_interop();
}
- template <typename V, typename E>
- bool operator<(const value_like_<V, E>& lhs,
- const value_like_<V, E>& rhs)
+ template <typename V, typename C, typename N, typename E>
+ bool operator<(const value_like_<V,C,N,E>& lhs,
+ const value_like_<V,C,N,E>& rhs)
{
- return V(lhs) < V(rhs);
+ return lhs.to_interop() < rhs.to_interop();
}
# endif // ! MLN_INCLUDE_ONLY
Index: mln/value/int_u.hh
--- mln/value/int_u.hh (revision 1271)
+++ mln/value/int_u.hh (working copy)
@@ -100,25 +100,29 @@
*/
template <unsigned n>
struct int_u
- : public Integer< int_u<n> >,
- public internal::value_like_< typename internal::encoding_unsigned_<n>::ret,
- int_u<n> >
+ :
+ public Integer< int_u<n> >,
+
+ public internal::value_like_< unsigned, // Equivalent.
+ typename internal::encoding_unsigned_<n>::ret, // Enc.
+ int, // Interoperation.
+ int_u<n> > // Exact.
{
protected:
- typedef internal::value_like_< typename
internal::encoding_unsigned_<n>::ret,
- int_u<n> > super;
+ /// Encoding associated type.
+ typedef typename internal::encoding_unsigned_<n>::ret enc_;
public:
- /// Encoding associated type.
- typedef typename super::enc enc;
-
/// Constructor without argument.
int_u();
/// Constructor from an integer.
int_u(int i);
+ /// Conversion to an integer.
+ operator int() const;
+
/// Assignment from an integer.
int_u<n>& operator=(int i);
@@ -136,6 +140,7 @@
};
+
// Safety.
template <> struct int_u<0>;
template <> struct int_u<1>;
@@ -178,8 +183,14 @@
int_u<n>::int_u(int i)
{
mln_precondition(i >= 0);
- mln_precondition(unsigned(i) <= mln_max(enc));
- this->v_ = enc(i);
+ mln_precondition(unsigned(i) <= mln_max(enc_));
+ this->v_ = enc_(i);
+ }
+
+ template <unsigned n>
+ int_u<n>::operator int() const
+ {
+ return this->v_;
}
template <unsigned n>
@@ -187,7 +198,7 @@
int_u<n>::operator=(int i)
{
mln_precondition(i >= 0);
- mln_precondition(unsigned(i) <= mln_max(enc));
+ mln_precondition(unsigned(i) <= mln_max(enc_));
this->v_ = i;
return *this;
}
@@ -197,7 +208,7 @@
int_u<n>::operator+=(int i)
{
mln_precondition(long(this->v_) + i >= 0);
- mln_precondition(long(this->v_) + i <= mln_max(enc));
+ mln_precondition(long(this->v_) + i <= mln_max(enc_));
this->v_ += i;
return *this;
}
@@ -207,7 +218,7 @@
int_u<n>::operator-=(int i)
{
mln_precondition(long(this->v_) - i >= 0);
- mln_precondition(long(this->v_) - i <= mln_max(enc));
+ mln_precondition(long(this->v_) - i <= mln_max(enc_));
this->v_ -= i;
return *this;
}
Index: mln/value/int_u_sat.hh
--- mln/value/int_u_sat.hh (revision 1271)
+++ mln/value/int_u_sat.hh (working copy)
@@ -55,25 +55,23 @@
*/
template <unsigned n>
struct int_u_sat
- : public Integer< int_u_sat<n> >,
- public internal::value_like_< typename internal::encoding_unsigned_<n>::ret,
- int_u_sat<n> >
- {
- protected:
- typedef internal::value_like_< typename
internal::encoding_unsigned_<n>::ret,
- int_u_sat<n> > like;
-
- public:
-
- /// Encoding associated type.
- typedef typename like::enc enc;
+ :
+ public Integer< int_u_sat<n> >,
+ public internal::value_like_< int_u<n>, // Equivalent.
+ mln_enc(int_u<n>), // Encoding.
+ int, // Interoperation.
+ int_u_sat<n> > // Exact.
+ {
/// Constructor without argument.
int_u_sat();
/// Constructor from an integer.
int_u_sat(int i);
+ /// Conversion to an integer.
+ operator int() const;
+
/// Assignment from an integer.
int_u_sat<n>& operator=(int i);
@@ -133,11 +131,17 @@
int_u_sat<n>::int_u_sat(int i)
{
if (i < 0)
- this->v_ = enc(0);
- else if (i > mln_max(enc))
- this->v_ = mln_max(enc);
+ this->v_ = 0;
+ else if (i > mln_max(int_u_sat<n>))
+ this->v_ = mln_max(int_u_sat<n>);
else
- this->v_ = enc(i);
+ this->v_ = i;
+ }
+
+ template <unsigned n>
+ int_u_sat<n>::operator int() const
+ {
+ return this->v_;
}
template <unsigned n>
@@ -145,11 +149,11 @@
int_u_sat<n>::operator=(int i)
{
if (i < 0)
- this->v_ = enc(0);
- else if (i > mln_max(enc))
- this->v_ = mln_max(enc);
+ this->v_ = 0;
+ else if (i > mln_max(int_u_sat<n>))
+ this->v_ = mln_max(int_u_sat<n>);
else
- this->v_ = enc(i);
+ this->v_ = i;
return *this;
}
Index: mln/value/float01.hh
--- mln/value/float01.hh (revision 1271)
+++ mln/value/float01.hh (working copy)
@@ -29,6 +29,7 @@
# define MLN_VALUE_FLOAT01_HH
# include <iostream>
+# include <utility>
# include <mln/core/concept/value.hh>
# include <mln/value/float01_.hh>
@@ -38,21 +39,23 @@
namespace mln
{
- // Fwd decls.
namespace value
{
- template <unsigned N> class float01_;
+
+ // Fwd decl.
+ template <unsigned n> class float01_;
class float01;
+
/// General float01_ class where n bits is not know at compile-time.
/// This class is used for exchange between float01_ types purpose.
- class float01 : public Value<float01>
+ class float01 : public Floating<float01>
{
public:
/// Encoding associated type.
- typedef float enc;
+ typedef std::pair<unsigned, unsigned long> enc;
/// Equivalent associated type.
typedef float equiv;
@@ -61,8 +64,8 @@
float01();
/// Ctor.
- template <unsigned N>
- float01(const float01_<N>& val);
+ template <unsigned n>
+ float01(const float01_<n>& val);
/// Ctor.
float01(unsigned nbits, float val);
@@ -73,12 +76,13 @@
unsigned nbits() const;
- void set_nbits(unsigned nbits);
+ float01& set_nbits(unsigned nbits);
+ const float01 to_nbits(unsigned nbits) const;
- float01 to_nbits(unsigned nbits) const;
+ operator float() const;
- template <unsigned N>
- operator float01_<N>() const;
+// template <unsigned n>
+// operator float01_<n>() const;
protected:
unsigned nbits_;
@@ -91,6 +95,7 @@
bool operator<(const float01& lhs, const float01& rhs);
+
# ifndef MLN_INCLUDE_ONLY
namespace internal
@@ -123,17 +128,18 @@
} // end of mln::value::internal
+
// Float01.
float01::float01()
- : nbits_(0)
+ : nbits_(0) // FIXME: Cost at run-time...
{
}
template <unsigned n>
float01::float01(const float01_<n>& g)
: nbits_(n),
- val_(g.value_ind())
+ val_(g.to_enc())
{
}
@@ -160,14 +166,13 @@
return nbits_;
}
-
-
- void float01::set_nbits(unsigned nbits)
+ float01&
+ float01::set_nbits(unsigned nbits)
{
mln_precondition(nbits != 0);
mln_invariant(nbits_ != 0);
if (nbits = nbits_)
- return;
+ return *this;
if (nbits > nbits_)
{
val_ *= internal::two_pow_n_minus_1(nbits);
@@ -178,10 +183,11 @@
val_ /= internal::two_pow_(nbits_ - nbits);
}
nbits_ = nbits;
+ return *this;
}
-
- float01 float01::to_nbits(unsigned nbits) const
+ const float01
+ float01::to_nbits(unsigned nbits) const
{
mln_precondition(nbits != 0);
mln_invariant(nbits_ != 0);
@@ -190,18 +196,25 @@
return tmp;
}
-
- template <unsigned n>
- float01::operator float01_<n>() const
+ float01::operator float() const
{
mln_precondition(nbits_ != 0);
- float01_<n> tmp;
- tmp.set_ind(internal::convert<n>(nbits_, val_));
- mln_assertion(tmp.value() < internal::two_pow_(n));
+ float tmp = float(val_) / internal::two_pow_n_minus_1(nbits_);
return tmp;
}
- // operators
+// template <unsigned n>
+// float01::operator float01_<n>() const
+// {
+// mln_precondition(nbits_ != 0);
+// float01_<n> tmp;
+// tmp.set_ind(internal::convert<n>(nbits_, val_));
+// mln_assertion(tmp.value() < internal::two_pow_(n));
+// return tmp;
+// }
+
+
+ // Operators.
std::ostream& operator<<(std::ostream& ostr, const float01& g)
{
Index: mln/value/float01_f.hh
--- mln/value/float01_f.hh (revision 1271)
+++ mln/value/float01_f.hh (working copy)
@@ -40,22 +40,24 @@
{
/// Fwd decl.
- class float01;
+ struct float01;
/// General float01-level class on n bits.
- class float01_f
- : public Floating< float01_f >,
- public internal::value_like_< float,
- float01_f >
+ struct float01_f
+ :
+ public Floating< float01_f >,
+
+ public internal::value_like_< float, // Equivalent.
+ float, // Encoding.
+ float, // Interoperation.
+ float01_f > // Exact.
{
- public:
-
/// Ctor.
float01_f();
/// Ctor.
- float01_f(const float val);
+ float01_f(float val);
/// Access to std type.
float value() const;
@@ -64,13 +66,9 @@
operator float() const;
float01_f& operator=(const float val);
- /// Op=.
- // bool operator=(const float01_f& rhs) const;
-
- protected:
- float val_;
};
+
template <>
struct props< float01_f >
{
@@ -83,47 +81,47 @@
typedef float interop;
};
+
# ifndef MLN_INCLUDE_ONLY
// Float01_F.
float01_f::float01_f()
- : val_(0)
{
}
- float01_f::float01_f(const float val)
- : val_(val)
+ float01_f::float01_f(float val)
{
mln_precondition(val >= 0);
mln_precondition(val <= 1);
+ this->v_ = val;
}
float
float01_f::value() const
{
- return (val_);
+ return this->v_;
}
float01_f&
- float01_f::operator=(const float val)
+ float01_f::operator=(float val)
{
mln_precondition(val >= 0);
mln_precondition(val <= 1);
- this->val_ = val;
+ this->v_ = val;
return *this;
}
float01_f::operator float() const
{
- return val_;
+ return this->v_;
}
-# endif
-
+# endif // ! MLN_INCLUDE_ONLY
} // end of namespace mln::value
} // end of namespace mln
+
#endif // ! MLN_CORE_VALUE_FLOAT01_F_HH