-
Notifications
You must be signed in to change notification settings - Fork 4
/
relational.h
30 lines (24 loc) · 973 Bytes
/
relational.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#pragma once
#include <cstdint>
// Relational represents a rational number with two parts: numerator and denominator
class Relational {
public:
Relational(int64_t num, int64_t den = 1);
int64_t num() const { return num_; }
int64_t den() const { return den_; }
Relational& operator=(const Relational& other);
operator double() const { return double(num_) / den_; }
private:
int64_t num_;
int64_t den_;
};
constexpr int64_t gcd(int64_t a, int64_t b);
constexpr int sgn(int64_t x);
Relational operator+(const Relational& a, const Relational& b);
Relational operator-(const Relational& a, const Relational& b);
Relational operator*(const Relational& a, const Relational& b);
Relational operator/(const Relational& a, const Relational& b);
Relational operator+(const Relational& a);
Relational operator-(const Relational& a);
bool operator==(const Relational& a, const Relational& b);
bool operator!=(const Relational& a, const Relational& b);