FreeTensor
Loading...
Searching...
No Matches
rational.h
Go to the documentation of this file.
1#ifndef FREE_TENSOR_RATIONAL_H
2#define FREE_TENSOR_RATIONAL_H
3
4#include <iostream>
5#include <numeric>
6
7namespace freetensor {
8
9template <class T> struct Rational {
10 T p_, q_; // p_ / q_
11
12 Rational(T p = 0, T q = 1) : p_(p), q_(q) {
13 if (p == 0) {
14 q_ = 1;
15 } else {
16 T g = std::gcd(p, q);
17 p_ /= g, q_ /= g;
18 if (q_ < 0) {
19 p_ = -p_, q_ = -q_;
20 }
21 }
22 }
23
24 friend bool operator==(const Rational<T> &lhs, const Rational<T> &rhs) {
25 return lhs.p_ == rhs.p_ && lhs.q_ == rhs.q_;
26 }
27
29 T g = std::gcd(lhs.q_, rhs.q_);
30 T p = rhs.q_ / g * lhs.p_ + lhs.q_ / g * rhs.p_;
31 T q = lhs.q_ / g * rhs.q_;
32 return Rational<T>{p, q};
33 }
34
36 T g = std::gcd(lhs.q_, rhs.q_);
37 T p = rhs.q_ / g * lhs.p_ - lhs.q_ / g * rhs.p_;
38 T q = lhs.q_ / g * rhs.q_;
39 return Rational<T>{p, q};
40 }
41
43 T p = lhs.p_ * rhs.p_;
44 T q = lhs.q_ * rhs.q_;
45 return Rational<T>{p, q};
46 }
47
49 T p = lhs.p_ * rhs.q_;
50 T q = lhs.q_ * rhs.p_;
51 return Rational<T>{p, q};
52 }
53
54 friend std::ostream &operator<<(std::ostream &os, const Rational<T> &r) {
55 os << "(" << r.p_ << " / " << r.q_ << ")";
56 return os;
57 }
58
59 friend auto operator<=>(const Rational<T> &lhs, const Rational<T> &rhs) {
60 T g = std::gcd(lhs.q_, rhs.q_);
61 return rhs.q_ / g * lhs.p_ <=> lhs.q_ / g * rhs.p_;
62 }
63
65 return *this = *this + other;
66 }
68 return *this = *this - other;
69 }
71 return *this = *this * other;
72 }
74 return *this = *this / other;
75 }
76
77 Rational operator-() const { return Rational{-p_, q_}; }
78};
79
80} // namespace freetensor
81
82namespace std {
83
84template <class T>
86 return x < 0 ? -x : x;
87}
88
89} // namespace std
90
91#endif // FREE_TENSOR_RATIONAL_H
Definition: allocator.h:9
auto && lhs
Definition: const_fold.cc:70
auto auto && rhs
Definition: const_fold.cc:70
STL namespace.
freetensor::Rational< T > abs(const freetensor::Rational< T > &x)
Definition: rational.h:85
Definition: rational.h:9
friend Rational operator*(const Rational< T > &lhs, const Rational< T > &rhs)
Definition: rational.h:42
Rational(T p=0, T q=1)
Definition: rational.h:12
friend auto operator<=>(const Rational< T > &lhs, const Rational< T > &rhs)
Definition: rational.h:59
Rational & operator+=(const Rational< T > &other)
Definition: rational.h:64
friend Rational operator/(const Rational< T > &lhs, const Rational< T > &rhs)
Definition: rational.h:48
Rational & operator/=(const Rational< T > &other)
Definition: rational.h:73
friend Rational operator-(const Rational< T > &lhs, const Rational< T > &rhs)
Definition: rational.h:35
friend Rational operator+(const Rational< T > &lhs, const Rational< T > &rhs)
Definition: rational.h:28
Rational & operator*=(const Rational< T > &other)
Definition: rational.h:70
Rational & operator-=(const Rational< T > &other)
Definition: rational.h:67
friend bool operator==(const Rational< T > &lhs, const Rational< T > &rhs)
Definition: rational.h:24
friend std::ostream & operator<<(std::ostream &os, const Rational< T > &r)
Definition: rational.h:54
T p_
Definition: rational.h:10
Rational operator-() const
Definition: rational.h:77
T q_
Definition: rational.h:10