Project 3 - Currency Type
|
Implement a fixed-point number class template. Values have N
places after the decimal point. All arithmetic follows the usual rules.
More...
#include <fixed.hpp>
Public Types | |
typedef T | value_type |
Type of the actual value. | |
Public Member Functions | |
constexpr | fixed () |
Default constructor initializes to zero. | |
fixed (value_type integer, value_type fraction) | |
constexpr | fixed (value_type integer) |
Construct from an integer with no fractional part. | |
fixed (double value) | |
Construct by rounding off a floating point number. | |
std::string | as_string () const |
template<class Char , class Traits > | |
bool | read (std::basic_istream< Char, Traits > &strm) |
double | as_long_double () const |
Convert to long double. | |
double | as_double () const |
Convert to double. | |
float | as_float () const |
Convert to float. | |
value_type | round () const |
value_type | integer () const |
Return the integer part (which is the same as trunc()). | |
value_type | fraction () const |
Return the fractional part, e.g., 3 for 12.03. | |
fixed & | operator+= (fixed f) |
Addition assignment operator. | |
fixed & | operator-= (fixed f) |
Subtraction assignment operator. | |
fixed & | operator*= (fixed f) |
Multiplication assignment operator. | |
fixed & | operator/= (fixed f) |
Division assignment operator. | |
void | negate () |
Negate this value. | |
fixed & | operator++ () |
Pre-increment. | |
fixed | operator++ (int) |
Post-increment. | |
fixed & | operator-- () |
Pre-decrement. | |
fixed | operator-- (int) |
Post-decrement. | |
value_type | value () const |
Return the internal value. | |
Static Public Attributes | |
static value_type constexpr | places {N} |
number of decimal places | |
static value_type constexpr | places10 {power10(N)} |
10places | |
Implement a fixed-point number class template. Values have N
places after the decimal point. All arithmetic follows the usual rules.
Listing 49-5. Changing fixed from a Class to a Class Template
fixed< T, N >::fixed | ( | value_type | integer, |
value_type | fraction | ||
) |
Construct from separate integer and fractional parts, e.g., initialize to 123.45 with fixed(123, 45). Initialize to 12.07 with fixed(12, 7).
std::string fixed< T, N >::as_string | ( | ) | const |
Convert to a string.
Definition at line 221 of file fixed.hpp.
Referenced by operator<<().
bool fixed< T, N >::read | ( | std::basic_istream< Char, Traits > & | strm | ) |
Read from a stream. Overwrite this value with the value read from the stream.
strm | the stream to read |
Definition at line 296 of file fixed.hpp.
Referenced by operator>>().
fixed< T, N >::value_type fixed< T, N >::round | ( | ) | const |
Return just the integer part, rounded off to the nearest integer. If the value lies equidistant between two integers, round even numbers up and odd numbers down (banker's rounding).
Definition at line 204 of file fixed.hpp.
Referenced by currency< T, N >::round().