- Main Page
- Classes
- Files

- Alphabetical List
- Class List
- Class Hierarchy
- Class Members

# number Class Reference

`#include <number.hpp>`

List of all members.

## Public Member Functions
| number ()
| number (int x)
| number (long x)
| number (double x)
| number (rational< long > const &x)
| number (number const &n)
number & | operator= (number const &n)
| number (std::istream &stream)
| ~number ()
void | save (std::ostream &stream) const
bool | equals (number b)
bool | less (number b)
void | coerce (number &rhs)
std::string | to_string () const
void | print (std::ostream &stream)
number | operator+ (number rhs)
number | operator- (number rhs)
number | operator * (number rhs)
number | operator/ (number rhs)
## Private Member Functions
| number (number_impl *ptr)
## Private Attributes
number_impl * | pimpl_

## Detailed Description

Number class represents a numeric value. Numbers of different types are supported, as well as conversions between types.

Definition at line 15 of file number.hpp.

## Constructor & Destructor Documentation

number::number | ( |  | ) |

Construct a void value. The calculator does not currently use void, but I can't think of a better value for a default number constructor.

Definition at line 7 of file number.cpp.

Referenced by coerce(), operator *(), operator+(), operator-(), and operator/().

    00008 : pimpl_(new number_void())
    00009 {}

number::number | ( | int | x | ) |

Construct an `int` value. For the sake of simplicitly, all integers are stored as `long` values. This overloaded operator exists so simple uses of, say, number(0), are not ambiguous.

Parameters:

  | x | The value of the number

Definition at line 11 of file number.cpp.

    00012 : pimpl_(new number_long(x))
    00013 {}

number::number | ( | long | x | ) |

Construct a `long` value. For the sake of simplicitly, all integers are stored as `long` values.

Parameters:

  | x | The value of the number

Definition at line 15 of file number.cpp.

    00016 : pimpl_(new number_long(x))
    00017 {}

number::number | ( | double | x | ) |

Construct a floating-point value. For the sake of simplicitly, all floating-point numbers are stored as `double` values.

Parameters:

  | x | The value of the number

Definition at line 23 of file number.cpp.

    00024 : pimpl_(new number_double(x))
    00025 {}

number::number | ( | rational< long > const & | x | ) |

Construct a rational number.

Parameters:

  | x | The value of the number

Definition at line 19 of file number.cpp.

    00020 : pimpl_(new number_rational(x))
    00021 {}

number::number | ( | number const & | n | ) |

Copy constructor. The actual number_impl is not copied; only the reference count is incremented.

Parameters:

  | n | The number to copy

Definition at line 27 of file number.cpp.

References number_impl::add_ref(), and pimpl_.

    00028 : pimpl_(n.pimpl_)
    00029 {
    00030   pimpl_->add_ref();
    00031 }

number::number | ( | std::istream & | stream | ) |

Construct a number by reading from a library file.

Definition at line 33 of file number.cpp.

    00034 : pimpl_(number_impl::read_library(stream))
    00035 {}

number::~number | ( |  | ) |

Destructor decrements the reference count.

Definition at line 41 of file number.cpp.

References number_impl::del_ref(), and pimpl_.

    00042 {
    00043   pimpl_->del_ref();
    00044 }

number::number | ( | number_impl * | ptr | ) | `[private]`

Private constructor to create a number from a number_impl.

Parameters:

  | ptr | Pointer to a number_impl.

Definition at line 37 of file number.cpp.

    00038 : pimpl_(pimpl)
    00039 {}

## Member Function Documentation

number & number::operator= | ( | number const & | n | ) |

Copy assignment operator. The actual number_impl is not copied; only the reference count is incremented.

Parameters:

  | n | The number to copy

Definition at line 46 of file number.cpp.

References number_impl::add_ref(), number_impl::del_ref(), and pimpl_.

    00047 {
    00048   that.pimpl_->add_ref();
    00049   this->pimpl_->del_ref();
    00050   this->pimpl_ = that.pimpl_;
    00051   return *this;
    00052 }

void number::save | ( | std::ostream & | stream | ) | const

Save a number to a library file.

Parameters:

  | stream | the library stream

Definition at line 54 of file number.cpp.

References pimpl_, and number_impl::save().

Referenced by BOOST_AUTO_TEST_CASE(), and node_number::save_node().

    00056 {
    00057   pimpl_->save(stream);
    00058 }

bool number::equals | ( | number | b | ) |

Test for equality. Coerce `this` and `b` to the same type, then compare.

Parameters:

  | b | the number to compare

Returns:
  true if `*this` is numerically equal to `b`

Definition at line 79 of file number.cpp.

References coerce(), number_impl::equals(), and pimpl_.

Referenced by operator==().

    00080 {
    00081   coerce(b);
    00082   return pimpl_->equals(*b.pimpl_);
    00083 }

bool number::less | ( | number | b | ) |

Test for less-than. Coerce `this` and `b` to the same type, then compare.

Parameters:

  | b | the number to compare

Returns:
  true if `*this` is numerically less than `b`

Definition at line 85 of file number.cpp.

References coerce(), number_impl::less(), and pimpl_.

Referenced by operator<().

    00086 {
    00087   coerce(b);
    00088   return pimpl_->less(*b.pimpl_);
    00089 }

void number::coerce | ( | number & | rhs | ) |

Coerce `this` and `rhs` to the same arithmetic type. Promote the "smaller" number to the "larger" type.

- number_long promotes to number_double
- everything promotes to void
  Parameters:

    `[in,out]` | rhs | the right-hand-side operand

Definition at line 71 of file number.cpp.

References number(), pimpl_, and number_impl::promote().

Referenced by equals(), less(), operator *(), operator+(), operator-(), and operator/().

    00072 {
    00073   // Promote that number to the type of this.
    00074   that = number(this->pimpl_->promote(*that.pimpl_));
    00075   // Promote this to the type of that.
    00076   *this = number(that.pimpl_->promote(*this->pimpl_));
    00077 }

std::string number::to_string | ( |  | ) | const

Convert this number to a human-readable string.

Definition at line 60 of file number.cpp.

References pimpl_, and number_impl::to_string().

Referenced by BOOST_AUTO_TEST_CASE().

    00062 {
    00063   return pimpl_->to_string();
    00064 }

void number::print | ( | std::ostream & | stream | ) |

Print a number to a stream.

Parameters:

  | stream | The output stream

Definition at line 66 of file number.cpp.

References pimpl_, and number_impl::print().

Referenced by operator<<().

    00067 {
    00068   pimpl_->print(stream);
    00069 }

number number::operator+ | ( | number | rhs | ) |

Addition operator. Coerce `this` and `rhs` to the same type, then add `rhs` and `this`.

Parameters:

  | rhs | The right-hand-side operand

Returns:
  `*this` + `rhs`

Definition at line 91 of file number.cpp.

References number_impl::add(), coerce(), number(), and pimpl_.

    00092 {
    00093   coerce(rhs);
    00094   return number(pimpl_->add(*rhs.pimpl_));
    00095 }

number number::operator- | ( | number | rhs | ) |

Subtraction operator. Coerce `this` and `rhs` to the same type, then subtract `rhs` from `this`.

Parameters:

  | rhs | The right-hand-side operand

Returns:
  `*this` - `rhs`

Definition at line 97 of file number.cpp.

References coerce(), number(), pimpl_, and number_impl::subtract().

    00098 {
    00099   coerce(rhs);
    00100   return number(pimpl_->subtract(*rhs.pimpl_));
    00101 }

number number::operator * | ( | number | rhs | ) |

Multiplication operator. Coerce `this` and `rhs` to the same type, then multiply `this` by `rhs`.

Parameters:

  | rhs | The right-hand-side operand

Returns:
  `*this` * `rhs`

Definition at line 103 of file number.cpp.

References coerce(), number_impl::multiply(), number(), and pimpl_.

    00104 {
    00105   coerce(rhs);
    00106   return number(pimpl_->multiply(*rhs.pimpl_));
    00107 }

number number::operator/ | ( | number | rhs | ) |

Division operator. Coerce `this` and `rhs` to the same type, then divide `this` by `rhs`.

Parameters:

  | rhs | The right-hand-side operand

Returns:
  `*this` / `rhs`

Exceptions:

  | calc_error | if `rhs` == 0.

Definition at line 109 of file number.cpp.

References coerce(), number_impl::divide(), number(), and pimpl_.

    00110 {
    00111   coerce(rhs);
    00112   return number(pimpl_->divide(*rhs.pimpl_));
    00113 }

## Member Data Documentation

number_impl* number::pimpl_`[private]`

Definition at line 129 of file number.hpp.

Referenced by coerce(), equals(), less(), number(), operator *(), operator+(), operator-(), operator/(), operator=(), print(), save(), to_string(), and ~number().

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

- number.hpp
- number.cpp