Theoretica
A C++ numerical and automatic mathematical library
Loading...
Searching...
No Matches
ratio.h
Go to the documentation of this file.
1
5
6#ifndef THEORETICA_RATIO_H
7#define THEORETICA_RATIO_H
8
9#ifndef THEORETICA_NO_PRINT
10#include <sstream>
11#include <ostream>
12#endif
13
14
15namespace theoretica {
16
22 template <typename T1, typename T2 = T1>
23 class ratio {
24 public:
25
28
31
34 ratio() : num(T1()), den(T2()) {}
35
40 ratio(const T1& n, const T2& d) : num(n), den(d) {}
41
42 ~ratio() = default;
43
45 inline ratio operator*(const ratio& r) const {
46 return ratio(num * r.num, den * r.den);
47 }
48
50 inline ratio operator/(const ratio& r) const {
51 return ratio(num * r.den, den * r.num);
52 }
53
55 inline ratio operator+(const ratio& r) const {
56 return ratio(num * r.den + r.num * den, den * r.den);
57 }
58
60 inline ratio operator-(const ratio& r) const {
61 return ratio(num * r.den - r.num * den, den * r.den);
62 }
63
65 inline ratio operator*(const T1& a) const {
66 return ratio(num * a, den);
67 }
68
70 inline ratio operator/(const T2& b) const {
71 return ratio(num, den * b);
72 }
73
81 template<typename T>
82 inline T eval_as() {
83 return static_cast<T>(num) / static_cast<T>(den);
84 }
85
86
90 inline T2 eval() {
91 return static_cast<T2>(num) / static_cast<T2>(den);
92 }
93
94
100 inline operator T2() {
101 return eval();
102 }
103
104
105#ifndef THEORETICA_NO_PRINT
106
108 inline std::string to_string() const {
109
110 std::stringstream res;
111 res << num << "/" << den;
112 return res.str();
113 }
114
115
117 inline operator std::string() {
118 return to_string();
119 }
120
121
124 inline friend std::ostream& operator<<(std::ostream& out, const ratio<T1, T2>& obj) {
125 return out << obj.to_string();
126 }
127
128#endif
129
130 };
131}
132
133
134#endif
representing a ratio between two objects, like a fraction or a rational polynomial.
Definition ratio.h:23
ratio(const T1 &n, const T2 &d)
Construct the object from two objects.
Definition ratio.h:40
T2 den
The denominator.
Definition ratio.h:30
T1 num
The numerator.
Definition ratio.h:27
ratio operator/(const ratio &r) const
Divide two ratios (without explicitly using division)
Definition ratio.h:50
ratio()
Construct the object from the default constructors of the two types.
Definition ratio.h:34
T2 eval()
Evaluate the ratio as the division between numerator and denominator cast to the type of the denomina...
Definition ratio.h:90
friend std::ostream & operator<<(std::ostream &out, const ratio< T1, T2 > &obj)
Stream the ratio in string representation to an output stream (std::ostream)
Definition ratio.h:124
std::string to_string() const
Convert the ratio to string representation.
Definition ratio.h:108
ratio operator*(const ratio &r) const
Multiply two ratios.
Definition ratio.h:45
ratio operator/(const T2 &b) const
Divide the ratio by a factor.
Definition ratio.h:70
T eval_as()
Evaluate the ratio as the division between numerator and denominator cast to the specified type.
Definition ratio.h:82
ratio operator-(const ratio &r) const
Subtract two ratios.
Definition ratio.h:60
ratio operator*(const T1 &a) const
Multiply the ratio by a factor.
Definition ratio.h:65
ratio operator+(const ratio &r) const
Add two ratios.
Definition ratio.h:55
Main namespace of the library which contains all functions and objects.
Definition algebra.h:27
std::remove_reference_t< decltype(std::declval< Structure >()[0])> vector_element_t
Extract the type of a vector (or any indexable container) from its operator[].
Definition core_traits.h:134