Project 2 - Fixed-Point Number Class
Public Types | Public Member Functions | Static Public Attributes | List of all members
fixed Class Reference

Implement a fixed-point number class. Values have places places after the decimal point. All arithmetic follows the usual rules. More...

#include <fixed.hpp>

Public Types

typedef int value_type
 Type of the actual value.
 

Public Member Functions

 fixed ()
 Default constructor initializes to zero.
 
 fixed (value_type integer, value_type fraction)
 
 fixed (value_type integer)
 Construct from an integer, with no fraction.
 
 fixed (double value)
 Construct by rounding off a floating point number.
 
std::string as_string () const
 
bool read (std::istream &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.
 
fixedoperator+= (fixed f)
 Addition assignment operator.
 
fixedoperator-= (fixed f)
 Subtraction assignment operator.
 
fixedoperator*= (fixed f)
 Multiplication assignment operator.
 
fixedoperator/= (fixed f)
 Division assignment operator.
 
void negate ()
 Negate this value.
 
fixedoperator++ ()
 Pre-increment.
 
fixed operator++ (int)
 Post-increment.
 
fixedoperator-- ()
 Pre-decrement.
 
fixed operator-- (int)
 Post-decrement.
 
value_type value () const
 Return the internal value.
 

Static Public Attributes

static int const places = 4
 number of decimal places
 
static value_type const places10 = 10000
 10places
 

Detailed Description

Implement a fixed-point number class. Values have places places after the decimal point. All arithmetic follows the usual rules.

Definition at line 37 of file fixed.hpp.

Constructor & Destructor Documentation

fixed::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).

Definition at line 14 of file fixed.cpp.

References fraction(), and places10.

Member Function Documentation

std::string fixed::as_string ( ) const

Convert to a string.

Returns
a string representation of the value, e.g., "123.04"

Definition at line 120 of file fixed.cpp.

References fraction(), integer(), and places.

Referenced by operator<<().

bool fixed::read ( std::istream &  strm)

Read from a stream. Overwrite this value with the value read from the stream.

Parameters
strmthe stream to read
Returns
true for success or false for failure

Definition at line 190 of file fixed.cpp.

References fraction(), integer(), places, and places10.

Referenced by operator>>().

fixed::value_type fixed::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 104 of file fixed.cpp.

References fraction(), integer(), places10, and value().


The documentation for this class was generated from the following files: