Calculator Step 6

- Main Page
- Classes
- Files

- Class List
- Class Index
- Class Hierarchy
- Class Members

Public Member Functions | Private Member Functions | Private Attributes | List of all members
number Class Reference

`#include <number.hpp>`

## Public Member Functions
| number ()
| number (int x)
| number (long x)
| number (long long x)
| number (double x)
| number (rational< long long > const &x)
| number (number const &)=default
| number (number &&)=default
number & | operator= (number const &)=default
number & | operator= (number &&)=default
| ~number ()=default
| number (std::istream &stream)
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 (std::shared_ptr< number_impl > ptr)

## Private Attributes
std::shared_ptr< 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 16 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/().

8 : pimpl_{std::make_shared<number_void>()}
9 {}
number::pimpl_
std::shared_ptr< number_impl > pimpl_
Definition: number.hpp:129

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

Construct an `int` value. For the sake of simplicitly, all integers are stored as `long` `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.

12 : pimpl_{std::make_shared<number_long>(x)}
13 {}
number::pimpl_
std::shared_ptr< number_impl > pimpl_
Definition: number.hpp:129

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

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

Parameters

  x | The value of the number

Definition at line 15 of file number.cpp.

16 : pimpl_{std::make_shared<number_long>(x)}
17 {}
number::pimpl_
std::shared_ptr< number_impl > pimpl_
Definition: number.hpp:129

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

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

Parameters

  x | The value of the number

Definition at line 19 of file number.cpp.

20 : pimpl_{std::make_shared<number_long>(x)}
21 {}
number::pimpl_
std::shared_ptr< number_impl > pimpl_
Definition: number.hpp:129

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 27 of file number.cpp.

28 : pimpl_{std::make_shared<number_double>(x)}
29 {}
number::pimpl_
std::shared_ptr< number_impl > pimpl_
Definition: number.hpp:129

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

Construct a rational number.

Parameters

  x | The value of the number

Definition at line 23 of file number.cpp.

24 : pimpl_{std::make_shared<number_rational>(x)}
25 {}
number::pimpl_
std::shared_ptr< number_impl > pimpl_
Definition: number.hpp:129

number::number | ( | number const & |  | ) |
| default

number::number | ( | number && |  | ) |
| default

number::~number | ( |  | ) |
| default

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

Construct a number by reading from a library file.

Definition at line 31 of file number.cpp.

References number_impl::read_library().

32 : pimpl_{number_impl::read_library(stream)}
33 {}
number::pimpl_
std::shared_ptr< number_impl > pimpl_
Definition: number.hpp:129
number_impl::read_library
static std::shared_ptr< number_impl > read_library(std::istream &stream)
Definition: number_impl.cpp:18

number::number | ( | std::shared_ptr< number_impl > | ptr | ) |
| private

Private constructor to create a number from a number_impl.

Parameters

  ptr | Pointer to a number_impl.

Definition at line 35 of file number.cpp.

36 : pimpl_{std::move(pimpl)}
37 {}
number::pimpl_
std::shared_ptr< number_impl > pimpl_
Definition: number.hpp:129

## Member Function Documentation

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 56 of file number.cpp.

References number(), and pimpl_.

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

57 {
58 // Promote that number to the type of this.
59 that = number(this->pimpl_->promote(*that.pimpl_));
60 // Promote this to the type of that.
61 *this = number(that.pimpl_->promote(*this->pimpl_));
62 }
number::pimpl_
std::shared_ptr< number_impl > pimpl_
Definition: number.hpp:129
number::number
number()
Definition: number.cpp:7

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 64 of file number.cpp.

References coerce(), and pimpl_.

Referenced by operator==().

65 {
66 coerce(b);
67 return pimpl_->equals(*b.pimpl_);
68 }
number::pimpl_
std::shared_ptr< number_impl > pimpl_
Definition: number.hpp:129
number::coerce
void coerce(number &rhs)
Definition: number.cpp:56

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 70 of file number.cpp.

References coerce(), and pimpl_.

Referenced by operator<().

71 {
72 coerce(b);
73 return pimpl_->less(*b.pimpl_);
74 }
number::pimpl_
std::shared_ptr< number_impl > pimpl_
Definition: number.hpp:129
number::coerce
void coerce(number &rhs)
Definition: number.cpp:56

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 88 of file number.cpp.

References coerce(), number(), and pimpl_.

89 {
90 coerce(rhs);
91 return number(pimpl_->multiply(*rhs.pimpl_));
92 }
number::pimpl_
std::shared_ptr< number_impl > pimpl_
Definition: number.hpp:129
number::number
number()
Definition: number.cpp:7
number::coerce
void coerce(number &rhs)
Definition: number.cpp:56

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 76 of file number.cpp.

References coerce(), number(), and pimpl_.

77 {
78 coerce(rhs);
79 return number(pimpl_->add(*rhs.pimpl_));
80 }
number::pimpl_
std::shared_ptr< number_impl > pimpl_
Definition: number.hpp:129
number::number
number()
Definition: number.cpp:7
number::coerce
void coerce(number &rhs)
Definition: number.cpp:56

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 82 of file number.cpp.

References coerce(), number(), and pimpl_.

83 {
84 coerce(rhs);
85 return number(pimpl_->subtract(*rhs.pimpl_));
86 }
number::pimpl_
std::shared_ptr< number_impl > pimpl_
Definition: number.hpp:129
number::number
number()
Definition: number.cpp:7
number::coerce
void coerce(number &rhs)
Definition: number.cpp:56

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 94 of file number.cpp.

References coerce(), number(), and pimpl_.

95 {
96 coerce(rhs);
97 return number(pimpl_->divide(*rhs.pimpl_));
98 }
number::pimpl_
std::shared_ptr< number_impl > pimpl_
Definition: number.hpp:129
number::number
number()
Definition: number.cpp:7
number::coerce
void coerce(number &rhs)
Definition: number.cpp:56

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

number& number::operator= | ( | number && |  | ) |
| default

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

Print a number to a stream.

Parameters

  stream | The output stream

Definition at line 51 of file number.cpp.

References pimpl_.

Referenced by operator<<().

52 {
53 pimpl_->print(stream);
54 }
number::pimpl_
std::shared_ptr< number_impl > pimpl_
Definition: number.hpp:129

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

Save a number to a library file.

Parameters

  stream | the library stream

Definition at line 39 of file number.cpp.

References pimpl_.

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

41 {
42 pimpl_->save(stream);
43 }
number::pimpl_
std::shared_ptr< number_impl > pimpl_
Definition: number.hpp:129

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

Convert this number to a human-readable string.

Definition at line 45 of file number.cpp.

References pimpl_.

Referenced by BOOST_AUTO_TEST_CASE().

47 {
48 return pimpl_->to_string();
49 }
number::pimpl_
std::shared_ptr< number_impl > pimpl_
Definition: number.hpp:129

## Member Data Documentation

std::shared_ptr<number_impl> number::pimpl_
| private

Definition at line 129 of file number.hpp.

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

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

- number.hpp
- number.cpp