Theoretica
Scientific Computing
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#include "real_analysis.h"
15
16
17namespace theoretica {
18
24 template <typename T1, typename T2 = T1>
25 class ratio {
26 public:
27
30
33
36 ratio() : num(T1()), den(T2()) {}
37
42 ratio(const T1& n, const T2& d) : num(n), den(d) {}
43
44 ~ratio() = default;
45
47 inline ratio operator*(const ratio& r) const {
48 return ratio(num * r.num, den * r.den);
49 }
50
52 inline ratio operator/(const ratio& r) const {
53 return ratio(num * r.den, den * r.num);
54 }
55
57 inline ratio operator+(const ratio& r) const {
58 return ratio(num * r.den + r.num * den, den * r.den);
59 }
60
62 inline ratio operator-(const ratio& r) const {
63 return ratio(num * r.den - r.num * den, den * r.den);
64 }
65
67 inline ratio operator*(const T1& a) const {
68 return ratio(num * a, den);
69 }
70
72 inline ratio operator/(const T2& b) const {
73 return ratio(num, den * b);
74 }
75
83 template<typename T>
84 inline T eval_as() {
85 return static_cast<T>(num) / static_cast<T>(den);
86 }
87
88
92 inline T2 eval() {
93 return static_cast<T2>(num) / static_cast<T2>(den);
94 }
95
96
102 inline operator T2() {
103 return eval();
104 }
105
106
113 template <
114 typename = std::enable_if_t<std::is_integral<T1>::value>,
115 typename = std::enable_if_t<std::is_integral<T2>::value>
116 >
118
119 if (num == 0) {
120 den = 1;
121 return *this;
122 }
123
124 const int g = gcd(num, den);
125 num /= g;
126 den /= g;
127
128 return *this;
129 }
130
131
132#ifndef THEORETICA_NO_PRINT
133
135 inline std::string to_string() const {
136
137 std::stringstream res;
138 res << num << "/" << den;
139 return res.str();
140 }
141
142
144 inline operator std::string() {
145 return to_string();
146 }
147
148
151 inline friend std::ostream& operator<<(std::ostream& out, const ratio<T1, T2>& obj) {
152 return out << obj.to_string();
153 }
154
155#endif
156
157 };
158}
159
160
161#endif
representing a ratio between two objects, like a fraction or a rational polynomial.
Definition ratio.h:25
ratio(const T1 &n, const T2 &d)
Construct the object from two objects.
Definition ratio.h:42
ratio< T1, T2 > & reduce()
Reduces a fraction (ratio of integers) to lowest terms by dividing numerator and denominator by their...
Definition ratio.h:117
T2 den
The denominator.
Definition ratio.h:32
T1 num
The numerator.
Definition ratio.h:29
ratio operator/(const ratio &r) const
Divide two ratios (without explicitly using division)
Definition ratio.h:52
ratio()
Construct the object from the default constructors of the two types.
Definition ratio.h:36
T2 eval()
Evaluate the ratio as the division between numerator and denominator cast to the type of the denomina...
Definition ratio.h:92
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:151
std::string to_string() const
Convert the ratio to string representation.
Definition ratio.h:135
ratio operator*(const ratio &r) const
Multiply two ratios.
Definition ratio.h:47
ratio operator/(const T2 &b) const
Divide the ratio by a factor.
Definition ratio.h:72
T eval_as()
Evaluate the ratio as the division between numerator and denominator cast to the specified type.
Definition ratio.h:84
ratio operator-(const ratio &r) const
Subtract two ratios.
Definition ratio.h:62
ratio operator*(const T1 &a) const
Multiply the ratio by a factor.
Definition ratio.h:67
ratio operator+(const ratio &r) const
Add two ratios.
Definition ratio.h:57
Main namespace of the library which contains all functions and objects.
Definition algebra.h:27
Vector make_error()
Create a vector representing an error state, with all NaN values.
Definition algebra.h:103
Integer1 gcd(Integer1 a, Integer2 b)
Compute the greatest common divisor of two numbers using Euclid's algorithm.
Definition real_analysis.h:1286
Real functions.