27 #ifndef MLN_VALUE_FLOAT01_HH
28 # define MLN_VALUE_FLOAT01_HH
37 # include <mln/core/concept/value.hh>
38 # include <mln/value/concept/floating.hh>
39 # include <mln/trait/value_.hh>
40 # include <mln/trait/all.hh>
51 template <
unsigned n>
struct float01_;
62 typedef std::pair<unsigned, unsigned long>
enc;
84 unsigned nbits()
const;
94 operator float()
const;
114 # ifndef MLN_INCLUDE_ONLY
120 unsigned long two_pow_(
unsigned n)
125 return 2 * two_pow_(n - 1);
129 unsigned long two_pow_n_minus_1(
unsigned n)
131 return two_pow_(n) - 1;
134 template <
unsigned n_dest>
136 unsigned long convert(
unsigned n_src,
unsigned long val)
142 return val * two_pow_n_minus_1(n_dest) / two_pow_n_minus_1(n_src);
144 return val / two_pow_(n_src - n_dest);
157 template <
unsigned n>
169 val_ =
static_cast<unsigned long>(val *
float(internal::two_pow_n_minus_1(nbits)));
175 mln_invariant(
nbits_ != 0);
182 mln_invariant(
nbits_ != 0);
196 mln_precondition(nbits != 0);
197 mln_invariant(
nbits_ != 0);
202 val_ *= internal::two_pow_n_minus_1(nbits);
217 mln_precondition(nbits != 0);
218 mln_invariant(
nbits_ != 0);
220 tmp.set_nbits(nbits);
225 float01::operator
float()
const
227 mln_precondition(nbits_ != 0);
228 float tmp =
float(val_) /
float(internal::two_pow_n_minus_1(nbits_));
246 std::ostream&
operator<<(std::ostream& ostr,
const float01& g)
248 return ostr << g.value() <<
'/' << g.nbits() <<
"nbits";
254 mln_precondition(lhs.nbits() != 0 && rhs.nbits() != 0);
256 if (rhs.nbits() == lhs.nbits())
257 return lhs.value_ind() == rhs.value_ind();
259 if (lhs.nbits() < rhs.nbits())
260 return lhs.value_ind() == rhs.to_nbits(lhs.nbits()).value_ind();
263 return lhs.to_nbits(rhs.nbits()).value_ind() == rhs.value_ind();
268 bool operator<(
const float01& lhs,
const float01& rhs)
270 mln_precondition(lhs.nbits() != 0 && rhs.nbits() != 0);
271 if (rhs.nbits() == lhs.nbits())
272 return lhs.value() < rhs.value();
273 if (lhs.nbits() > rhs.nbits())
274 return lhs.value() < rhs.to_nbits(lhs.nbits()).
value();
276 return lhs.to_nbits(rhs.nbits()).
value() < rhs.value();
279 # endif // ! MLN_INCLUDE_ONLY
285 # include <mln/value/float01_.hh>
287 #endif // ! MLN_VALUE_FLOAT01_HH