21 if (not (stream >> type))
22 throw calc_error{
"malformed library, missing number type"};
25 return std::make_shared<number_void>();
29 if (not (stream >> x))
30 throw calc_error{
"malformed library, missing long value"};
31 return std::make_shared<number_long>(x);
34 if (type ==
"double") {
36 if (not (stream >> x))
37 throw calc_error{
"malformed library, missing double value"};
38 return std::make_shared<number_double>(x);
41 throw calc_error{
"malformed library, unknown number type: " + type};
99 return shared_from_this();
104 return shared_from_this();
109 return shared_from_this();
114 return shared_from_this();
122 return std::string{};
148 return shared_from_this();
153 return shared_from_this();
158 return shared_from_this();
163 return shared_from_this();
177 std::ostringstream stream{};
185 stream <<
"long " <<
value() <<
' ';
220 return std::make_shared<number_rational>(
value_, right);
231 return std::make_shared<number_void>();
236 return std::make_shared<number_rational>(
value());
241 return std::make_shared<number_double>(
value());
248 std::ostringstream stream{};
256 stream <<
"rational " <<
value() <<
' ';
274 if (result.denominator() == 1)
275 return std::make_shared<number_long>(result.numerator());
277 return std::make_shared<number_rational>(result);
283 if (result.denominator() == 1)
284 return std::make_shared<number_long>(result.numerator());
286 return std::make_shared<number_rational>(result);
292 if (result.denominator() == 1)
293 return std::make_shared<number_long>(result.numerator());
295 return std::make_shared<number_rational>(result);
301 if (right.numerator() == 0)
305 return std::make_shared<number_long>(result.
numerator());
307 return std::make_shared<number_rational>(result);
318 return std::make_shared<number_void>();
323 return std::make_shared<number_double>(
static_cast<double>(
value()));
331 std::ostringstream stream{};
339 stream <<
"double " <<
value() <<
' ';
374 return std::make_shared<number_double>(
value_ / right);
385 return std::make_shared<number_void>();
virtual bool do_less(number_impl const &rhs) const
virtual void do_save(std::ostream &stream) const
virtual void do_save(std::ostream &stream) const
virtual bool do_less(number_impl const &rhs) const =0
virtual std::shared_ptr< number_impl > do_multiply(number_impl const &rhs)
value_type denominator() const
Return the denominator.
virtual std::shared_ptr< number_impl > do_multiply(number_impl const &rhs)
void save(std::ostream &stream) const
virtual std::shared_ptr< number_impl > do_add(number_impl const &rhs)
value_type numerator() const
Return the numerator.
virtual std::shared_ptr< number_impl > promote_to_double()
virtual std::shared_ptr< number_impl > do_subtract(number_impl const &rhs)=0
virtual std::shared_ptr< number_impl > do_add(number_impl const &rhs)
virtual std::shared_ptr< number_impl > promote_to_void()
bool less(number_impl const &rhs) const
std::shared_ptr< number_impl > promote(number_impl &rhs) const
virtual std::shared_ptr< number_impl > do_subtract(number_impl const &rhs)
virtual bool do_less(number_impl const &rhs) const
virtual std::shared_ptr< number_impl > do_add(number_impl const &rhs)
virtual bool do_equals(number_impl const &rhs) const
virtual std::shared_ptr< number_impl > promote_to_double()
virtual std::shared_ptr< number_impl > do_subtract(number_impl const &rhs)
virtual std::shared_ptr< number_impl > do_divide(number_impl const &rhs)
void print(std::ostream &stream) const
virtual std::shared_ptr< number_impl > promote_to_rational()
std::shared_ptr< number_impl > add(number_impl const &rhs)
virtual void do_save(std::ostream &stream) const
virtual std::shared_ptr< number_impl > do_add(number_impl const &rhs)=0
virtual std::shared_ptr< number_impl > do_promote(number_impl &rhs) const
virtual void do_save(std::ostream &stream) const =0
virtual std::shared_ptr< number_impl > do_promote(number_impl &rhs) const =0
virtual bool do_less(number_impl const &rhs) const
std::shared_ptr< number_impl > subtract(number_impl const &rhs)
virtual std::shared_ptr< number_impl > do_promote(number_impl &rhs) const
virtual std::shared_ptr< number_impl > do_divide(number_impl const &rhs)=0
virtual std::string do_to_string() const
virtual std::shared_ptr< number_impl > do_subtract(number_impl const &rhs)
rational< long long > value() const
virtual bool do_equals(number_impl const &rhs) const
virtual std::string do_to_string() const =0
static std::shared_ptr< number_impl > read_library(std::istream &stream)
virtual std::shared_ptr< number_impl > promote_to_rational()
std::string to_string() const
virtual std::string do_to_string() const
virtual std::shared_ptr< number_impl > do_multiply(number_impl const &rhs)
virtual std::shared_ptr< number_impl > do_multiply(number_impl const &rhs)=0
std::shared_ptr< number_impl > multiply(number_impl const &rhs)
virtual bool do_equals(number_impl const &rhs) const
virtual std::shared_ptr< number_impl > promote_to_void()
virtual std::shared_ptr< number_impl > do_divide(number_impl const &rhs)
virtual std::shared_ptr< number_impl > do_promote(number_impl &rhs) const
virtual std::shared_ptr< number_impl > promote_to_void()
virtual std::shared_ptr< number_impl > do_subtract(number_impl const &rhs)
virtual std::shared_ptr< number_impl > do_divide(number_impl const &rhs)
virtual std::string do_to_string() const
virtual void do_save(std::ostream &stream) const
virtual bool do_less(number_impl const &rhs) const
virtual std::shared_ptr< number_impl > promote_to_void()
virtual std::shared_ptr< number_impl > promote_to_long()
virtual bool do_equals(number_impl const &rhs) const
virtual bool do_equals(number_impl const &rhs) const =0
virtual std::shared_ptr< number_impl > do_add(number_impl const &rhs)
virtual std::string do_to_string() const
virtual std::shared_ptr< number_impl > do_promote(number_impl &rhs) const
bool equals(number_impl const &rhs) const
rational< long long > value_
std::shared_ptr< number_impl > divide(number_impl const &rhs)
virtual std::shared_ptr< number_impl > promote_to_double()
virtual std::shared_ptr< number_impl > do_multiply(number_impl const &rhs)
virtual std::shared_ptr< number_impl > do_divide(number_impl const &rhs)