Rational.hpp
1 // Distributed under the MIT License.
2 // See LICENSE.txt for details.
3 
4 #pragma once
5 
6 #include <cstddef>
7 #include <cstdint>
8 #include <functional>
9 #include <iosfwd>
10 
11 /// \cond
12 namespace PUP {
13 class er;
14 } // namespace PUP
15 /// \endcond
16 
17 /// \ingroup UtilitiesGroup
18 /// A rational number
19 ///
20 /// This serves as a faster replacement for
21 /// `boost::rational<std::int32_t>`. As of Boost 1.65.0, arithmetic
22 /// operators average about twice as fast, and ordering operators are
23 /// about eight times as fast.
24 class Rational {
25  public:
26  // clang-tidy: google-explicit-constructor
27  // Treating integers as rationals is desired.
28  Rational(std::int32_t numerator = 0, // NOLINT
29  std::int32_t denominator = 1) noexcept;
30 
31  std::int32_t numerator() const noexcept { return numerator_; }
32  std::int32_t denominator() const noexcept { return denominator_; }
33 
34  double value() const noexcept;
35 
36  Rational inverse() const noexcept;
37 
38  Rational& operator+=(const Rational& other) noexcept;
39  Rational& operator-=(const Rational& other) noexcept;
40  Rational& operator*=(const Rational& other) noexcept;
41  Rational& operator/=(const Rational& other) noexcept;
42 
43  // clang-tidy: google-runtime-references
44  void pup(PUP::er& p) noexcept; // NOLINT
45 
46  friend Rational operator-(Rational r) noexcept;
47 
48  private:
49  std::int32_t numerator_{0};
50  std::int32_t denominator_{1};
51 };
52 
53 Rational operator+(const Rational& a, const Rational& b) noexcept;
54 Rational operator-(const Rational& a, const Rational& b) noexcept;
55 Rational operator*(const Rational& a, const Rational& b) noexcept;
56 Rational operator/(const Rational& a, const Rational& b) noexcept;
57 
58 bool operator==(const Rational& a, const Rational& b) noexcept;
59 bool operator!=(const Rational& a, const Rational& b) noexcept;
60 bool operator<(const Rational& a, const Rational& b) noexcept;
61 bool operator>(const Rational& a, const Rational& b) noexcept;
62 bool operator<=(const Rational& a, const Rational& b) noexcept;
63 bool operator>=(const Rational& a, const Rational& b) noexcept;
64 
65 std::ostream& operator<<(std::ostream& os, const Rational& r) noexcept;
66 
67 size_t hash_value(const Rational& r) noexcept;
68 
69 namespace std {
70 template <>
71 struct hash<Rational> {
72  size_t operator()(const Rational& r) const noexcept;
73 };
74 } // namespace std
Definition: Strahlkorper.hpp:14
bool operator>(const Slab &a, const Slab &b) noexcept
Slab comparison operators give the time ordering. Overlapping unequal slabs should not be compared (a...
Definition: Slab.hpp:122
auto operator*(const TensorExpression< T1, X, Symm1, IndexList1, Args1 > &t1, const TensorExpression< T2, X, Symm2, IndexList2, Args2 > &t2)
Definition: Product.hpp:89
bool operator<=(const Slab &a, const Slab &b) noexcept
Slab comparison operators give the time ordering. Overlapping unequal slabs should not be compared (a...
Definition: Slab.hpp:125
A rational number.
Definition: Rational.hpp:24
bool operator>=(const Slab &a, const Slab &b) noexcept
Slab comparison operators give the time ordering. Overlapping unequal slabs should not be compared (a...
Definition: Slab.hpp:128
bool operator<(const Slab &a, const Slab &b) noexcept
Slab comparison operators give the time ordering. Overlapping unequal slabs should not be compared (a...
Definition: Slab.hpp:117