GCC Code Coverage Report


Directory: src/
Coverage: low: ≥ 0% medium: ≥ 75.0% high: ≥ 90.0%
Coverage Exec / Excl / Total
Lines: 0.0% 0 / 0 / 40
Functions: -% 0 / 0 / 0
Branches: -% 0 / 0 / 0

num/fraction.hpp
Line Branch Exec Source
1 #pragma once
2
3 #include <ostream>
4 #include <numeric>
5
6 namespace wala {
7
8 template <typename T, typename MulT=T> struct fraction_t {
9 T numer = 0, denom = 1;
10
11 ✗ fraction_t() : numer(0), denom(1) {}
12 ✗ fraction_t(T v) : numer(v), denom(1) {}
13 ✗ fraction_t(T n, T d) : numer(n), denom(d) {
14 ✗ if (denom < 0 || (denom == 0 && numer < 0)) {
15 ✗ numer = -numer;
16 ✗ denom = -denom;
17 }
18 }
19 ✗ template <typename U, typename V> explicit fraction_t(const fraction_t<U, V> o) : numer(T(o.numer)), denom(T(o.denom)) {}
20
21 ✗ friend std::ostream& operator << (std::ostream& o, const fraction_t& f) {
22 ✗ return o << f.numer << '/' << f.denom;
23 }
24
25 ✗ friend MulT cross(const fraction_t& a, const fraction_t& b) {
26 ✗ return MulT(a.numer) * MulT(b.denom) - MulT(b.numer) * MulT(a.denom);
27 }
28
29 ✗ friend bool operator == (const fraction_t& a, const fraction_t& b) {
30 ✗ return cross(a, b) == 0;
31 }
32 ✗ friend std::strong_ordering operator <=> (const fraction_t& a, const fraction_t& b) {
33 ✗ return cross(a, b) <=> 0;
34 }
35
36 ✗ fraction_t operator + () const { return fraction_t(+numer, denom); }
37 ✗ fraction_t operator - () const { return fraction_t(-numer, denom); }
38
39 ✗ fraction_t& operator *= (const fraction_t& o) {
40 ✗ numer *= o.numer;
41 ✗ denom *= o.denom;
42 ✗ return *this;
43 }
44 ✗ fraction_t& operator /= (const fraction_t& o) {
45 ✗ numer *= o.denom;
46 ✗ denom *= o.numer;
47 ✗ return *this;
48 }
49 ✗ friend fraction_t operator * (const fraction_t& a, const fraction_t& b) {
50 ✗ return fraction_t(a.numer * b.numer, a.denom * b.denom);
51 }
52 ✗ friend fraction_t operator / (const fraction_t& a, const fraction_t& b) {
53 ✗ return fraction_t(a.numer * b.denom, a.denom * b.numer);
54 }
55
56 ✗ friend fraction_t operator + (const fraction_t& a, const fraction_t& b) {
57 ✗ return {a.numer * b.denom + b.numer * a.denom, a.denom * b.denom};
58 }
59 ✗ friend fraction_t operator - (const fraction_t& a, const fraction_t& b) {
60 ✗ return {a.numer * b.denom - b.numer * a.denom, a.denom * b.denom};
61 }
62 ✗ fraction_t& operator += (const fraction_t& o) { return *this = *this + o; }
63 ✗ fraction_t& operator -= (const fraction_t& o) { return *this = *this - o; }
64
65 ✗ fraction_t& reduce() {
66 using std::gcd;
67 ✗ T g = gcd(numer, denom);
68 ✗ numer /= g;
69 ✗ denom /= g;
70 ✗ return *this;
71 }
72 };
73
74 } // namespace wala
75