19 #include "power10.hpp"
25 template<
class T,
int N>
57 template<
class Char,
class Traits>
58 bool read(std::basic_istream<Char, Traits>& strm);
104 template<
class T,
int N>
108 template<
class T,
int N>
112 throw std::invalid_argument(
"negative fraction not allowed");
113 fraction = reduce(fraction);
115 value_ = integer * places10 - fraction;
117 value_ = integer * places10 + fraction;
121 template<
class T,
int N>
124 : value_(integer * places10)
128 template<
class T,
int N>
132 return std::abs(value()) % places10;
144 template<
class T,
int N>
149 while (f >= places10*10 and f % 10 == 0)
154 if (f >= places10*10)
160 while (f >= places10)
172 else if (f >= places10)
178 assert(f < places10);
198 assert(frac < places10);
203 template<
class T,
int N>
208 int adjust(value() < 0 ? -1 : +1);
209 if (frac > places10/2)
210 return integer()+adjust;
211 else if (frac < places10/2)
213 else if (integer() % 2 == 0)
216 return integer()+adjust;
220 template<
class T,
int N>
224 std::ostringstream out;
225 out << integer() <<
'.'
226 << std::setfill(
'0') << std::setw(places) << fraction();
230 template<
class T,
int N>
237 template<
class T,
int N>
244 template<
class T,
int N>
247 value_ = (value_ * f.
value()) / places10;
251 template<
class T,
int N>
254 value_ = (value_ * places10) / f.
value();
258 template<
class T,
int N>
264 template<
class T,
int N>
271 template<
class T,
int N>
279 template<
class T,
int N>
286 template<
class T,
int N>
294 template<
class T,
int N>
295 template<
class Char,
class Traits>
302 if (not (strm >> integer))
304 strm.unsetf(std::ios_base::skipws);
305 if (not (strm >> decimal) or decimal !=
'.')
311 strm.clear(strm.rdstate() & ~strm.failbit);
312 value_ = integer * places10;
322 p != places+1 and strm >> c and std::isdigit(c, strm.getloc());
325 fraction = fraction * 10 + (c -
'0');
328 for (; p < places; ++p)
329 fraction = fraction * 10;
332 while (strm and std::isdigit(c, strm.getloc()))
339 strm.clear(strm.rdstate() & ~strm.failbit);
340 fraction = reduce(fraction);
342 value_ = integer * places10 - fraction;
344 value_ = integer * places10 + fraction;
350 template<
class T,
int N,
class Char,
class Traits>
353 if (not f.
read(strm))
354 strm.setstate(strm.failbit);
359 template<
class T,
int N,
class Char,
class Traits>
360 std::basic_ostream<Char, Traits>& operator<<(std::basic_ostream<Char, Traits>& strm,
fixed<T,N> f)
367 template<
class T,
int N>
375 template<
class T,
int N>
383 template<
class T,
int N>
391 template<
class T,
int N>
399 template<
class T,
int N>
407 template<
class T,
int N>
413 template<
class T,
int N>
416 return a == b.
value();
419 template<
class T,
int N>
422 return a.
value() == b;
426 template<
class T,
int N>
432 template<
class T,
int N>
438 template<
class T,
int N>
445 template<
class T,
int N>
448 return a.
value() < b.value();
451 template<
class T,
int N>
452 bool operator<(T a, fixed<T,N> b)
454 return a < b.value();
457 template<
class T,
int N>
458 bool operator<(fixed<T,N> a, T b)
460 return a.value() < b;
464 template<
class T,
int N>
470 template<
class T,
int N>
476 template<
class T,
int N>
483 template<
class T,
int N>
489 template<
class T,
int N>
490 bool operator<=(T a, fixed<T,N> b)
495 template<
class T,
int N>
496 bool operator<=(fixed<T,N> a, T b)
502 template<
class T,
int N>
508 template<
class T,
int N>
514 template<
class T,
int N>
double as_long_double() const
Convert to long double.
currency< T, N > operator+(currency< T, N > a, currency< T, N > b)
Implement a fixed-point number class template. Values have N places after the decimal point...
bool operator==(currency< T, N > a, currency< T, N > b)
currency< T, N > operator*(currency< T, N > a, T b)
std::string as_string() const
fixed & operator++()
Pre-increment.
currency< T, N > operator/(currency< T, N > a, T b)
float as_float() const
Convert to float.
constexpr fixed()
Default constructor initializes to zero.
Save and restore I/O stream flags.
T value_type
Type of the actual value.
fixed & operator--()
Pre-decrement.
static value_type constexpr places10
10places
bool operator!=(currency< T, N > a, currency< T, N > b)
double as_double() const
Convert to double.
value_type fraction() const
Return the fractional part, e.g., 3 for 12.03.
fixed & operator*=(fixed f)
Multiplication assignment operator.
fixed(double value)
Construct by rounding off a floating point number.
bool operator>=(currency< T, N > a, currency< T, N > b)
fixed & operator-=(fixed f)
Subtraction assignment operator.
fixed & operator/=(fixed f)
Division assignment operator.
value_type integer() const
Return the integer part (which is the same as trunc()).
bool read(std::basic_istream< Char, Traits > &strm)
value_type value() const
Return the internal value.
bool operator>(currency< T, N > a, currency< T, N > b)
void negate()
Negate this value.
std::basic_istream< Char, Traits > & operator>>(std::basic_istream< Char, Traits > &strm, currency< T, N > &c)
static value_type constexpr places
number of decimal places
fixed & operator+=(fixed f)
Addition assignment operator.
currency< T, N > operator-(currency< T, N > a)
Negate a currency value.