fixed< T, N > Class Template Reference

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>

List of all members.

Public Types

typedef T 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)
 fixed (double value)
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.
fixedoperator+= (fixed f)
fixedoperator-= (fixed f)
fixedoperator *= (fixed f)
 
Parameters:
f The right-hand operand

fixedoperator *= (value_type v)
fixedoperator/= (fixed f)
fixedoperator/= (value_type v)
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 value_type const places = N
 number of decimal places
static value_type const places10 = power10<N>::value
 10places


Detailed Description

template<class T, int N>
class fixed< T, N >

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

Definition at line 26 of file fixed.hpp.


Constructor & Destructor Documentation

template<class T, int N>
fixed< T, N >::fixed ( value_type  integer,
value_type  fraction 
) [inline]

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

Parameters:
integer The integer part of the initial value
fraction The fraction part of the initial value

Definition at line 120 of file fixed.hpp.

References fixed< T, N >::places10.

template<class T, int N>
fixed< T, N >::fixed ( value_type  integer  )  [inline]

Construct from an integer with no fractional part.

Parameters:
integer The initial value

Definition at line 133 of file fixed.hpp.

template<class T, int N>
fixed< T, N >::fixed ( double  value  )  [inline]

Construct by rounding off a floating point number.

Parameters:
value The initial value

Definition at line 50 of file fixed.hpp.


Member Function Documentation

template<class T, int N>
std::string fixed< T, N >::as_string (  )  const [inline]

Convert to a string.

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

Definition at line 231 of file fixed.hpp.

References fixed< T, N >::fraction(), fixed< T, N >::integer(), and fixed< T, N >::places.

template<class T, int N>
template<class Char, class Traits>
bool fixed< T, N >::read ( std::basic_istream< Char, Traits > &  strm  )  [inline]

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

Parameters:
strm the stream to read
Returns:
true for success or false for failure

Definition at line 320 of file fixed.hpp.

References fixed< T, N >::fraction(), fixed< T, N >::integer(), fixed< T, N >::places, and fixed< T, N >::places10.

Referenced by operator>>().

template<class T, int N>
fixed< T, N >::value_type fixed< T, N >::round (  )  const [inline]

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 214 of file fixed.hpp.

References fixed< T, N >::fraction(), fixed< T, N >::integer(), fixed< T, N >::places10, and fixed< T, N >::value().

template<class T, int N>
fixed< T, N > & fixed< T, N >::operator+= ( fixed< T, N >  f  )  [inline]

Addition assignment operator

Parameters:
f The right-hand operand

Definition at line 241 of file fixed.hpp.

References fixed< T, N >::value().

template<class T, int N>
fixed< T, N > & fixed< T, N >::operator-= ( fixed< T, N >  f  )  [inline]

Subtraction assignment operator

Parameters:
f The right-hand operand

Definition at line 248 of file fixed.hpp.

References fixed< T, N >::value().

template<class T, int N>
fixed< T, N > & fixed< T, N >::operator *= ( fixed< T, N >  f  )  [inline]

Parameters:
f The right-hand operand

Multiplication assignment operator

Definition at line 255 of file fixed.hpp.

References fixed< T, N >::places10, and fixed< T, N >::value().

template<class T, int N>
fixed< T, N > & fixed< T, N >::operator *= ( value_type  v  )  [inline]

Multiplication assignment operator

Parameters:
v The right-hand operand

Definition at line 262 of file fixed.hpp.

template<class T, int N>
fixed< T, N > & fixed< T, N >::operator/= ( fixed< T, N >  f  )  [inline]

Division assignment operator

Parameters:
f The right-hand operand

Definition at line 269 of file fixed.hpp.

References fixed< T, N >::places10, and fixed< T, N >::value().

template<class T, int N>
fixed< T, N > & fixed< T, N >::operator/= ( value_type  v  )  [inline]

Division assignment operator

Parameters:
v The right-hand operand

Definition at line 276 of file fixed.hpp.


The documentation for this class was generated from the following file:
Generated on Sun Nov 30 09:53:23 2008 for Exploring C++ - Final Forms of Key Examples by  doxygen 1.5.3