26 #ifndef MLN_VALUE_FLOAT01__HH
27 # define MLN_VALUE_FLOAT01__HH
35 # include <mln/core/contract.hh>
36 # include <mln/metal/math/pow.hh>
37 # include <mln/metal/bexpr.hh>
39 # include <mln/value/int_u.hh>
40 # include <mln/value/concept/floating.hh>
41 # include <mln/value/internal/convert.hh>
42 # include <mln/value/float01.hh>
43 # include <mln/trait/value_.hh>
53 template <
unsigned n>
struct float01_;
61 struct value_< mln::value::float01_<n> >
66 card = mln_value_card_from_(nbits)
69 typedef trait::value::nature::floating nature;
70 typedef trait::value::kind::data kind;
71 typedef mln_value_quant_from_(card) quant;
73 static
float min() {
return 0.f; }
74 static float max() {
return 1.f; }
75 static float epsilon() {
return 0.f; }
92 :
public Floating< float01_<n> >,
94 public internal::value_like_< float,
113 float01_<n>& operator=(
float val);
119 void set_ind(
unsigned long val);
122 operator float()
const;
125 typedef mln_enc(int_u<n>) enc_;
132 template <
unsigned n>
133 struct convert_< float01_<n> >
135 static float01_<n> value_at_index(
unsigned i)
142 static unsigned index_of_value(
const float01_<n>& v)
151 template <
unsigned n>
152 std::ostream& operator<<(std::ostream& ostr, const float01_<n>& f);
155 template <
unsigned n,
unsigned m>
156 bool approx_equal(
const float01_<n>& lhs,
const float01_<m>& rhs);
158 template <
unsigned n>
159 bool approx_equal(
const float01_<n>& lhs,
const float f);
163 # ifndef MLN_INCLUDE_ONLY
167 template <
unsigned n>
169 float01_<n>::float01_()
173 template <
unsigned n>
175 float01_<n>::float01_(
float val)
177 mln_precondition(val >= 0.f);
178 mln_precondition(val <= 1.f);
179 this->v_ =
static_cast<enc_
>(val * (float(mln_max(enc_)) - 1.f));
182 template <
unsigned n>
185 float01_<n>::value()
const
187 return float(this->v_) / (float(mln_max(enc_)) - 1.f);
190 template <
unsigned n>
193 float01_<n>::set_ind(
unsigned long val)
195 this->v_ =
static_cast<enc_
>(val);
198 template <
unsigned n>
205 template <
unsigned n>
214 template <
unsigned n>
221 template <
unsigned n>
230 template <
unsigned n>
233 float01_<n>::operator=(
float val)
235 mln_precondition(val >= 0.f);
236 mln_precondition(val <= 1.f);
237 this->v_ =
static_cast<enc_
>(val * (float(mln_max(enc_)) - 1.f));
241 template <
unsigned n>
243 float01_<n>::operator float()
const
245 return float(this->v_) / (float(mln_max(enc_)) - 1.f);
251 template <
unsigned n>
253 std::ostream& operator<<(std::ostream& ostr, const float01_<n>& f)
255 return ostr << f.value();
258 template <
unsigned n,
unsigned m>
260 bool approx_equal(
const float01_<n>& lhs,
const float01_<m>& rhs)
265 template <
unsigned n>
267 bool approx_equal(
const float01_<n>& lhs,
float f)
269 return float01(lhs) == float01_<n>(f);
273 # endif // ! MLN_INCLUDE_ONLY
279 #endif // ! MLN_VALUE_FLOAT01__HH